SDSU CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2002
Strategy
Previous    Lecture Notes Index    Next    
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 26-Feb-02

References

Design Patterns: Elements of Reusable Object-Oriented Software , Gamma, Helm, Johnson, Vlissides, Addison-Wesley, 1995, pp. 315-314

The Design Patterns Smalltalk Companion, Alpert, Brown, Woolf, 1998, pp 339-354


Doc 8, Strategy Slide # 2

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

Structure




Doc 8, Strategy Slide # 3
Examples

Java Layout Managers for Windows

Java Comparators

Smalltalk sort blocks

Java Layout Managers


import java.awt.*;
class  FlowExample  extends Frame  {
   
   public FlowExample( int  width, int height ) {
      setTitle( "Flow Example" );
      setSize( width, height );
      setLayout( new FlowLayout( FlowLayout.LEFT) );
      
      for ( int label = 1; label < 10; label++ )
         add( new Button( String.valueOf( label ) ) );
      show();
   }
   
   public  static  void  main( String  args[] ) {
      new  FlowExample( 175, 100 );
      new  FlowExample( 175, 100 );
   }
}

Doc 8, Strategy Slide # 4
Why Not use Inheritance?




But there are:


So using inheritance would require 780 classes!

Doc 8, Strategy Slide # 5
Java Comparators

import java.util. Comparator;
import java.util.*;
   
class Student  {
   String name;
   
   public Student( String newName) { name = newName;}
   
   public String toString() { return name; }
   }
   
final class StudentNameComparator implements Comparator {
   
   public int compare( Object leftOp, Object rightOp ) {
      String leftName = ((Student) leftOp).name;
      String rightName = ((Student) rightOp).name;
      return leftName.compareTo( rightName );
   }
   
   public boolean equals( Object comparator ) {
      return comparator instanceof StudentNameComparator;
   }
}
   
public class Test  {
   public static void main(String args[])  {
      Student[] cs596 = { new Student( "Li" ), new Student( "Swen" ), 
          new Student( "Chan" ) };
      //Sort the array
      Arrays.sort( cs596, new StudentNameComparator() );
   }
}

Doc 8, Strategy Slide # 6
Smalltalk SortBlocks

| list |
list := #( 1 6 2 3 9 5 ) asSortedCollection.
Transcript 
   print: list;
   cr.
list sortBlock: [:x :y | x > y].
Transcript 
   print: list;
   cr;
   flush.

Doc 8, Strategy Slide # 7
Why Not use Inheritance


There are arbitrarily many ways to sort

So get arbitrarily many


But with comparators (blocks) one can:





Doc 8, Strategy Slide # 8

Applicability


Use the Strategy pattern when






Doc 8, Strategy Slide # 9

Consequences




What is the big deal? You still subclass Strategy!


Replace in Context code like:

      switch  ( flag ) {
         case A: doA(); break;
         case B: doB(); break;
         case C: doC(); break;
      }

With code like:

      strategy.do();



   SortedList studentRecords = new SortedList(new ShellSort());




Doc 8, Strategy Slide # 10

Implementation



How does data flow between them

Context pass data to Strategy

Strategy has point to Context, gets data from Context

In Java use inner classes


Can be used if Strategy can be selected at compile-time and does not change at runtime

   SortedList<ShellSort> studentRecords;


Give Context default behavior

If default used no need to create Strategy object



Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 26-Feb-02    Next