0

I have an abstract class AxisState with two concrete subclasses XState and YState. They're supposed to be immutable objects with a bunch of methods that return a different AxisState. Most of these methods are completely defined in AxisState, and a few of them are abstract and implemented in the concrete subclasses.

The problem is that I need to somehow return a new instance of the current concrete subtype of AxisState from within the methods entirely implemented in AxisState.

What I'm currently doing is implementing a method abstract AxisState NewStateOfSameType() which is implemented as returning a new XState or YState from within these subtypes, respectively.

public abstract class AxisState {
    public abstract AxisState NewStateOfSameType(/*...*/);
    public abstract AxisState OverloadMethod();
    public abstract AxisState AnotherOverloadMethod();

    public AxisState(int fieldOne, int fieldTwo) {
        // ...
    } 

    public AxisState NextState(/*...*/) {
        // ...
        return NewStateOfSameType(fieldOne, fieldTwo);
    }

    // ...
}

And the implementation in XState/YState:

public class XState : AxisState {    
    public XState(int fieldOne, int fieldTwo) : base(fieldOne, fieldTwo) {}
    
    public override AxisState NewStateOfSameType(int fieldOne, int fieldTwo) {
        return new XState(fieldOne, fieldTwo);
    }

    // Abstract methods implemented here
}
public class YState : AxisState {    
    public YState(int fieldOne, int fieldTwo) : base(fieldOne, fieldTwo) {}
    
    public override AxisState NewStateOfSameType(int fieldOne, int fieldTwo) {
        return new YState(fieldOne, fieldTwo);
    }

    // Abstract methods implemented here
}

Is there a cleaner way to handle all this, or some way to restructure this whole thing?

directquest
  • 130
  • 10
  • What do these classes try to achieve? (XState, YState) What is the difference between them? What do they have in common? Are the differences more "logical" than in what they achieve as classes? Are you trying to create XState from XState and YState from YState? (For this You might be able to instead use a generic base class.) – Alexandru Clonțea Aug 01 '21 at 13:02
  • The reason I ask is that maybe one class would suffice (AxisState) and you would have one instance of AxisState called XState and another called YState – Alexandru Clonțea Aug 01 '21 at 13:03
  • They're essentially the same structure with the same fields and methods, but some of the methods are implemented one way in XState and another in YState (so there is some logical difference). – directquest Aug 01 '21 at 13:08
  • Yes I am trying to create XState from XState and YState from YState. – directquest Aug 01 '21 at 13:09
  • Perhaps this question helps you: https://stackoverflow.com/questions/78536/deep-cloning-objects – Jose Luis Aug 01 '21 at 13:24
  • 1
    @JoseLuis My objects are immutable so I can't make changes to them after they've been cloned. – directquest Aug 01 '21 at 13:36

0 Answers0