I have a class, something like this one (C#):public class MyFavoriteClass {   public string Name { get; set; }   public string Description { get; set; }   (...)   public ReferencedClass1 ReferencedClass1 {get; protected set;}   public ReferencedClass2 ReferencedClass2  {get; protected set;}   public ReferencedClass3 ReferencedClass3  {get; protected set;}   public AnotherReferencedClass1 AnotherReferencedClass1  {get; protected set;}   public AnotherReferencedClass2 AnotherReferencedClass2  {get; protected set;}   public AnotherReferencedClass3 AnotherReferencedClass3  {get; protected set;}}Referenced classes dont have any common ancestor class.There are constrains:Object (instance of MyFavoriteClass) can have setted only one of properties ReferencedClass1, ReferencedClass2 and ReferencedClass3. Others have to be null.Same rule for AnotherReferencedClass1, AnotherReferencedClass2 and AnotherReferencedClass3.Whats the best way to enforce these constraints? In factory pattern? Should I write nine methods in this factory? Or should i wrapp these options in another object?
Best way to enforce constraints in Class
c#;design;design patterns;object oriented design
Looks like MyFavoriteClass itself is a problem and should be split. Create specializations of this class which can only contain a specific pair of referenced types, that's how you get the constraint.Then use a factory which is capable of emitting the correct specializations of MyFavoriteClass. You may either provide an overload for each combination of possible parameters or just rely on dynamic type detection - that's your choice.Alternative approach, only create a wrapper class including specializations encapsulating each of the triplets - that way MyFavoriteClass doesn't need to know about specific details and you can avoid having to create all 9 combinations.That's only if MyFavoriteClass doesn't need to care about special cases though.