By creating an instance of a class
using the new keyword, all constructors in the chain are
called.
You may creating more than one object
Thik this case.. Result may be severe,
If you need to create a new instance of a class, you can
use the clone() method of an object that implements the
cloneable interface. The clone method doesn’t invoke any
class constructors.
If you’ve used design patterns as part of your architecture
and use the factory pattern to create objects, the change will
be simple. Listed below is the typical implementation of the
factory pattern.
public static Account getNewAccount() {
return new Account();
}
The refactored code using the clone method may look
something like this:
private static Account BaseAccount = new Account();
public static Account getNewAccount() {
return (Account) BaseAccount.clone();
}
The above thought process is also useful for the implementation
of arrays.
Reference:James McGovern
No comments:
Post a Comment