Search code examples
javadesign-patternsconstructorclonefactory

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


Imagine you have a derived class, where the base class is something you cannot modify. The base class has a lot of state (many non-constant private members) and many constructors, with varying numbers of arguments to initialize some subset of the state (the size of the subset varies by constructor, of course).

Now my derived class is a very lightweight wrapper over the base class. Let's assume it adds no state of its own, and only slightly modifies the behavior of a couple methods (perhaps doing some additional logging around a super.originalMethod() call).

The issue I have is that I want to take an object of the base class, and create a "copy" of it, with the same state, but as an instance of my derived class.

This is proving difficult. I can't call the "most complete" constructor of the base class, passing all the state from the source by calling getters, since depending on how the base class was constructed, some of the state values may be rejected by this constructor. For example, you can create a default object with a 0-arg ctor, any many values will be null. It is not, however, legal to pass null values in the ctor that allows you to specify these values.

Furthermore, the method above is fragile because if a modification to the base class occurs which adds more state, and "even more complete" constructor (or state which can't be set in a constructor, but only through accessor methods) is added, the copy won't be complete any more.

What I want is like `clone(), but rather initializing a new object of the same type, initialize the base class members of the derived class. I guess such a thing doesn't exist. Any suggestions on a pattern that might offer something equivalent?

Keep in mind I cannot modify the base class. If I could, this would be much easier.


Solution

  • If you can override all public methods, you can save the source object as the delegate

    Class D extends B
        B src;
        D(B src){ super(whatever); this.src=src; }
    
        public method1(){ src.method1(); }