Академический Документы
Профессиональный Документы
Культура Документы
DESIGN PRINCIPLE
Identify the aspects of your application that vary and separate them from what
stays the same.
The Duck behaviors will live in a separate class—a class that implements a particular behavior
interface.
That way, the Duck classes won’t need to know any of the implementation details for their own
behaviors.
DESIGN PRINCIPLE
Program to an interface, not an implementation.
Programming to an interface/supertype would be: (We know it’s a Dog, but we can now use the animal
reference polymorphically.)
a = getAnimal();
a.makeSound();
With our new design, the Duck subclasses will use a behavior represented by an interface ( FlyBehavior
and QuackBehavior ), so that the actual implementation of the behavior (in other words, the specific
concrete behavior coded in the class that implements the FlyBehavior or QuackBehavior )
won’t be locked into the Duck subclass .
We’ll replace fly() and quack() in the Duck class with two similar methods, called
performFly() and performQuack() .
To perform the quack, a Duck just allows the object that is referenced by
quackBehavior to quack for it.
3. How the flyBehavior and quackBehavior instance variables are set. Example looking the
CaezaverdeDuck class (o a MallardDuck ):
MiniDuckSimulator.java
Duck.java
public Duck() {}
public abstract void display();
FlyBehavior.java
QuackBehavior.java
MallardDuck.java
public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
Quack.java
@Override
public void quack() {
System.out.println("Quack");
}
}
FlyWithWings.java
@Override
public void fly() {
System.out.println("I'm flying!");
}
}
Set the duck’s behavior type through a setter method on the duck subclass, rather than by instantiating it in
the duck’s constructor.
public Duck() {}
FlyWithWings.java
@Override
public void fly() {
System.out.println("I'm flying!");
}
}
ModelDuck.java
public ModelDuck() {
flyBehavior = new FlyNoWay();
this.quackBehavior = new Quack();
}
@Override
public void display() {
System.out.println("I'm a model duck");
}
}
FlyRocketPowered.java
@Override
public void fly() {
System.out.println("I'm flying with a rocket");
}
}
Output
https://gist.github.com/Integralist/5736427
Context.prototype.executeA = function() {
return this.strategy();
}
// strategy users
contextUno = new Context(strategyAa);
contextDos = new Context(strategyAb);
contextUno.executeA();
contextDos.executeA();
OUTPUT
Strategy Aa
Strategy Ab