1 / 16

Strategy Pattern

Strategy Pattern. Lab 5 retrospective Base fish provided Specialized fish were modeled as subclasses of the base fish This works!. Combinations of behaviors. How is a fish with a combination of behaviors handled? Not so well…. Two behaviors. Can subclass an existing fish.

emera
Download Presentation

Strategy Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strategy Pattern • Lab 5 retrospective • Base fish provided • Specialized fish were modeled as subclasses of the base fish • This works! CSE 115/503 Introduction to Computer Science I

  2. Combinations of behaviors • How is a fish with a combination of behaviors handled? • Not so well… CSE 115/503 Introduction to Computer Science I

  3. Two behaviors • Can subclass an existing fish. • Suppose we want a dizzy chameleon fish: with single-inheritance, our new fish can derive only from one existing fish. • Which one? CSE 115/503 Introduction to Computer Science I

  4. Two choices CSE 115/503 Introduction to Computer Science I

  5. Which one? • Either choice is equally bad: we must duplicate code of other’s update method to get both behaviors. public void update() { super.update(); setRotation(getRotation() + _rotationSpeed); } public void update() { super.update(); setColor(Utilities.randomColor()); } CSE 115/503 Introduction to Computer Science I

  6. Combinations of more than two? • Suppose we have N total individual behaviors (like Chameleon, Dizzy, etc.) • Consider the basic fish to have a null behavior. There is one of these. • There are N immediate subclasses of Fish, that have one added behavior. CSE 115/503 Introduction to Computer Science I

  7. 1 (non-null) behavior Total number of fish types is 2: • 1 (with a null behavior) • 1 (with a non-null behavior) CSE 115/503 Introduction to Computer Science I

  8. 2 (non-null) behaviors Total number of fish types is 4: • 1 (with a null behavior) • 2 (with a non-null behavior) • 1 (with both behaviors) CSE 115/503 Introduction to Computer Science I

  9. 3 (non-null) behaviors Total number of fish types is 8: • 1 (with a null behavior) • 3 (with a non-null behavior) • 3 (with a pair of behaviors) • 1 (with all three behaviors) CSE 115/503 Introduction to Computer Science I

  10. Another view of data 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 … CSE 115/503 Introduction to Computer Science I

  11. Pascal’s triangle 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 … 1 2 3 4 5 2 4 8 16 32 N Sum =2N CSE 115/503 Introduction to Computer Science I

  12. So how bad is this approach? • The sum represents the number of distinct classes you need to define to accommodate all possible combinations of behaviors that are possible. • Imagine adding one more behavior to the system: • this doubles the number of classes that need to be defined (e.g. from 32 to 64, or from 64 to 128) • requires compilation of all these classes • all possible combinations must be formed at compile time (statically), whether they’re needed or not • This quickly becomes intractable! CSE 115/503 Introduction to Computer Science I

  13. Strategy pattern • What’s the basic problem? • We’re stuck because behaviors are expressed as a few lines of code inside the class definition of a particular type of fish. • These concepts are too tightly coupled. • Why not model a behavior using an object? • This lets us decouple behavior specification from fish specification. • This is the basic insight of the Strategy Pattern. CSE 115/503 Introduction to Computer Science I

  14. Strategy Pattern Fish specification and behavior specifications are decoupled. CSE 115/503 Introduction to Computer Science I

  15. Combining behaviors • One way is to make use of the Composite Pattern: Composite lets us combine behaviors dynamically! CSE 115/503 Introduction to Computer Science I

  16. Consequences • Behaviors are defined once, independently of fish. • For N behaviors, only N+1 classes must be defined (one for each behavior, one for the composite). • Combinations of behaviors are not determined statically. • Only those combinations actually needed at runtime are formed, dynamically. CSE 115/503 Introduction to Computer Science I

More Related