fall 2005cse 115/503 introduction to computer science i1 strategy pattern lab 5 retrospective...

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

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Fall 2005 CSE 115/503 Introduction to Computer Science I 1

Strategy Pattern• Lab 5 retrospective

– Base fish provided– Specialized fish were

modeled as subclasses of the base fish

– This works!

Fall 2005 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…

Fall 2005 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?

Fall 2005 CSE 115/503 Introduction to Computer Science I 4

Two choices

Fall 2005 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());}

Fall 2005 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.

Fall 2005 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)

Fall 2005 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)

Fall 2005 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)

Fall 2005 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

Fall 2005 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

Fall 2005 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!

Fall 2005 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.

Fall 2005 CSE 115/503 Introduction to Computer Science I 14

Strategy Pattern

Fish specification and behaviorspecifications are decoupled.

Fall 2005 CSE 115/503 Introduction to Computer Science I 15

Combining behaviors

• One way is to make use of the Composite Pattern:

Composite lets us combinebehaviors dynamically!

Fall 2005 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.