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

Create successful ePaper yourself

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

136<br />

Saturday Afternoon<br />

Table 12-3<br />

The State 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 State Pattern<br />

You need to create a Product class for the Inventory Control<br />

System. The Product will be used in a number of different<br />

steps within the system. Depending on where a Product is in<br />

the workflow, each method on the Product may or may not be<br />

valid. Also, some methods might need to behave differently<br />

during different steps in the workflow.<br />

The state design pattern has two resources, in this example<br />

they are the Product, which provides the context, and the<br />

Product State (state), which defines the state-specific implementations<br />

for each method. Figure 12-3 illustrates the Class<br />

diagram for this relationship.<br />

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

context, which is provided by the Product class, and the state,<br />

which is provided by the Product State class. The Product State<br />

is actually a generalization of classes that define the individual<br />

states for Product and their state-specific implementations<br />

for each operation.<br />

The Product receives requests in the course of normal business.<br />

But rather than implement the requested operations, it delegates<br />

or forwards each request to an instance of a ProductState<br />

subclass that represents the current state of the Product. The<br />

ProductState instance performs the implementation that it<br />

owns for the operation and returns control to the Product. As<br />

the state of the Product changes, each new request is actually<br />

forwarded to an instance of a different ProductState subclass<br />

that represents the current condition of the Product.<br />

Many objects simply don’t have a significant number of states,<br />

or their behavior doesn’t vary significantly from state to state.<br />

Figure 12-3 shows the association “delegates to” between Product and ProductState. If<br />

that were all the Class diagram showed you, then you wouldn’t really be able to tell why<br />

they were associated. After all, Products are also associated with LineItems and Locations.<br />

The association name may give you a clue, but when you add the pattern notation, you<br />

immediately bring in all the information about how the pattern tells you to use these two<br />

classes in this relationship. The pattern notation explains their communication, the methods<br />

they need to support, and the attributes they will need in order to support the relationship<br />

and the communication.

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

Saved successfully!

Ooh no, something went wrong!