Hi,
We're in the process of setting up a class hierarchy, but are encountering some issues. We wondered whether any of you with more experience might be able to lend a hand, please?
At present, we have an abstract base class called Entity. Inheriting from this are ClassA, ClassB and ClassC. The vast majority of properties/methods in Entity are usable in all child classes. However, we come across some instances as below:
Entity has the method Move(). ClassA and ClassC can be moved, but ClassB cannot.
Do you have any advice for an appropriate way to implement this, please? We have looked at:
1) Having all the code in Entity, and then in ClassB.Move() throwing a NotImplemented exception. This is not ideal because the code compiles, but only dies at runtime.
2) Having an IMovable interface that contains the Move() function, and is implemented by ClassA and ClassC:
a) with the Move code defined in both classes - not ideal because of code duplication
b) with the Move code defined in a static Helper class and called within each class - not ideal if the method signature ever has to be changed
c) with the Move code defined as a protected method in Entity, with ClassA and ClassC calling base.Move() - not ideal for the same reason as above
Thanks in advance for any advice,
Liz
We're in the process of setting up a class hierarchy, but are encountering some issues. We wondered whether any of you with more experience might be able to lend a hand, please?
At present, we have an abstract base class called Entity. Inheriting from this are ClassA, ClassB and ClassC. The vast majority of properties/methods in Entity are usable in all child classes. However, we come across some instances as below:
Entity has the method Move(). ClassA and ClassC can be moved, but ClassB cannot.
Do you have any advice for an appropriate way to implement this, please? We have looked at:
1) Having all the code in Entity, and then in ClassB.Move() throwing a NotImplemented exception. This is not ideal because the code compiles, but only dies at runtime.
2) Having an IMovable interface that contains the Move() function, and is implemented by ClassA and ClassC:
a) with the Move code defined in both classes - not ideal because of code duplication
b) with the Move code defined in a static Helper class and called within each class - not ideal if the method signature ever has to be changed
c) with the Move code defined as a protected method in Entity, with ClassA and ClassC calling base.Move() - not ideal for the same reason as above
Thanks in advance for any advice,
Liz
Comment