Docsity

Study of State and Decorator Design Patterns in OOP: FSM and Java I/O - Prof. Adam A. Port, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433, Fall 2002
50
State pattern
•
Suppose an object is always in one of several
known states
•
The state an object is in determines the behavior
of several methods
•
Could use if/case statements in each method
•
Better solution: state pattern
CMSC 433, Fall 2002
51
State pattern
•
Have a reference to a state object
–
Normally, state object doesn’t contain any fields
–
Change state: change state object
–
Methods delegate to state object
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Study of State and Decorator Design Patterns in OOP: FSM and Java I/O - Prof. Adam A. Port and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Fall 2002 50

State pattern

• Suppose an object is always in one of several

known states

• The state an object is in determines the behavior

of several methods

• Could use if/case statements in each method

• Better solution: state pattern

CMSC 433, Fall 2002 51

State pattern

• Have a reference to a state object

– Normally, state object doesn’t contain any fields

– Change state: change state object

– Methods delegate to state object

CMSC 433, Fall 2002 52

Structure of State pattern

CMSC 433, Fall 2002 53

Instance of State Pattern

CMSC 433, Fall 2002 56

FSM Example – cont.

class State1 implements State { static State1 instance = new State1(); private State1() {} public State move (char c) { switch (c) { case 'a': return State2.instance; case 'b': return State1.instance; default: throw new IllegalArgumentException(); } } public boolean accept() {return false;} }

class State2 implements State { static State2 instance = new State2(); private State2() {} public State move (char c) { switch (c) { case 'a': return State1.instance; case 'b': return State1.instance; default: throw new IllegalArgumentException(); } } public boolean accept() {return true;} }

CMSC 433, Fall 2002 57

Decorator Pattern

  • Motivation
    • Want to add responsibilities to individual objects, not to an entire class
    • Inheritance requires a compile-time choice
  • Solution
    • Enclose the component in another object that adds the responsibility
      • The enclosing object is called a decorator.
    • Decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients.
    • Decorator forwards requests to the component and may perform additional actions before or after forwarding.
    • Can nest decorators recursively, allowing unlimited added responsibilities.
    • Can add/remove responsibilities dynamically

CMSC 433, Fall 2002 58

Structure

CMSC 433, Fall 2002 59

Example: Java I/O

FileReader frdr = new FileReader(filename); LineNumberReader lrdr = new LineNumberReader(frdr); String line; while ((line = lrdr.readLine()) != null) { System.out.print(lrdr.getLineNumber() + ":\t" + line); }

CMSC 433, Fall 2002 62

Design Issues

• Representation and manipulation of document

• Formatting a document

• Adding scroll bars and borders to Lexi windows

• Support multiple look-and-feel standards

• Handle multiple windowing systems

• Support user operations

• Advanced features

– spell-checking and hyphenation

CMSC 433, Fall 2002 63

Structure of a Lexi Document

• Goals:

– store text and graphics in document

– generate visual display

– maintain info about location of display elements

• Caveats:

– treat different objects uniformly

  • e.g., text, pictures, graphics

– treat individual objects and groups of objects uniformly

  • e.g., characters and lines of text

CMSC 433, Fall 2002 64

Structure of a Lexi Document

• Solution:

– define abstract class Glyph for all displayed objects

– glyph responsibilities:

  • know how to draw itself
  • knows what space it occupies
  • knows its children and parent

– use recursive composition for defining and handling

complex objects

– define composition of Glyph as instances of Glyph

CMSC 433, Fall 2002 65

Glyph Class Diagram

CMSC 433, Fall 2002 68

Composite Pattern (cont’d)

• Consequences:

– class hierarchy has both simple and composite objects

– simplifies clients

– aids extensibility

  • clients do not have to be modified

– too general a pattern?

  • difficult to to restrict functionality of concrete leaf subclasses

CMSC 433, Fall 2002 69

Formatting Lexi Documents

• Handle justification, margins, line breaking, etc.

• Many good algorithms exist;

  • different tradeoffs between quality and speed
  • design decision: implement different algorithms, decide at run-time which algorithm to use

• Goal: maintain orthogonality between formatting and

representation (glyphs)

• Solution

  • define root class that supports many algorithms
  • each algorithm implemented in a subclass

CMSC 433, Fall 2002 70

Compositor and Composition

• Relevant design decisions:

– compositor: class containing formatting algorithm

– pass objects to be formatted as parameters to

compositor methods

– parameters are instances of a Glyph subclass called

Composition

– uniform interface between formattable objects and

compositor algorithms

CMSC 433, Fall 2002 71

Compositor and Composition

• Relevant design decisions (cont’d):

– each Composition instance has a reference to a

compositor object

– when a composition needs to format itself, it sends a

message to its compositor instance

CMSC 433, Fall 2002 74

Strategy Pattern: Structure

CMSC 433, Fall 2002 75

Strategy Pattern (cont’d)

• Consequences

– clear separation of algorithm definition and use

  • glyphs and formatting algorithms are independent
  • alternative (many subclasses) is unappealing
    • proliferation of classes
    • algorithms cannot be changed dynamically

– elimination of conditional statements

  • as usual with OO programming

CMSC 433, Fall 2002 76

Strategy Pattern (cont’d)

• Consequences (continued)

– clients must be aware of different strategies

  • when initializing objects

– proliferation of instances at run-time

  • each glyph has a strategy object with formatting information
  • if strategy is stateless, share strategy objects