Design Pattern with Java IV Lecture 12-14 State Design Pattern and - - PowerPoint PPT Presentation

design pattern with java iv
SMART_READER_LITE
LIVE PREVIEW

Design Pattern with Java IV Lecture 12-14 State Design Pattern and - - PowerPoint PPT Presentation

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,


slide-1
SLIDE 1

Design Pattern with Java IV

Lecture 12-14 State Design Pattern and Application to Mutable Linear Recursive Structure Framework

1

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

UML Class Diagram for the State Design Pattern

4

slide-5
SLIDE 5

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

slide-6
SLIDE 6

6

UML class diagram of the LRStruct

slide-7
SLIDE 7

The intrinsic structural behavior of an LRStruct.

  • The public constructor:

– LRStruct()

  • and the methods:

– insertFront(...) – removeFront(...) – getFirst() – setFirst(...) – getRest() – setRest(...),

7

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Client Code

  • To perform an algorithm on an LRStruct , the

client must "ask" the LRStruct to "execute" the algorithm

10

slide-11
SLIDE 11

11

slide-12
SLIDE 12

12

slide-13
SLIDE 13

IMPLEMENTATION OF LINEAR RECURSIVE STRUCTURE FRAMEWORK

13

slide-14
SLIDE 14

14

UML class diagram of the LRStruct

slide-15
SLIDE 15

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

slide-16
SLIDE 16

16

LRStruct.java

slide-17
SLIDE 17

17

slide-18
SLIDE 18

18

slide-19
SLIDE 19

The States

19

slide-20
SLIDE 20

20

slide-21
SLIDE 21

21

slide-22
SLIDE 22

Example: Series of object diagrams

22

slide-23
SLIDE 23

23

slide-24
SLIDE 24

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

slide-25
SLIDE 25

25