java - Pattern to initialize base class in derived class constructor (or factory) -


imagine have derived class, base class cannot modify. base class has lot of state (many non-constant private members) , many constructors, varying numbers of arguments initialize subset of state (the size of subset varies constructor, of course).

now derived class lightweight wrapper on base class. let's assume adds no state of own, , modifies behavior of couple methods (perhaps doing additional logging around super.originalmethod() call).

the issue have want take object of base class, , create "copy" of it, same state, instance of derived class.

this proving difficult. can't call "most complete" constructor of base class, passing state source calling getters, since depending on how base class constructed, of state values may rejected constructor. example, can create default object 0-arg ctor, many values null. not, however, legal pass null values in ctor allows specify these values.

furthermore, method above fragile because if modification base class occurs adds more state, , "even more complete" constructor (or state can't set in constructor, through accessor methods) added, copy won't complete more.

what want `clone(), rather initializing new object of same type, initialize base class members of derived class. guess such thing doesn't exist. suggestions on pattern might offer equivalent?

keep in mind cannot modify base class. if could, easier.

if can override public methods, can save source object delegate

class d extends b     b src;     d(b src){ super(whatever); this.src=src; }      public method1(){ src.method1(); } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -