Você está na página 1de 16

Strategy

Pa*ern and State Pa*ern

1 CSCI 3132 Summer 2011

Strategy Pa*ern Revisit


OO Design Principles Revisit
Program to an interface, not an implementa:on Favor object aggrega:on over class inheritance Find what varies and encapsulate it.

Strategy Pa*ern Revisit


A behavior pa*ern. 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 :me. The algorithm/behavior encapsulated in its own class is called a strategy.

Strategy Pa*ern - Class Diagram

Trade-os
Advantages Eliminates large condi:onal statements. Easier to keep track of dierent behavior because they are in dierent classes. A variety of implementa:ons for the same behavior. Disadvantages Increases the number of objects. All algorithms use the same interface.

The State Pa*ern


Another type of Behavioral pa*ern. Allows an object to alter its behavior when its internal state changes. The object will appear to change its class. Uses Polymorphism to dene dierent behaviors for dierent states of an object.
6

When to use STATE pa*ern ?


State pa*ern is useful when there is an object that can be in one of several states, with dierent behavior in each state. To simplify opera:ons that have large condi:onal statements that depend on the objects state.
if (myself = happy) then { eatIceCream(); . } else if (myself = sad) then { goToPub(); . } else if (myself = ecstaAc) then { .
7

Can we use a state-transi:on table?


Yes, but they are more complex to understand and implement. Upgrading func:onality of program is more dicult.

Benets of using STATE pa*ern


Localizes all behavior associated with a par3cular state into one object. New state and transi:ons can be added easily by dening new subclasses. Simplies maintenance. It makes state transi3ons explicit. Separate objects for separate states makes transi:on explicit rather than using internal data values to dene transi:ons in one combined object. State objects can be shared Context can share State objects if there are no instance variables.
9

Example I
water state variable
increaseTemp() decreaseTemp()

StateOfWater
increaseTemp() decreaseTemp()

WaterVapor Client
increaseTemp() increaseTemp() decreaseTemp()

LiquidWater
increaseTemp() decreaseTemp()

Ice
increaseTemp() decreaseTemp()

10

State Pa*ern
Context class: Represents the interface to the outside world. State abstract class: Base class which defines the different states of the state machine. Derived classes from the State class: Defines the true nature of the state that the state machine can be in. Context class maintains a pointer to the current state. To change the state of the state machine, the pointer needs to be changed.

11

Example II
MyMood state variable
doSomething()

MoodState

mad Client
doSomething()

angry

happy

doSomething()

doSomething()

doSomething()

12

Why Use A State Pa*ern?


Dynamically change an objects internal state during run:me. The state pa*ern creates a separate class for each condi:onal branch. This eliminates if/else or switch/case statements. Code is modular to allow easy crea:on of new states.

13

State Pa*ern - Class Diagram

14

Trade-os
Advantages
Encapsulates all behavior of a state into an object Eliminates massive lines of code involving if/else or switch/case statements with state transi:on logic State changes occur using one object, therefore, avoiding inconsistent states.

Disadvantages

Increased number of objects.

15

State v.s. Strategy


State pattern and Strategy pattern are very similar, but their of intent are different. - State object encapsulates a state-dependent behavior (and possibly state transitions) - Strategy object encapsulates an algorithm - Both examples of Composition with Delegation!

16

Você também pode gostar