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.

Session 12—Applying the Class Diagram to the Case Study 135<br />

The next question is how to model the use of a pattern in the <strong>UML</strong>. The <strong>UML</strong> refers to a<br />

pattern as a collaboration (not to be confused with a Collaboration diagram). In Figure 12-2,<br />

the collaboration is rendered as an oval shape containing the pattern name. The classes are<br />

drawn around it. Then you draw dashed lines from the collaboration to the classes that<br />

implement the pattern. At the end of the dashed lines next to the classes, you place the<br />

role that the class plays in the implementation of the pattern. In Figure 12-2, the Shipment<br />

plays the role of observable and the Order plays the role of observer.<br />

Observer<br />

Pattern<br />

observable<br />

observer<br />

Shipment<br />

Figure 12-2<br />

is observed by<br />

0..* 1..1<br />

Observer pattern example in a Class diagram<br />

Order<br />

Using Design Patterns in the Class Diagram<br />

Another common problem in applications is managing complex state-specific behavior. If you<br />

haven’t worked much with states, that may sound complicated. But you may have worked<br />

around this problem without realizing it. Have you ever written a method in which you<br />

found you had to use a lot of conditional logic like if statements or case statements You<br />

couldn’t determine how the method should work until you first checked on the condition of<br />

various attributes in the object. Well, an object’s state is nothing more than the current values<br />

of its attributes. So when you checked the attribute values in the conditional logic, you<br />

were actually checking the object’s state.<br />

When the implementation of the methods depends on changes in the attribute values<br />

(state changes), the code can quickly become very complicated to write and difficult to<br />

maintain. The state design pattern offers a solution that both makes the initial writing<br />

easier and substantially reduces the maintenance costs.<br />

Table 12-3 describes the state design pattern.

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

Saved successfully!

Ooh no, something went wrong!