M.P.
M.P.

Reputation: 1

Transformation of an object into a subclass object without loosing the references to the object

This is my first problem I can't solve by searching. It's a general OOP problem, but the code is in java. Perhaps I miss an essential point?

Assume there is a baseclass and a subclass. An object of the baseclass is in many lists. There is a transformer class with one duty to transform the object into a subclass object. The transformer should not know anything about the lists. But as a result of the transformation the new subclass object should be in all the lists. (replace the former base class object)

Can this be done somehow?

class BaseClass {
    //
}
 class SubClass extends BaseClass{
    //
}
 class Transformer{
    BaseClass base;
    public Transformer(BaseClass base){
        this.base = base;
    }
    public void transform(){
        //transforms the Object to a subtype-object
        // ???
        // (move the references of the old object to the new one)
        this.base = new SubClass(); //something like this (but not so!!!)
    }
}


 class Programm{
    private List<BaseClass> list1 = new ArrayList<>();
    private List<BaseClass> list2 = new ArrayList<>();
    private List<BaseClass> list3 = new ArrayList<>();
    //many more Lists
    private List<BaseClass> listn = new ArrayList<>();



    public void main() {
        BaseClass myObject = new BaseClass();
        list1.add(myObject);
        list2.add(myObject);
        list3.add(myObject);
        listn.add(myObject);

        Transformer transformer = new Transformer(myObject);
        transformer.transform();

        //required result
        // All Lists contain the transformed Object (from Type SubClass)
    }
}

Upvotes: 0

Views: 76

Answers (1)

user11100202
user11100202

Reputation: 26

What you're trying to do is luckily impossible (imagine if your objects started changing classes in the middle of your code). You can create a subclass object based on a superclass object (if you can sensibly fill in any missing properties), but you can't turn an existing object into its subclass (such that the reference equality would work as you're hoping in your example code, i.e. converting myObject would affect all the lists).

I don't know how you came up with this idea, but somewhere along the way you've gone down the wrong path. If you tell us what you're trying to achieve, we can provide you with a better solution.

Edit: Since you're doing checkers and you need to crown a piece, you have the simple choice of adding a boolean crowned property to pieces, and writing logic based on that.

In a more complex situation you could for example use the strategy pattern, where you would have a Piece class, and they would have a Type property that specifies the actual type of the piece. This way the object you put in lists (Piece) always stays the same, but if you replace its Type property, it would seem as if the piece magically changes it's type!

Upvotes: 1

Related Questions