I am trying to implement a Composite
pattern as shown in the image below...
NOTE: Summary will have either handle1() or handle2()
1) Can I implement as shown in the figure - or should I have a base class like shown in the GoF composite structure?
2) For the summary part, I also have to implement a requirement where summary might have to be grouped by category. or user may choose to see just the overall summary.
In this case, I can have a list of child summaries. For overall sum, I can just loop through the I have 3 approaches:
A) loop through child summaries and call handle2()
In this case, each child summary will loop through the entire list and select only that satisfies its group - this will waste lot of time...
B) loop through items - do a condition check on child summaries and cal appropriate child summary's handle1()
C) Instead of the above way, I am thinking I should chain my child summaries - Chain of Resp. and handle will be passed on a need basis...
but in this case, I will need to loop through child summaries for totals - so, I end up maintaining both a List of child summaries and also the chain object in each summary - this approach saves time but uses up more memory...
is "C)" more complicated? can I combine patterns like this - composite & chain of resp?