Skip to main content
deleted 103 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

java Java member method initialization pattern

toTo avoid the overridable method call in constructor dilemma this approache delegate the variables initialization and calls to overridable methods to a member method that return reference to the instance its self and exception to be thrown if trying to use instance without invoking the initialization method/s so that the creation of objects will look like this:

    SuperClass sup = new SuperClass().initialize("big");
    SubClass sub = new SubClass().initialize("sato", "try initialzer");
    SubClass sub2 = new SubClass().initialize("happy");
    SubClass sub3 = new SubClass().initialize();

myMy 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

    Initialize method in which I write code to do the following in order

  2. Call setter methods and other initialization code

  3. Make call to overridable method putting them at end of the method. This method can be overloaded to provide versions of it with necessary arguments as illustrated in complex example

  4. The 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.

a) call setter methods and other initialization code
b) make call to overridable method putting them at end of the method
this method can be overloaded to provide versions of it with necessary arguments as illustrated in complex example

  1. 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

theThe subclass should override the initialize method/s "all the super class versions of 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:

handling overloaded versions of initialize done by calling the one with more parameters from the less parameter one

  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

Handling overloaded versions of initialize done by calling the following are my codeone with more parameters from the one with fewer parameters.

theThe interface:

the NonInitializedInstanceException classThe NonInitializedInstanceException class:

exampleExample of using them:

aA more complicated example using initializer with arguments:

suggestionSuggestion of using static method factory pattern and as i. As I expected, static pain never ends here. Here is my trial to adapt my pattern into static one but i. I failed i, so I don't know how to make subClass invoke superClass and create a method to affect its instance.

i hope it be a good idea waiting for your comments and suggestions

java member method initialization pattern

to avoid the overridable method call in constructor dilemma this approache delegate the variables initialization and calls to overridable methods to a member method that return reference to the instance its self and exception to be thrown if trying to use instance without invoking the initialization method/s so that the creation of objects will look like this

    SuperClass sup = new SuperClass().initialize("big");
    SubClass sub = new SubClass().initialize("sato", "try initialzer");
    SubClass sub2 = new SubClass().initialize("happy");
    SubClass sub3 = 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
this method can be overloaded to provide versions of it with necessary arguments as illustrated in complex example

  1. 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/s "all the super class versions of 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

handling overloaded versions of initialize done by calling the one with more parameters from the less parameter one

the following are my code

the interface

the NonInitializedInstanceException class

example of using them

a more complicated example using initializer with arguments

suggestion of using static method factory pattern and as i expected static pain never ends here is my trial to adapt my pattern into static one but i failed i don't know how to make subClass invoke superClass create method to affect its instance

i hope it be a good idea waiting for your comments and suggestions

Java member method initialization pattern

To avoid the overridable method call in constructor dilemma this approache delegate the variables initialization and calls to overridable methods to a member method that return reference to the instance its self and exception to be thrown if trying to use instance without invoking the initialization method/s so that the creation of objects will look like this:

SuperClass sup = new SuperClass().initialize("big");
SubClass sub = new SubClass().initialize("sato", "try initialzer");
SubClass sub2 = new SubClass().initialize("happy");
SubClass sub3 = 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

  2. Call setter methods and other initialization code

  3. Make call to overridable method putting them at end of the method. This method can be overloaded to provide versions of it with necessary arguments as illustrated in complex example

  4. The 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/s "all the super class versions of 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

Handling overloaded versions of initialize done by calling the one with more parameters from the one with fewer parameters.

The interface:

The NonInitializedInstanceException class:

Example of using them:

A more complicated example using initializer with arguments:

Suggestion of using static method factory pattern. As I expected, static pain never ends. Here is my trial to adapt my pattern into static one. I failed, so I don't know how to make subClass invoke superClass and create a method to affect its instance.

Source Link

java member method initialization pattern

to avoid the overridable method call in constructor dilemma this approache delegate the variables initialization and calls to overridable methods to a member method that return reference to the instance its self and exception to be thrown if trying to use instance without invoking the initialization method/s so that the creation of objects will look like this

    SuperClass sup = new SuperClass().initialize("big");
    SubClass sub = new SubClass().initialize("sato", "try initialzer");
    SubClass sub2 = new SubClass().initialize("happy");
    SubClass sub3 = 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
this method can be overloaded to provide versions of it with necessary arguments as illustrated in complex example

  1. 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/s "all the super class versions of 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

handling overloaded versions of initialize done by calling the one with more parameters from the less parameter one

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 
    }

}

a more complicated example using initializer with arguments

class LearnJava {

    public static void main(String args[]) {
        SuperClass sup = new SuperClass().initialize("big");
        SubClass sub = new SubClass().initialize("sato", "try initializer");
        SubClass sub2 = new SubClass().initialize("happy");
        SubClass sub3 = new SubClass().initialize();
    }

}

class SuperClass implements Initializable<SuperClass> {

    private String initializeDependentName;
    private final String nonInitializeDependentVal = "well done";

    @Override
    public SuperClass initialize() {
        return initialize("i'm super with no name");
    }

    public SuperClass initialize(String name) {
        //initializing super Class fields
        setInitializeDependentName(name);
        //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 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() {
        return initialize("i'm subclass no one give me name");
    }

    @Override
    public SubClass initialize(String name) {
        return initialize(name, "no one give me job");
    }

    public SubClass initialize(String name, String job) {
        //initializing subClass fields
        setJob(job);
        //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();
        //calling super.initialize() method initialize super methods
        super.initialize(name);//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 stuped 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("setter called in 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 overiden method my name is "
                + getInitializeDependentName()
                + " and i do " + job);
    }

    @Override
    public boolean isInitialized() {
        //return false;//stuped version 
        //return true;//take the risk
        return job != null && super.isInitialized();//a good one 
    }

}

suggestion of using static method factory pattern and as i expected static pain never ends here is my trial to adapt my pattern into static one but i failed i don't know how to make subClass invoke superClass create method to affect its instance

class LearnJava {

    public static void main(String args[]) {
        SuperClass.create("nono");
        SubClass.create("sato","test factory");
        
    }

}

class SuperClass{

    private String name;
    protected SuperClass() {
    }

    public static SuperClass create(String name) {
        SuperClass sup = new SuperClass();
        //initializing super Class fields
        sup.setName(name);
        //overidable method call useless as i always call the super instance version 
        sup.overridablePrintName();
        return sup;
    }

    public final String getName(){
        return name;
    }

    public final void setName(String name) {
        this.name = name;
    }
    
    public void overridablePrintName() {
        System.out.println("i'm the super method my name is " + getName());
    }
}

class SubClass extends SuperClass {

    private String job;

    protected SubClass() {
        super();
    }

    public static SubClass create(String name, String job) {
        SubClass sub = new SubClass();
        //initializing subClass fields
        sub.setJob(job);
        //call the super class initializer to affect this sub class instatnce 
        //seems no way to do it
        sub.create(name);
        SubClass.create(name);
        SuperClass.create(name);
        return sub;
    }

    public final void setJob(String job) {
        this.job = job;
    }

    @Override
    public void overridablePrintName() {
        System.out.println("i'm overiden method my name is "
                + getName()
                + " and i do " + job);
    }
}

i hope it be a good idea waiting for your comments and suggestions