代码实现
package ltd.huazhongyang.strategy.strategy.interfaces;
public interface FlyBehavior {
/**
* 子类具体实现
*/
void fly();
}
- 编写飞行技术高超类,并实现FlyBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
public class GoodFlyBehavior implements FlyBehavior {
@Override
public void fly() {
System.out.println("飞行技术呱呱叫。。。");
}
}
- 编写飞行技术一般类,并实现FlyBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
public class BadFlyBehavior implements FlyBehavior {
@Override
public void fly() {
System.out.println("飞行技术就那样。。。");
}
}
- 编写不会飞行技术类,并实现FlyBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
public class NoFlyBehavior implements FlyBehavior {
@Override
public void fly() {
System.out.println("没有遨游太空的权力。。。");
}
}
package ltd.huazhongyang.strategy.strategy.interfaces;
public interface QuackBehavior {
/**
* 子类具体实现
*/
void quack();
}
- 编写嘎嘎叫类,并实现QuackBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
public class GaGaQuackBehavior implements QuackBehavior {
@Override
public void quack() {
System.out.println("嘎嘎叫。。。");
}
}
- 编写咯咯叫类,并实现QuackBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
public class GeGeQuackBehavior implements QuackBehavior {
@Override
public void quack() {
System.out.println("咯咯叫。。。");
}
}
- 编写不会叫类,并实现QuackBehavior接口
package ltd.huazhongyang.strategy.strategy.interfaces.impl;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
public class NoQuackBehavior implements QuackBehavior {
@Override
public void quack() {
System.out.println("不会叫。。。");
}
}
- 编写Duck抽象类,并聚合FlyBehavior和QuackBehavior接口类
package ltd.huazhongyang.strategy.strategy.abstracts;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
public abstract class Duck {
/**
* 策略接口
*/
private FlyBehavior flyBehavior ;
private QuackBehavior quackBehavior ;
public Duck(){
}
public void setFlyBehavior(FlyBehavior flyBehavior) {
this.flyBehavior = flyBehavior;
}
public void setQuackBehavior(QuackBehavior quackBehavior) {
this.quackBehavior = quackBehavior;
}
public void fly(){
if(flyBehavior != null){
flyBehavior.fly();
}
}
public void quack(){
if(quackBehavior != null){
quackBehavior.quack();
}
}
public abstract void display();//显示鸭子信息
}
package ltd.huazhongyang.strategy.strategy.abstracts.entends;
import ltd.huazhongyang.strategy.strategy.abstracts.Duck;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.GaGaQuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.GoodFlyBehavior;
public class WildDuck extends Duck {
/**
* 构造器 传入FlyBehavior
*/
public WildDuck(){
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(new GaGaQuackBehavior());
}
public WildDuck(FlyBehavior flyBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(new GaGaQuackBehavior());
}
public WildDuck(QuackBehavior quackBehavior) {
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(quackBehavior);
}
public WildDuck(FlyBehavior flyBehavior,QuackBehavior quackBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(quackBehavior);
}
@Override
public void display() {
System.out.println("这是野鸭子~~~~~");
}
}
- 编写北京鸭子类,并继承Duck超类以及实现相关方法
package ltd.huazhongyang.strategy.strategy.abstracts.entends;
import ltd.huazhongyang.strategy.strategy.abstracts.Duck;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.GeGeQuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.GoodFlyBehavior;
public class PakingDuck extends Duck {
/**
* 构造器 传入FlyBehavior
*/
public PakingDuck(){
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(new GeGeQuackBehavior());
}
public PakingDuck(FlyBehavior flyBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(new GeGeQuackBehavior());
}
public PakingDuck(QuackBehavior quackBehavior) {
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(quackBehavior);
}
public PakingDuck(FlyBehavior flyBehavior,QuackBehavior quackBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(quackBehavior);
}
@Override
public void display() {
System.out.println("这是北京鸭子");
}
}
- 编写玩具鸭子类,并继承Duck超类以及实现相关方法
package ltd.huazhongyang.strategy.strategy.abstracts.entends;
import ltd.huazhongyang.strategy.strategy.abstracts.Duck;
import ltd.huazhongyang.strategy.strategy.interfaces.FlyBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.QuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.NoQuackBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.GoodFlyBehavior;
public class ToyDuck extends Duck {
/**
* 构造器 传入FlyBehavior
*/
public ToyDuck(){
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(new NoQuackBehavior());
}
public ToyDuck(FlyBehavior flyBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(new NoQuackBehavior());
}
public ToyDuck(QuackBehavior quackBehavior) {
super.setFlyBehavior(new GoodFlyBehavior());
super.setQuackBehavior(quackBehavior);
}
public ToyDuck(FlyBehavior flyBehavior,QuackBehavior quackBehavior) {
super.setFlyBehavior(flyBehavior);
super.setQuackBehavior(quackBehavior);
}
@Override
public void display() {
System.out.println("这是玩具鸭子~~~~~");
}
}
package ltd.huazhongyang.strategy.strategy;
import ltd.huazhongyang.strategy.strategy.abstracts.entends.PakingDuck;
import ltd.huazhongyang.strategy.strategy.abstracts.entends.WildDuck;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.BadFlyBehavior;
import ltd.huazhongyang.strategy.strategy.interfaces.impl.NoQuackBehavior;
public class Client {
public static void main(String[] args) {
WildDuck wildDuck = new WildDuck();
wildDuck.display();
wildDuck.fly();
wildDuck.quack();
PakingDuck pakingDuck = new PakingDuck();
pakingDuck.display();
pakingDuck.fly();
pakingDuck.quack();
PakingDuck pakingDuck1 = new PakingDuck(new BadFlyBehavior(),new NoQuackBehavior());
pakingDuck1.display();
pakingDuck1.fly();
pakingDuck1.quack();
}
}
- 测试结果