Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy/Pattern to bypass a super() constructor

it's me again with my legacy Java App. I have a core class that handles a lot of the important logic in the App. Let's called it MyManager. MyManager inherits from a class in a library, let's call it TopManager. The constraint is you can't modify any of these definition.

public class MyManager extends TopManager {

    public MyManager(String a, String b){ 
        super(a);
        initialize(b);
    }

    public MyManager(){
        this(null,null);
    }

    private void initialize(String b){
        //specific implementation
    }
}

public class TopManager {
    public TopManager(String s){ 
        //some implementation
    } 
}

I want to create a class CustomManager who have all the MyManager features but using TopManager default constructor.

public class CustomManager extends MyManager
    public CustomManager(){

     //super(); NO should call TopManager constructor
    }

Since it's not possible in Java, I am looking for other bright ideas

like image 376
mahery rafara Avatar asked Dec 06 '25 17:12

mahery rafara


2 Answers

I want to create a class CustomManager who have all the MyManager features

yes, using composition

but using TopManager default constructor.

by using generalization

class CustomManager extends TopManager { // have all properties of TopManager

  private MyManager myManager; // have all behaviours of MyManager by delegating the call

}
like image 125
sanbhat Avatar answered Dec 08 '25 07:12

sanbhat


Your CustomManager should inherit from TopManager instead of MyManager.

like image 22
eternay Avatar answered Dec 08 '25 07:12

eternay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!