to avoid the overridable method call in constructor delegate the variables initialization and calls to overridable methods to a member method that return reference to the instance its self so that the creation of objects will look like this
SuperClass sup = new SuperClass().initialize();
SubClass sub = new SubClass().initialize();
my super class should implement methods of initializable interface it has two methods
1) initialize method in which i write code to do the following in order
a) call setter methods and other initialization code
b) make call to overridable method putting them at end of the method
2) isInitialzed method to check if the initialization done or not when a call to a method in my class that reference a variable that is initialized in initialize method and initialization not done i throw my NonInitializedInstanceException
the subclass should override the initialize method and do the following in order
1)initialize its variables
2)can call super method that don't throw NonInitializedInstanceException
3)call super.initialize() method
4)call setter methods "note that the super call the setters so to avoid our setter been useless we should call it after super initialize method call"
5)can call super method that throw NonInitializedInstanceException
the following are my code
the interface
/**
* @author ahmed mazher
* @param <T> making class initializable
*
*/
public interface Initializable<T> {
/**
* do the initialization job and return reference to the implementing
* class current instance
* @return
*/
T initialize();
/**
* test the variables that you initialize in the initialize method
* if it was initialized or not "or what ever test you see appropriate"
* @return
*/
boolean isInitialized();
}
the NonInitializedInstanceException class
/**
* @author Ahmed mazher
* runtime exception to be thrown by method that reference variables that
* are initialized by the initialize method of the initializable interface
* you can test if initialization was done or not by isInialized method
* of the same interface if false throw the exception
*/
public class NonInitializedInstanceException extends RuntimeException {
private static final long serialVersionUID = 1L;
@Override
public String getMessage() {
return super.getMessage()
+ " you must call instance.initialize() method "
+ "before calling this method";
}
}
example of using them
/**
* @author ahmed mazher
* testing the initializer pattern
*/
class LearnJava {
public static void main(String args[]) {
SuperClass sup = new SuperClass().initialize();
SubClass sub = new SubClass().initialize();
}
}
class SuperClass implements Initializable<SuperClass> {
private String initializeDependentName;
private final String nonInitializeDependentVal = "well done";
@Override
public SuperClass initialize() {
//initializing super Class fields
setInitializeDependentName("super class");
//overidable method call in initializer "replacement of
//call in constructor"
overridablePrintName();
return this;
}
public final String getInitializeDependentName() throws NonInitializedInstanceException {
if (!isInitialized()) {
throw new NonInitializedInstanceException();
}
return initializeDependentName;
}
public final void setInitializeDependentName(String initializeDependentName) {
this.initializeDependentName = initializeDependentName;
}
public final void youCanCallMeBeforeInitialization(){
System.out.println(nonInitializeDependentVal);
}
public void overridablePrintName() {
System.out.println("I'm the super method and my name is " + getInitializeDependentName());
}
@Override
public boolean isInitialized() {
return initializeDependentName != null;
}
//to avoid some one messing with isInitialized() make your
//private one and use it instead but don't forget to call it inside
//the body of isInitialized() to maintian integrity of inheritance hierarchy
// private boolean privateIsInitialized(){
// return initializeDependentName != null;
// }
// @Override
// public boolean isInitialized() {
// return privateIsInitialized();
// }
}
class SubClass extends SuperClass {
private String job;
@Override
public SubClass initialize() {
//initializing subClass fields
setJob("testing initialize method");
//call super method that throws nonInitialiezinstanceException
//before call super.initialize() method will rise the exception
//unless you overided the isInitialized method "welling to take the risk"
//------------------------------------------------------------------
//System.out.println(getName().toUpperCase());
//------------------------------------------------------------------
//call super method that not throws nonInitialiezinstanceException
//before call super.initialize() method
youCanCallMeBeforeInitialization();
setInitializeDependentName("subClass");
//calling super.initialize() method initialize super methods
super.initialize();//initialize the super class
//call super method that throws nonInitialiezinstanceException
//after call super.initialize() method will not rise the exception
//unless you overided the isInitialized method in stupid way ^_^
System.out.println(getInitializeDependentName().toUpperCase());
//calling the setter method now to change my property as calling
//it before the super initialization is useless as the super
//initialization will erase it
setInitializeDependentName("subClass");
System.out.println(getInitializeDependentName().toUpperCase());
return this;
}
public final void setJob(String job) {
this.job = job;
}
@Override
public void overridablePrintName() {
System.out.println("i'm overriden method and i do " + job);
}
@Override
public boolean isInitialized() {
//return false;//stupid version
//return true;//take the risk
return job != null && super.isInitialized();//a good one
}
}
i hope it be a good idea waiting for your comments and suggestions
Aucun commentaire:
Enregistrer un commentaire