Page 206 -
P. 206

7.2   Design patterns  189



                                                                                    «interface»
                                                     «interface»                  Remote Control
                                                      Reporting
                                                                           startInstrument(instrument): iStatus
                                            weatherReport(WS-Ident): Wreport  stopInstrument(instrument): iStatus
                                            statusReport(WS-Ident): Sreport
                     Figure 7.9 Weather                                    collectData(instrument): iStatus
                                                                           provideData(instrument): string
                     station interfaces


                                       the services that are provided by the object or by a group of objects. Interfaces can be
                                       specified in the UML using the same notation as a class diagram. However, there is
                                       no attribute section and the UML stereotype ‹‹interface›› should be included in the
                                       name part. The semantics of the interface may be defined using the object constraint
                                       language (OCL). I explain this in Chapter 17, where I cover component-based soft-
                                       ware engineering. I also show an alternative way to represent interfaces in the UML.
                                         You should not include details of the data representation in an interface design,
                                       as attributes are not defined in an interface specification. However, you should
                                       include operations to access and update data. As the data representation is hidden, it
                                       can be easily changed without affecting the objects that use that data. This leads to
                                       a design that is inherently more maintainable. For example, an array representation
                                       of a stack may be changed to a list representation without affecting other objects
                                       that use the stack. By contrast, it often makes sense to expose the attributes in a
                                       static design model, as this is the most compact way of illustrating essential charac-
                                       teristics of the objects.
                                         There is not a simple 1:1 relationship between objects and interfaces. The same
                                       object may have several interfaces, each of which is a viewpoint on the methods that
                                       it provides. This is supported directly in Java, where interfaces are declared sepa-
                                       rately from objects and objects ‘implement’ interfaces. Equally, a group of objects
                                       may all be accessed through a single interface.
                                         Figure 7.9 shows two interfaces that may be defined for the weather station. The
                                       left-hand interface is a reporting interface that defines the operation names that are
                                       used to generate weather and status reports. These map directly to operations in the
                                       WeatherStation object. The remote control interface provides four operations, which
                                       map onto a single method in the WeatherStation object. In this case, the individual
                                       operations are encoded in the command string associated with the remoteControl
                                       method, shown in Figure 7.6.




                                 7.2 Design patterns


                                       Design patterns were derived from ideas put forward by Christopher Alexander
                                       (Alexander et al., 1977), who suggested that there were certain common patterns of
                                       building design that were inherently pleasing and effective. The pattern is a description
                                       of the problem and the essence of its solution, so that the solution may be reused in
   201   202   203   204   205   206   207   208   209   210   211