This is a follow up of http://codea.io/talk/discussion/6446/draggable-objects-and-design-patterns.
Trying to use Simeon component template, i found out that it was not meant to manage a hierachy of objects (objects made of components, themselves made of components, etc…). To make complex objects out of simple assemblies of simple objects, is really what i am looking for. Reading the provided links, i found out that what i wanted is really the composite design pattern.
So i tried to implement one, and i found out that:
- draw() and touched() are quite easy to propagate.
- update() is a real headache, because i want 2 way communication between parent and children, and propagation of the connunications through the whole tree, and coherent results.
I also came to the conclusion that if i want a 2 way communication, the Composite class will be more complex to understand and use. So i decided to split my goal in 3 targets, from the simplest (and weakest) to the most complex (but hopefully more useful):
Composite0: is a composite class that manage only top-down information flow. It is close to Simeon Component, with the addition that the top-bottom communication process is taken in charge via the definition of an interface table.
Composite1: is like Composite1, except 1 feedback variable is passed around, down and up, and used to block the update of all the tree is one component refuses the consequence of the update. This is the minimal tbottom-up control flow to do something useful.
Composite2: is a composite class that manage top-down and bottom-up information flow, via 2 interfaces, and control the flow via a meta variable. Component order is made unimportant. Conflicts between components are managed with an update rule (that can be customized for each variable passed up). Infinite loops are managed. The price to pay is more complexity of use (in the sub classes : 2 interfaces to define, a bit of meta communication (refuseUpdate, sendMessage), and you have to avoid creating pitfalls), more memory and cpu used for updating. It seems to work well though, i have to use it to relky make something to verify it really helps to build complex reusable objects out of simpler ones.
I’ve made a toy project (a series of boxes in hierarchy) and implented it with the 3 Composite class, to verify my code works as advertized. I will post the 3 projects below, and hope some of you are interested and provide me some feedback.