views:

19

answers:

1

Hello everybody!

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: 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: 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.

+1  A: 

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.

Gabriel Ščerbák
The Prototype pattern looks very good for this, I'll try a small implementation. Thank you.
Duddle
@Duddle no problem, design patterns are great pieces of design knowledge:) ...btw. if you get into some sort of space issues with the prototypes, check-out the Flyweight pattern, might help.
Gabriel Ščerbák