Docstoc

Lecture_10_-_Strategy

Document Sample
Lecture_10_-_Strategy Powered By Docstoc
					Software Design and Architecture
Behavioral Design Patterns


                             Muhammad Nasir
                               m.nasir@iiu.edu.pk
Strategy

   Intent
       Define a family of algorithms, encapsulate each
        one, and make them interchangeable
       Strategy lets the algorithm vary independently
        from clients that use it.
   Also Known As
        Policy
Strategy – Signaling System



The light-
switching policy
changes by the
hour


                              3
    Strategy
   The “dumb” policy: Change the green route
    every 15 seconds
   Mid-Night policy: Change to green whenever
    a “sensor” detects a vehicle
   Rush hour policy: Double the “green time” in
    the busy route
   Participants
      Strategy: Light Switching Policy

      Context: Time i.e. Midnight, Rush Hours
       etc


                                                   4
Strategy
   Structure
Strategy

   Applicability
    Use the Strategy pattern when…
     Strategies provide a way to configure a class
      with one of many behaviors
     You need different variants of algorithm

     An algorithms uses data that client shouldn’t
      know about.
Strategy
Participants
   Strategy (Light Switching Policy)
       Declares an interface common to all
        supported algorithms
   ConcreteStrategy (Rush-Hour Policy, Midnight Policy)
       Implements the algorithm using the Strategy
        interface
   Context (Time e.g. Rush Hour, Mid-Night )
       Is configured with a ConcreteStrategy
        object
       Maintains a reference to a Strategy object
Strategy
   Collaborations
       Strategy and Context interact to implement the
        chosen policy or algorithm.
       A context forwards requests to its strategy.
Strategy
   Sample Code
Strategy
    Strategy
   Consequences
       Advantages
           Helps in maintaining families of related algorithms.
           Easy to add new strategy (or remove an existing one,
            etc.)
           Strategies eliminate conditional statements.
           Strategies can provide different implementations of the
            same behavior
       Disadvantages
           Communication overhead between Strategy and
            Context
           Strategies increase the number of objects in an
            application
Strategy
   Related Patterns
    Strategy objects often make good flyweights.
The End

   Thanks for listening
   Questions would be appreciated…

				
DOCUMENT INFO
Shared By:
Stats:
views:2
posted:4/26/2012
language:
pages:13
Description: software design and architecture