I am having trouble correctly modeling related objects that can use templates. This is not homework, but part of a small project in the university.
In this application the user can add several elements, which can either be passive or active. Each concrete element has different attributes, these must be set by the user. See diagram 1:
开发者_开发问答Since the user will create many elements, we want there to be templates for each type of element, so some of the attributes are filled in automatically. See diagram 2:
In my opinion, this is a bad design. For example, to get all possible templates for a PassiveElementA-object, there has to be a list/set somewhere that only holds PassiveElementATemplate-objects.
There has to be a separate list for each subclass of Element. So if you wanted to add a new PassiveElement-child, you also have to edit the class which holds all these separate lists.
I cannot figure out a good way to solve this problem. Since the concrete classes (i.e. PassiveElementA, ..., PassiveElementZ) have so many different attributes, many of the design patterns I know do not work.
Thanks in advance for any hints, and sorry for my bad English.
If I understood you correctly and by template you mean object, which would have complicated construction and therefore you have many ready objects, which get copied as you need them, you might consider using Prototype design pattern. Otherwise, maybe some sort of Abstract Factory or maybe even Dependency Injection. It might help if you provide reasons for deciding to have it the way your design looks like now.
精彩评论