160 likes | 245 Views
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.
E N D
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
Combinations of behaviors • How is a fish with a combination of behaviors handled? • Not so well… CSE 115/503 Introduction to Computer Science I
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
Two choices CSE 115/503 Introduction to Computer Science I
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
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
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
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
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
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
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
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
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
Strategy Pattern Fish specification and behavior specifications are decoupled. CSE 115/503 Introduction to Computer Science I
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
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