Which pattern to use in replacement of composite with limited number of element type?
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开发者_Python百科();
}
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
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.
精彩评论