Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen using the decorator pattern, can I add new decorators within a concrete decorator object?
    primarykey
    data
    text
    <p>Example:</p> <pre><code>class Display { public: virtual void display() = 0; }; class PageDisplay : public Display { public: void display() { /* ... */ } }; class DisplayDecorator : public Display { public: DisplayDecorator(Display* display) : m_display(display) {} virtual void display() { m_display-&gt;display(); } private: Display* m_display; }; class BorderDecorator : public DisplayDecorator { public: BorderDecorator(Display* display) : DisplayDecorator(display) {} virtual void display() { DisplayDecorator::display(); /* do border stuff here ... */ } }; int main() { Display* pageDisp = new BorderDecorator(new PageDisplay()); pageDisp-&gt;display(); } </code></pre> <p>So, this is a fairly rudimentary implementation of the decorator pattern. But let's say, for instance, that I wanted to add an additional decorator FROM WITHIN the BorderDecorator concrete class. So the BorderDecorator::display function would now look something like this:</p> <pre><code>virtual void display() { /* I need to add a decorator that will display a slider bar at the side of the screen, and that will wrap the PageDisplay decorator so that it will run before the page display. Is there a clean way to get my base class' m_display pointer so that I can do something like this: */ m_display = new SliderDecorator(m_display); DisplayDecorator::display(); /* do border stuff here ... */ } </code></pre> <p>I also know that in the case of this example, I would really just want to apply the SliderDecorator from within the client function (main in this case). Off the top of my head I cannot come up with a good example of why one would need to add additional decorators from within concrete decorator objects (besides what I am working on), but bare with me; I do have a good reason for doing so. I also know I could make the abstract base class pointer in the abstract decorator object (m_display in this example) protected instead of private, and then add additional decorators from within a concrete decorator object just like I did in the example, but this seems like a bad idea as it breaks down data encapsulation, and now decorators could improperly interact with the objects that they decorate. That being said, is that my only option here? Is there a better way from within a concrete decorator's "decoration" function to wrap the composite decorator with another decorator and then continue on like normal?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload