jeudi 25 octobre 2018

Combine Inheritance and Composition - Design Pattern

Sorry for the unclear Title. I have a following scenario:

I have 4 types: Type1, Type2, Type3, Type4. (which have same structure: name and children)

I have other 4 types of children: Child1, Child2, Child3, Child4. (which in turn have the same structure: parent and value).

Condition: Type1 can have only children of type: Child1; Type2->child2 and so on.

If I use Inheritance for this case: all of types inheritate from SuperType and all of children inheritate from Children.

public class Children{

}
public class SuperType{
private List<Children> children;
}
public class Type1 extends SupperType{}
public class Child1 extends Children{}

Type1 can have children from Child2, Child3, Child4. That's not what I want.

Do you have any idea about pattern designs which I could use for this case?

Aucun commentaire:

Enregistrer un commentaire