Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2005CSE 115/503 Introduction to Computer Science I1 Strategy Pattern Lab 5 retrospective –Base fish provided –Specialized fish were modeled as subclasses.

Similar presentations


Presentation on theme: "Fall 2005CSE 115/503 Introduction to Computer Science I1 Strategy Pattern Lab 5 retrospective –Base fish provided –Specialized fish were modeled as subclasses."— Presentation transcript:

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

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

3 Fall 2005CSE 115/503 Introduction to Computer Science I3 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?

4 Fall 2005CSE 115/503 Introduction to Computer Science I4 Two choices

5 Fall 2005CSE 115/503 Introduction to Computer Science I5 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()); }

6 Fall 2005CSE 115/503 Introduction to Computer Science I6 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.

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

8 Fall 2005CSE 115/503 Introduction to Computer Science I8 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)

9 Fall 2005CSE 115/503 Introduction to Computer Science I9 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)

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

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

12 Fall 2005CSE 115/503 Introduction to Computer Science I12 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!

13 Fall 2005CSE 115/503 Introduction to Computer Science I13 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.

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

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

16 Fall 2005CSE 115/503 Introduction to Computer Science I16 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.


Download ppt "Fall 2005CSE 115/503 Introduction to Computer Science I1 Strategy Pattern Lab 5 retrospective –Base fish provided –Specialized fish were modeled as subclasses."

Similar presentations


Ads by Google