01.01.2015 Views

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

134<br />

Saturday Afternoon<br />

designed to help developers communicate common solutions to common problems. In fact,<br />

because communication and problem solving are parts of nearly every occupation, you can<br />

find patterns in almost every discipline of software development. There are analysis level<br />

patterns, business patterns, architectural patterns, and many more, all developed to facilitate<br />

communication about solutions that are used consistently to solve similar recurring<br />

problems in each area of endeavor.<br />

One software design pattern, called the observer design pattern, addresses the problem<br />

where one object needs to know when something happens to another object so that it<br />

can respond immediately to the change. The example in Table 12-2 uses the Order and the<br />

Shipment from the Inventory Control case study to illustrate the pattern. Using the four<br />

elements from Table 12-1 as a guide, Table 12-2 describes how the observer design pattern<br />

allows you to immediately update the Order when the Shipment leaves the building.<br />

Table 12-2<br />

The Observer Design Pattern Example<br />

Pattern Element<br />

A problem to solve<br />

The resources to solve it<br />

The set of rules about how<br />

the resources could be<br />

used to solve the problem<br />

Guidelines to know when<br />

the pattern works well<br />

and when it does not<br />

Element Description for Observer Pattern<br />

The Order needs to be updated to a status of “shipped” when<br />

the Shipment has left the premises. The “shipped” status is the<br />

condition that kicks off the Accounts Receivable and tracking<br />

processes.<br />

The observer pattern identifies two resources, an observer<br />

object and an observable (observed) object. The observable<br />

object is responsible for notifying the observers. This eliminates<br />

the need for the observer to constantly ask the observable<br />

(observed) object what is happening.<br />

The rules for the observer pattern dictate two roles: The<br />

Shipment object that is being observed assumes the role of<br />

the observable object, and the Order takes on the role of the<br />

observer. The Order tells the Shipment object that it wants to<br />

be notified when something happens. The Shipment places the<br />

Order on its list of observers to be notified. When the<br />

Shipment is shipped, the Shipment sends a standard notification<br />

to all the observers in the list. When the Order receives<br />

the notification, it can then respond as it needs to.<br />

The observer pattern can be overused, causing performance<br />

problems that result from the volume of notifications sent to<br />

all observers for every change in the observable object. It<br />

works best when a few very specific immediate responses are<br />

essential to the application, as in a user interface, embedded<br />

application, or automation.

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

Saved successfully!

Ooh no, something went wrong!