class HeavyweightObjcet
{
public void operate() {
System.out.println("Operating...");
}
}
class LazyInitializer
{
HeavyweightObjcet objcet;
public void operate()
{
if (objcet == null)
objcet = new HeavyweightObjcet();
objcet.operate();
}
}
Here I'm making a virtual proxy for a heavyweight object. Each time before calling HeavyweightObject::operate
, the program checks first whether the object is null
or not. This part is checked once and only once through the entire lifetime of the object.
A possible improvement maybe using the state pattern like this:
class HeavyweightObjcet
{
public void operate() {
System.out.println("Operating...");
}
}
class LazyInitializer
{
HeavyweightObjcet objcet;
State state = new UninitializedState(this);
public void operate()
{
state.operate();
}
}
abstract class State
{
LazyInitializer initializer;
public State(LazyInitializer initializer)
{
this.initializer = initializer;
}
abstract void operate();
}
class UninitializedState extends State
{
public UninitializedState(LazyInitializer initializer) {
super(initializer);
}
@Override
public void operate() {
initializer.objcet = new HeavyweightObjcet();
initializer.state = new InitializedState(initializer);
initializer.operate();
}
}
class InitializedState extends State
{
public InitializedState(LazyInitializer initializer) {
super(initializer);
}
@Override
public void operate() {
initializer.objcet.operate();
}
}
Does this solution make sense?
Is there any possible improvement to the code?
Are there any examples to something like this that's done before?
Is it an unnecessary complication or does it worth it or does it depend on the situation?
Does it make the code faster? I mean, the extra function calls may be slower than just a simple conditional.
Is it an unnecessary complication or does it worth it or does it depend on the situation?
While it is completely fine to have only 2 states when using the State Pattern, it is most definitely an overkill in this particular case for the following reasons :
UninitializedState
-> InitailizedState
. Once HeavyWeightObjcet
has been initialized, you are most definitely not going to
alternate between transitioning from InitializedState
-> UninitializedState
or
vice-versaoperate
method thread-safe?)Does it make the code faster? I mean, the extra function calls may be slower than just a simple conditional.
This is too small an area to be worrying about when it comes to performance Read : micro-optimization
Last but not the least, the State Pattern allows us to adhere to the Open-Closed principle.. As the example stands, there is no convincing reason for the operate
method to change as far as initialization of the HeavyWeightObject
is concerned. Moreover, the initialization code should be in the constructor rather than the operate
method in the first place.