Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 787
BeeOnRope Avatar asked Feb 23 '23 12:02

BeeOnRope


2 Answers

Favour composition over inheritance, perhaps by creating a wrapper class (as below). If your base class utilizes interfaces, your wrapper class can implement the same interfaces and delegate calls to the base class (decorator).

However, there is no robust strategy to do as you describe with inheritance. Even if you used reflection to perform a deep copy, as you pointed out, the implementation may change. You have broken encapsulation and your code would be intimately coupled with the base class.

public static void main(final String[] args) {
    final Base base = new Base("Hello");
    base.printState(); // Prints "Hello"
    final Wrapper wrapper = new Wrapper(base);
    wrapper.printState(); // Prints "Wrapper says Hello"
    wrapper.clone().printState(); // Prints "Wrapper says Hello"
}

private static class Wrapper {

    private final Base base;

    public Wrapper(final Base base) {
        this.base = base;
    }

    public Wrapper clone() {
        return new Wrapper(base);
    }

    public void printState() {
        System.out.printf("Wrapper says ");
        base.printState();
    }
}

private static class Base {

    private Object state;

    public Base(final Object state) {
        if (state == null) {
            throw new IllegalArgumentException("State cannot be null");
        }
        this.state = state;
    }

    public void printState() {
        System.out.println(state);
    }
}
like image 24
hoipolloi Avatar answered Feb 26 '23 03:02

hoipolloi


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(); }
like image 116
irreputable Avatar answered Feb 26 '23 02:02

irreputable