image

Motivation

Problem: Started with a simple SimUDuck App.

image

But now we need the ducks to FLY

image

First Design:

image

What we got, Rubber ducks are also able to fly

image

image

Drawbacks

image

Second Design:

Using inheritance and polymorphism

image

Drawbacks:

Third Idea

How about an interface?

image

image

Drawbacks: It completely destroy code reuse for those behaviors.

image

Why this will not work?

Conclusions:

Solution

  1. Design Principles: Identify the aspects of your application that vary and separate them form what stays the same! which means Encapsulate the parts that vary!
  2. Design Principles: Program to an interface, not an implementation! (interface here means supertype! including interface and abstract class!.. making use of the polymorphism functionality).
  3. Design Principles: Favor composition over interface!

Strategy Pattern

image

image

image

Final UML Diagram

image

Ref:

TODO: