21.01.2022 Views

Sommerville-Software-Engineering-10ed

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7.2 ■ Design patterns 211

C

D

B

A

50

25

0

A B C D

Subject

Figure 7.11 Multiple

displays

Observer 1

A: 40

B: 25

C: 15

D: 20

Observer 2

four essential description elements and also include a brief statement of what the

pattern can do. This pattern can be used in situations where different presentations of

an object’s state are required. It separates the object that must be displayed from the

different forms of presentation. This is illustrated in Figure 7.11, which shows two

different graphical presentations of the same dataset.

Graphical representations are normally used to illustrate the object classes in

patterns and their relationships. These supplement the pattern description and add

detail to the solution description. Figure 7.12 is the representation in UML of the

Observer 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).

Figure 7.12 A UML

model of the

Observer pattern

2. Tidy up the interfaces to a number of related objects that have often been developed

incrementally (Façade pattern).

Subject

Observer

Attach (Observer)

Detach (Observer)

Notify ()

for all o in observers

o -> Update ()

Update ()

ConcreteSubject

GetState ()

return subjectState

ConcreteObserver

Update ()

observerState =

subject -> GetState ()

subjectState

observerState

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!