State and Strategy Pattern by rt3463df

VIEWS: 15 PAGES: 26

									State and Strategy Pattern

        By: Luke Robak
        Brandon Liang
        Presentation Overview
Part 1              Part 2
State Pattern       Strategy Pattern
Need of a State     Demand for
Mission Statement    Strategy
Uses                Strategic Plan
                     Uses
                           Problem
   We have 4 states: blue, black, green and red.
   Two functions push() and pull() changes the current state.
   Blue state: push -> green state, pull -> red state
   Black state: push -> red state, pull -> black state
   Green state: push -> black state, pull -> blue state
   Red state: push -> blue state, pull -> green state

   What are the common programming structures to solve this
    problem?
             If/Else Statements
             Switch/Case Statements
Alternative Solution
      State Design
    Why Use A State Pattern?
 Dynamically change an objects internal state
  during runtime.
 The state pattern creates a separate class for
  each conditional branch. This eliminates if/else or
  switch/case statements.
 Code is modular to allow easy creation of new
  states.
State Pattern - Class Diagram
                    Trade-offs
Advantages                     Disadvantages
 Encapsulates all behavior      Increased number of
  of a state into an object       objects.
 Eliminates massive lines of
  code involving if/else or
  switch/case statements
  with state transition logic
 State changes occur using
  one object, therefore,
  avoiding inconsistent
  states.
                Back to the Problem
   Implementation 1: No State Pattern
   public class ContextNoSP {
      // The state!
      private Color state = null;
      // Creates a new ContextNoSP with the specified state (color).
      public ContextNoSP(Color color) {state = color;}
      // Creates a new ContextNoSP with the default state
      public ContextNoSP() {this(Color.red);}
      // Returns the state.
      public Color getState() {return state;}
      // Sets the state.

        public void setState(Color state) {this.state = state;}
     Implementation 1 Continued
   public void push() {
      if (state == Color.red) state = Color.blue;
      else if (state == Color.green) state = Color.black;
      else if (state == Color.black) state = Color.red;
      else if (state == Color.blue) state = Color.green;
      }
     public void pull() {
      if (state == Color.red) state = Color.green;
      else if (state == Color.green) state = Color.blue;
      else if (state == Color.black) state = Color.green;
      else if (state == Color.blue) state = Color.red;
      }    }
           State Pattern Solution
   Implementation 2: State Pattern
   public abstract class State {
      public abstract void handlePush(Context c);
      public abstract void handlePull(Context c);
      public abstract Color getColor();
     }
     public class BlackState extends State {
        // Next state for the Black state:
        // On a push(), go to "red"
        // On a pull(), go to "green"
        public void handlePush(Context c) {c.setState(new RedState());}
        public void handlePull(Context c) {c.setState(new GreenState());}
        public Color getColor() {return (Color.black);} }
     Implementation 2 Continued
   public class Context {
      // The contained state.
      private State state = null; // State attribute
      // Creates a new Context with the specified state.
      public Context(State state) {this.state = state;}
      // Creates a new Context with the default state.
      public Context() {this(new RedState());}
      // Returns the state.
      public State getState() {return state;}
      // Sets the state.
      public void setState(State state) {this.state = state;}
      public void push() {state.handlePush(this);}
      public void pull() {state.handlePull(this);}
     }
               Questions
Who   creates the transition states?
  Incorporated into the ConcreteState classes
  Context Classes for simple cases
  When are ConcreteState Objects
             Created?
Create  when they are needed.
Create once and have the context
 class reference it when they are
 needed.
 Can we use a state-transition
            table?
Yes, but they are more complex to
 understand and implement.
Upgrading functionality of program is
 more difficult.
          Strategy Pattern
Takes  an algorithm from its host and
 encapsulates it into a separate class.
An object and its behavior are separated
 and put into two classes.
This allows the algorithm to be switched at
 any time.
The algorithm/behavior encapsulated in its
 own class is called a strategy.
Strategy Pattern - Class
       Diagram
      Demand for Strategy
When  would you want to use a
 strategy pattern?
 When you have several objects that are the same
 and only differ in behavior.
                    Trade-offs
Advantages                      Disadvantages
 Eliminates large conditional    Increases the number of
  statements.                      objects.
 Easier to keep track of         All algorithms use the same
  different behavior because       interface.
  they are in different
  classes.
 A variety of
  implementations for the
  same behavior.
 Example of Strategy Pattern
You  have an array and at run-time
 you want to decide which sort
 algorithm to use.
Encapsulate each different sort
 algorithm using the strategy pattern.
Class Diagram of Sort Example
             Example 2
Many   different layout strategies
 exist. A GUI container wants to
 decide at run-time which layout to
 use.
Encapsulate each different layout
 algorithm using the strategy pattern.
Class Diagram of Layout
        Example
 Summary of Strategy Pattern
Many   related classes differ in behavior
Need to use the same algorithm with a
 slight variation
Hides complex, algorithm-specific data
 structures from the client
Eliminate conditional branches and put
 them in their own separate strategy class
            Exercise 1
There are a lot of similarities
 between state and strategy patterns.
 What is the difference between the
 two?
            Exercise 2
Given  the java classes available
 online, add a new department to the
 program (logistics), which has two sub
 states, shipments, deliveries.
             Exercise 3
Homework  for
 reading week!!
            HAVE FUN!!!

								
To top