I have a class Container, which represents wrapper around std::list.This class contains following public methods:void add(Shape *item)void next()void setBegin()Shape* curr()and protected variables std::list <Shape*> _containerstd::list <Shape*>::iterator _iterNow I need add sorting of Container without changing existing class code.Which pattern should I use? I think, that Decorator is most appropriate pattern in this case.Should I redesign class Container?
Add behavior without changing existing class
c++;object oriented design
Container as it stands is both a list and an iterator, so I would actually suggest breaking it into two classes - one for each of the responsibilities. You can then make an alternative implementation of the list container that is sortable.e.g.class UnorderedContainer : Container{}class SortableContainer : Container{    void Sort();}abstract class Container{    void Add(Shape *item);    Iterator GetIterator();}class Iterator{       Shape* Current();    void Next();}I'd also carefully consider whether the list is mutable while you are using next() to iterate through it. It's not at all obvious what would happens when you call next() after a sort() - can it revisit items that you have already stepped through? If you break out the iterator into its own class you could have it act on an immutable copy of the list taken at the point where you began the iteration.