Design Pattern with Java IV Lecture 12-14 State Design Pattern and Application to Mutable Linear Recursive Structure Framework 1
State Pattern and Dynamic Reclassification • Object, an instance of container structures – can store, retrieve, and remove data • A mutable container structure is a system that may change its state. 2
Dynamic Reclassification • For each distinct state, the algorithms to implement the methods differ. • For example, retrieve method – empty state -it simply returns null – non-empty state- it is more complicated 3
UML Class Diagram for the State Design Pattern 4
Mutable Linear Recursive Structure • A mutable linear recursive structure (LRStruct) can be in the empty state or in a non-empty state. – empty, it contains no object – Non-empty, it contains an object called first , and a LRStruct object called rest . 5
6 UML class diagram of the LRStruct
The intrinsic structural behavior of an LRStruct. • The public constructor: – LRStruct() • and the methods: – insertFront(...) – removeFront(...) – getFirst() – setFirst(...) – getRest() – setRest(...), 7
The Extrinsic behaviors • The method, – Object execute(IAlgo algo, Object... inp), is called the extensibility "hook". • It allows the client to add an open-ended number of new application-dependent behaviors to the data structure LRStruct, – Computing its length – Merging one LRStruct with another 8
Adding a new operation on an LRStruct • The client writes appropriate concrete classes that implements IAlgo . For example, • With some concrete code for emptyCase(...) and some concrete code for nonEmptyCase(... 9
Client Code • To perform an algorithm on an LRStruct , the client must "ask" the LRStruct to "execute" the algorithm 10
11
12
IMPLEMENTATION OF LINEAR RECURSIVE STRUCTURE FRAMEWORK 13
14 UML class diagram of the LRStruct
The Structure • LRStruct is the invariant part of the system. It remains the same for all applications using LRStruct. • The variant part of the system is the behaviors that are abstractly encapsulated in an interface called IAlgo. 15
LRStruct.java 16
17
18
The States 19
20
21
Example: Series of object diagrams 22
23
The Visitor • Algorithms on LRStruct are written as IAlgo visitors • The following slides show an algorithm to remove the last element from an LRStruct. 24
25
Recommend
More recommend