2

I would like to combine two design patterns in my software. I have first created a composite pattern, modeling my business problem.

enter image description here

Thus with this, I model the structure of my problem well, and an object tree will be created. I want to run some business logic on this (say execute function, which returns a number of elements). But this algorithm needs to be adjustable on high level. ie I want to be able to change the execute functions for all the classes shown in the picture.

I am looking for some usefull documentation and/or tips! Any ideas?

2 Answers 2

3

Design Patterns are often combined. I think the fact that most Pattern books show each one separately is misleading to people learning about them.

Your example is a little vague so it's a little hard to fully understand what you mean, however I think factoring out the execute() function into a separate interface to turn it into a Strategy and allowing that Strategy to be injected into Class A (the root of the composite) is one solution. Class A can then inject the strategy to the other objects that are being composited.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply! I am aware its vague, the other option was explaining the business problem, which brings other trouble :-) Ok, suppose I inject a strategy in A, and A injects its children. How can A know what to inject in C/E? It only knows that it has children of the 'conceptual class'. Now I realize my diagram is confusing. It would have been better to have an aggregation relation between A and 'Conceptual class', and remove the aggregations to C and E. Am I making sense?
0

I don't see a Composite pattern working well with components being objects of heterogeneous types that don't share a common ancestor. You need all the components to inherit some kind of Component class, possibly with Composite and Leaf subclasses to specify a general behavior for leaves and nodes.

How can A know what to inject in C/E? It only knows that it has children of the 'conceptual class'

A shouldn't know it has children of the 'conceptual class'. It should only know it has Component children. C and E are the ones who know they are of the 'conceptual class'. Depending on your language, this double nature of C and E might be tricky to implement (no multiple inheritance, etc.)

Can you provide details on how the fact that Class E and Class C derive from a "conceptual superclass" interferes with the whole composite / execute() / strategy thing ? Without that knowledge, it's hard to recommend anything.

5 Comments

Thanks for the reply. Let me explain shortly: 1) The tree structure is to model a "house", the root is a house object. It contains children of the type roof, floor, and wall. 2) wall can be implemented in various ways: it can be modeled more general (length x width x hight, and thats all) (Class E), or it can later be implemented in more detail (the wall contains rows of bricks, ...) (Class C) 3) Calculating the price of my house requires the strategy, because I want to be able to switch calculation methodologies. So: one strategy might count wall differently then another.
If being a Wall doesn't change anything about the price calculation strategy, you can have class A pass the strategy around to its Components (HouseComponents ?) regardless whether they are Walls, Floors, Roofs, etc. If being a Wall does change the price calculation logic inside the strategy, then maybe you need another pattern (Visitor ?)
The price calculation strategy for the complete house, consists of all the smaller calculations of its contained components. I imagine that in the execute of House, the list of walls will be iterated, and execute will be called on each wall. Would you still use visitor pattern, knowing this?
Furthermore, I would like to be able to extend the strategy. In a general strategy, the price of a house is called, and it might calculate 1 dollar engineering/design cost per wall. But then I extend House to the class fancyHouse, and the strategy to calcFancyHouse. This calculation does the same as its ancestor, but adds 1 dollar per wall to paint it..
The visitor pattern seems to be the solution indeed. I should have googled it first :-) thanks! en.wikipedia.org/wiki/Visitor_pattern

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.