Thinking inheritance + polymorphism
Here is the code, I will make points after ...
public abstract class WeirdPerson() { public virtual void DoThis() {
WeirdPerson is a more general concept that defines the basic material for all subtypes.- The subtype will receive "fixed" things, such as
Name - inheritance - Subtype may
override default behavior (methods) - polymorphism - now we have no fun things:
public class WeirdDad : WeirdChild
- Constructors
- Constructors
Version_1 , Version_2 force the client to provide the corresponding subtype of WeirdPerson .- The client must provide us with all the necessary materials.
- We can check / cross-check the incoming arguments.
- We don't need any stinkin '
interfaceabstract class allows us to have default behavior (methods) and default state (properties)public methods and properties of any class are an interface in a general sense. We do not need to have (C # keyword) interface in order to follow the main code for non-executable interfacesabstract methods enforce the implementation of a subclass. just. as. up interface .
new- A WARNING. The hide method at this point breaks the inheritance chain. If we then inherit this class, we do not inherit the implementation of the method of the original base class.
- Liskov is happy.
Additional refactoring
Make Version hierarchy parallel to WeirdPerson hierarchy
Assuming this matches your design. I think in a year you will be glad you did.
public abstract class Version_X {
Edit - Motivated discussion of comment with DVK
The principle of least knowledge states that the client does not need to know the internal details to use the class. The need to know how to properly compile Version and Weird is a violation, it can be argued.
Assume that the default constructor, for Visitor let's say, is needed elsewhere in the overall design. This means that the client can manage the Version / Weird composition.
Abstraction - loose communication - is in the abstract classes. Concrete classes must necessarily be correctly composed, so that a "strong connection" is inherent in the creation of specific objects (parameters of the constructor of the explicit type), but the basic free communication provides the desired flexibility.
public enum WeirdFamily { Child, Dad, Mother, TheThing } public static class AdamsFamilyFactory () { public static Version_X Create (WeirdFamily familyMember) { switch (familyMember) { case Dad: return BuildAdamsDad();
source share