Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Both Proxy and State provide a surrogate class. Your code talks to this surrogate class, and the real class that does the work is
hidden behind this surrogate class. When you call a function in the surrogate,
it simply turns around and calls the function in the implementing class. These
two patterns are so similar that, structurally, Proxy is simply a special case
of State. One is tempted to just lump the two together into a pattern called Surrogate,
but the intent of the two patterns is different. It can be easy to fall
into the trap of thinking that if the structure is the same, the patterns are
the same. You must always look to the intent of the pattern in order to be
clear about what it does.
The basic idea is simple: from a base class, the surrogate
is derived along with the class or classes that provide the actual
implementation:
When a surrogate object is created, it is given an
implementation to which it sends the function calls.
Structurally, the difference between Proxy and State is
simple: a Proxy has only one implementation, while State has more than one. The
application of the patterns is considered (in GoF) to be distinct: Proxy
controls access to its implementation, while State changes the implementation
dynamically. However, if you expand your notion of controlling access to
implementation then the two seem to be part of a continuum.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |