1CMSC 433, Fall 200251State 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 patternCMSC 433, Fall 200252State 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 object2CMSC 433, Fall 200253Structure of State patternCMSC 433, Fall 200254Instance of State Pattern3CMSC 433, Fall 200255State pattern notes•Can use singletons for instances of each state class–State objects don’t encapsulate (mutable) state, so can be shared•Easy to add new states–New states can extend the base class, or–New states can extend other states•Override only selected functionsCMSC 433, Fall 200256Example –Finite State Machineclass FSM {State state;public FSM(State s) { state = s; }public void move(char c) { state = state.move(c); }public booleanaccept() { return state.accept();}}public interface State {State move(char c);booleanaccept();}4CMSC 433, Fall 200257FSM 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 booleanaccept() {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 booleanaccept() {return true;}}CMSC 433, Fall 200258Decorator Pattern•Motivation–Want to add responsibilities/capabilities to individual objects, not to an entire class.–Inheritance requires a compile-time choice of parent class.•Solution–Enclose the component in another object that adds the responsibility/capability•The enclosing object is called a decorator.5CMSC 433, Fall 200259Decorator Pattern: Features•A decorator conforms to the interface of the component it decorates–so that its presence is transparent to the component's clients. •A decorator forwards requests to its encapsulated 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 200260Structure6CMSC 433, Fall 200261Decorator Pattern: ExampleCMSC 433, Fall 200262Decorator Pattern Analysis•Advantages–fewer classes than with static inheritance–dynamic addition/removal of decorators–keeps root classes simple•Disadvantages–proliferation of run-time instances–abstract Decorator must provide common interface• Tradeoffs:–useful when components are lightweight–otherwise use Strategy7CMSC 433, Fall 200263Interaction diagramCMSC 433, Fall 200264Example: Java I/OFileReader frdr= newFileReader(filename);LineNumberReader lrdr= newLineNumberReader(frdr);String line;while ((line =lrdr.readLine()) != null) {System.out.print(lrdr.getLineNumber() + ":\t" + line);}This document was created with Win2PDF available at http://www.daneprairie.com.The unregistered version of Win2PDF is for evaluation or non-commercial use
View Full Document