I am working on a piece of software designed to create a book like document. I have implemented it using using decorators to add various features. Here is an example of the initialization'
$this->chapter[i] = new ChapDecorator1(new ChapDecorator2(new Chapter(i)));
The challenge is that these decorators are hard coded and that there is the potential for many iterations of the sofware. Interesting changes made on new projects are often backported to the older ones (which isn't terrible now with decorators since all that is required is adding another decorator to the definition). However, this still requires me to edit the subclass code. The optimal situation being the actual content creators choosing what features they need without requiring a programmer to edit anything.
Clearly, the better thing to do in this case is for the Book to use an object implementing the Builder pattern to create the Chapter object and wrap it in the correct decorators for the project.
Finally, we arrive at my question which is how can I make the builder object handle the configuration order dynamically and correctly? The order in which decorators are wrapped with imply an order for how interface calls are resolved (LIFO). An example is that the document edit tracking is implemented as a decorator but for obvious reasons should always evaluate first to save state before changes are made (it should be the last wrapper). For future development assuming there will be many decorators, some which might need to resolve first should each decorator have something like a priority data member (integer?) which the Builder might sort on? It seems like a workable solution but I'm concerned that the implementation wouldn't be very robust if a large number of priority sensitive decorators/modules were created. Conflicting priorities might require renumbering many classes for example. Anyway, I would appreciate any thoughts people had on the matter.
The second issue is if two decorators change the same feature in some way. Should that situation even be possible? Should each decorator specify its domain and traverse the list of decorators looking for conflicts?
This is all assuming there will be many decorators and that some project editors will choose some but not others. Thanks!