Tutorial :Which pattern to use in replacement of composite with limited number of element type?



Question:

I have an object can be compose by components but each components has a type and should be unique :

Class Client{    Set<IComposite> elements    }  interface IComposite{    String getType();  }  class Status implements IComposite{    String getType(){return "status"}  }  class ClientDates implements IComposite{    String getType(){return "clientdate"}  }      

So I suppose I could encapsulate the collection but each element should be unique, so only 1 status, only one clentdate, but perhaps can I create a new Composite class that could be multiple.

An idea how to design that ?

Thanks a lot


Solution:1

you may have a Dictionary as the collection of your elements with their unique id(type in your case) as the dictionary's key. while adding the element you can check if the element is exist in the dictionary. So you can keep the collection with unique elements.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »