Page 209 -
P. 209

192   Chapter 7   Design and implementation



                                 Subject                               Observer
                             Attach(Observer)                          Update( )
                             Detach(Observer)
                             Notify( )         for All o in observers
                                                 o -> Update( )




                             ConcreteSubject                        ConcreteObserver
                                                                                        observerState =
                             GetState( )        return subjectState  Update( )        subject -> GetState( )
                             subjectState                           observerState




                                    detail to the solution description. Figure 7.12 is the representation in UML of the
                   Figure 7.12 A UML
                   model of the Observer  Observer pattern.
                   pattern            To use patterns in your design, you need to recognize that any design problem
                                    you are facing may have an associated pattern that can be applied. Examples of such
                                    problems, documented in the ‘Gang of Four’s original patterns book, include:

                                    1.  Tell several objects that the state of some other object has changed (Observer
                                        pattern).
                                    2.  Tidy up the interfaces to a number of related objects that have often been devel-
                                        oped incrementally (Façade pattern).

                                    3.  Provide a standard way of accessing the elements in a collection, irrespective of
                                        how that collection is implemented (Iterator pattern).
                                    4.  Allow for the possibility of extending the functionality of an existing class at
                                        run-time (Decorator pattern).

                                      Patterns support high-level, concept reuse. When you try to reuse executable
                                    components you are inevitably constrained by detailed design decisions that have
                                    been made by the implementers of these components. These range from the
                                    particular algorithms that have been used to implement the components to the
                                    objects and types in the component interfaces. When these design decisions con-
                                    flict  with  your  particular  requirements,  reusing  the  component  is  either
                                    impossible or introduces inefficiencies into your system. Using patterns means
                                    that you reuse the ideas but can adapt the implementation to suit the system that
                                    you are developing.
                                      When you start designing a system, it can be difficult to know, in advance, if you
                                    will need a particular pattern. Therefore, using patterns in a design process often
                                    involves developing a design, experiencing a problem, and then recognizing that a
                                    pattern can be used. This is certainly possible if you focus on the 23 general-purpose
   204   205   206   207   208   209   210   211   212   213   214