designPatterns-04.pdf

Embed Size (px)

Citation preview

  • 7/29/2019 designPatterns-04.pdf

    1/16

    StrategyPa*ern

    and

    StatePa*ern

    CSCI 3132 Summer 2011

    1

  • 7/29/2019 designPatterns-04.pdf

    2/16

    StrategyPa*ernRevisit

    OODesignPrinciplesRevisitProgramtoaninterface,notanimplementa:onFavorobjectaggrega:onoverclassinheritanceFindwhatvariesandencapsulateit.

    2

  • 7/29/2019 designPatterns-04.pdf

    3/16

    StrategyPa*ernRevisit

    Abehaviorpa*ern. Takesanalgorithmfromitshostandencapsulatesitintoaseparateclass.

    Anobjectanditsbehaviorareseparatedandputintotwoclasses.

    Thisallowsthealgorithmtobeswitchedatany:me. Thealgorithm/behaviorencapsulatedinitsownclassiscalledastrategy.

    3

  • 7/29/2019 designPatterns-04.pdf

    4/16

    StrategyPa*ern-ClassDiagram

    4

  • 7/29/2019 designPatterns-04.pdf

    5/16

    Trade-offs

    Advantages Eliminateslargecondi:onal

    statements.

    Easiertokeeptrackofdifferentbehaviorbecause

    theyareindifferentclasses.

    Avarietyofimplementa:onsforthe

    samebehavior.

    Disadvantages Increasesthenumberof

    objects.

    Allalgorithmsusethesameinterface.

    5

  • 7/29/2019 designPatterns-04.pdf

    6/16

    TheStatePa*ern

    AnothertypeofBehavioralpa*ern.Allowsanobjecttoalteritsbehaviorwhenitsinternalstatechanges.Theobjectwillappearto

    changeitsclass.

    UsesPolymorphismtodefinedifferentbehaviorsfordifferentstatesofanobject.

    6

  • 7/29/2019 designPatterns-04.pdf

    7/16

    WhentouseSTATEpa*ern?

    Statepa*ernisusefulwhenthereisanobjectthatcanbeinoneofseveralstates,withdifferentbehaviorineachstate.

    Tosimplifyopera:onsthathavelargecondi:onalstatementsthatdependontheobjectsstate.

    if(myself=happy)then

    {

    eatIceCream();

    .

    }elseif(myself=sad)then

    {

    goToPub();

    .

    }

    elseif(myself=ecstaAc)then

    {

    .

    7

  • 7/29/2019 designPatterns-04.pdf

    8/16

    Canweuseastate-transi:ontable?

    Yes,buttheyaremorecomplextounderstandandimplement.

    Upgradingfunc:onalityofprogramismoredifficult.

    8

  • 7/29/2019 designPatterns-04.pdf

    9/16

    BenefitsofusingSTATEpa*ern

    Localizesallbehaviorassociatedwithapar3cularstateintooneobject. Newstateandtransi:onscanbeaddedeasilybydefiningnewsubclasses.

    Simplifiesmaintenance. Itmakesstatetransi3onsexplicit.Separateobjectsforseparatestatesmakestransi:onexplicit

    ratherthanusinginternaldatavaluestodefinetransi:onsin

    onecombinedobject.

    StateobjectscanbesharedContextcanshareStateobjectsiftherearenoinstance

    variables.

    9

  • 7/29/2019 designPatterns-04.pdf

    10/16

    ExampleI

    water StateOfWater

    WaterVapor LiquidWater Ice

    increaseTemp()decreaseTemp()

    state variable

    ClientincreaseTemp()

    increaseTemp()decreaseTemp()

    increaseTemp()decreaseTemp()

    increaseTemp()

    decreaseTemp()

    increaseTemp()decreaseTemp()

    10

  • 7/29/2019 designPatterns-04.pdf

    11/16

    StatePa*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 changethe state of the state machine, the pointer needs to be changed.

    11

  • 7/29/2019 designPatterns-04.pdf

    12/16

    ExampleII

    MyMood MoodState

    doSomething()

    mad angry happy

    doSomething() doSomething() doSomething()

    state variable

    ClientdoSomething()

    12

  • 7/29/2019 designPatterns-04.pdf

    13/16

    WhyUseAStatePa*ern?

    Dynamicallychangeanobjectsinternalstateduringrun:me. Thestatepa*erncreatesaseparateclassforeachcondi:onal

    branch.Thiseliminatesif/elseorswitch/casestatements.

    Codeismodulartoalloweasycrea:onofnewstates.

    13

  • 7/29/2019 designPatterns-04.pdf

    14/16

    StatePa*ern-ClassDiagram

    14

  • 7/29/2019 designPatterns-04.pdf

    15/16

    Trade-offs

    Advantages Encapsulatesallbehaviorofa

    stateintoanobject

    Eliminatesmassivelinesofcodeinvolvingif/elseorswitch/casestatementswithstatetransi:onlogic

    Statechangesoccurusingoneobject,therefore,avoidinginconsistentstates.

    Disadvantages Increasednumberofobjects.

    15

  • 7/29/2019 designPatterns-04.pdf

    16/16

    Statev.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