190 likes | 262 Views
Learn how to implement the strategy pattern successfully for code efficiency. Understand the benefits of separating algorithms, achieving interchangeability, and adapting strategies dynamically. Discover how composition over inheritance can optimize your programming approach.
E N D
CSC 313 – Advanced Programming Topics Lecture 5:Strategy Pattern –or–a Knife At a Gunfight
Bested by Lilliputians quack behavior fly behavior
Bested by Lilliputians quack behavior fly behavior • Lot of short ones beats a big, tall one
Implementing a Strategy public interface FlyBehavior {public void fly(); } public class UseJets implements FlyBehavior {public void fly() {System.out.println(“Vrooom”);} } public class Wings implements FlyBehavior {public void fly() {System.out.println(“Band On the Run”);} }
Implementing a Strategy public interface FlyBehavior {public void fly(); } public class UseJets implements FlyBehavior {public void fly() {System.out.println(“Vrooom”);} } public class Wings implements FlyBehavior {public void fly() {System.out.println(“Band On the Run”);} }
Using a Strategy public interface FlyBehavior {public void fly(); } public abstractclass Duck {private FlyBehaviorflyBehavior;public Duck(FlyBehaviorf) { flyBehavior = f; }public void fly() { flyBehavior.fly(); } } public class PaulMcCartney extends Duck {public PaulMcCartney() { super(new Wings()); } } public class DecoyDuck extends Duck {public DecoyDuck() { super(newNoFly()); } }
Strategy Pattern • Define supertype for family of algorithms • interface used nearly always for this • Entire family relies on method signature specified • Make algorithms interchangeable • Separate class encapsulates each algorithm • Algorithm definition split from its use • Add algorithms easily with no changes to code • New environments can also reuse algorithms
Zen & the Art of Programming Identify and isolate what will change from what stays the same • What is being done split from how to do it Program to a concept, not a class • Client cannot know what Strategy does • Strategy limited to parameters not fields*
Changing your Strategy • Strategy fixed only when client calls it • Determined by value of field when the call is made • Set & change strategies at any time • Client does not know implementation to rely on it • Simply reassign field whenever it should change • All the Client subclasses may not be needed • If only Strategy differs, just change field’s value • At worst, subclasses limited to a constructor
Reusing a Strategy public class JetFighter {private FlyBehaviorflyBehavior;public JetFighter() {flyBehavior = new UseJets();}public void fly() { flyBehavior.fly();} }
Reusing a Strategy public class JetFighter {private FlyBehaviorflyBehavior;public JetFighter() {flyBehavior = new UseJets();}public void fly() {flyBehavior.fly();} } Thanks to the Strategy Pattern, I had more time for Skyrim!
Zen & Art of Programming Favor composition over inheritance • Composition is far easier to spell
Tools: Are They Worth It? • Good in correct environments & for correct uses
Tools: Are They Worth It? • Good in correct environments & for correct uses • Become an abomination in other situations
Making Tools Work • Use inheritance… • …to have class expand concepts in superclass • …not add or modify behavior that is inherited • Use Strategy Pattern… • …for single concept with multiple behaviors • …to enable behavior reuse in later systems • …when you can separate strategy from context
Zen & the Art of Programming Favor composition over inheritance • Inheritance has problems • Specify when written;cannot change dynamically • Limits use & re-useof classes to original concept • Makes adding & mixing functionality difficult • Composition also imperfect • Can make debugging more difficult • Requires more classes & interfaces to work • With composition, optimizations may not occur
For Next Lecture • Read notes on compilers via Angel link • Understanding text requires taking what steps? • Lexical analysis & parsing play what role? • How do we store programs being compiled? • Was that a design pattern? How is it used? • Given a compiler, where do optimizations occur?