DOC PREVIEW
UMD CMSC 433 - State pattern

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

UMD CMSC 433 - State pattern

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download State pattern
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view State pattern and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view State pattern 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?