面试总结之 oop desing 之 The Strategy Pattern

本文通过鸭子模拟案例介绍了策略模式的概念及应用。策略模式定义了一系列可互换的算法,并将它们封装起来,使得算法可以独立于使用它的客户端变化。文中详细展示了如何通过接口实现不同飞行与叫声行为,并能在运行时动态改变鸭子的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Strategy Pattern defines a family of algorithms,
encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it.

 

1.Duck 问题(head frist oop)

 

有的能飞,有的能叫,经典问题.

//Duck.java

public abstract class Duck {
FlyBehavior flyBehavior;
QuackBehavior quackBehavior;
public Duck() {
}
public abstract void display();
public void performFly() {
flyBehavior.fly();
}
public void performQuack() {
quackBehavior.quack();
}
public void swim() {
System.out.println(“All ducks float, even decoys!”);
}
}



public class MallardDuck extends Duck {
public MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
public void display() {
System.out.println(“I’m a real Mallard duck”);
}
}

//Type and compile the FlyBehavior interface (FlyBehavior.java) and
//the two behavior implementation classes (FlyWithWings.java and
//FlyNoWay.java).
接口作为一个行为,因为有的能飞有的不能飞,所以不能够在父类里写fly(). 用一个interface作为一个行为汇总,interface下还有其他的行为. public interface FlyBehavior { public void fly(); }

public class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println(“I’m flying!!”);
}
}

public class FlyNoWay implements FlyBehavior {
public void fly() {
System.out.println(“I can’t fly”);
}
}

 

public interface QuackBehavior {
public void quack();
}

public class Quack implements QuackBehavior {
public void quack() {
System.out.println(“Quack”);
}
}

public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println(“<< Silence >>”);
}
}

public class Squeak implements QuackBehavior {

public void quack() {
System.out.println(“Squeak”);
}
}

Setting behavior dynamically

//Add two new methods to the Duck class:

public void setFlyBehavior(FlyBehavior fb) {
fl yBehavior = fb;
}
public void setQuackBehavior(QuackBehavior qb) {
quackBehavior = qb;
}
//Make a new Duck type (ModelDuck.java).

public class ModelDuck extends Duck {
public ModelDuck() {
flyBehavior = new FlyNoWay();
quackBehavior = new Quack();
}
public void display() {
System.out.println(“I’m a model duck”);
}
}
//Make a new FlyBehavior type (FlyRocketPowered.java).

public class FlyRocketPowered implements FlyBehavior {
public void fly() {
System.out.println(“I’m fl ying with a rocket!”);
}
}
//Change the test class (MiniDuckSimulator.java), add the
//ModelDuck, and make the ModelDuck rocket-enabled.

public class MiniDuckSimulator {
public static void main(String[] args) {
Duck mallard = new MallardDuck();
mallard.performQuack();
mallard.performFly();
Duck model = new ModelDuck();
model.performFly();
model.setFlyBehavior(new FlyRocketPowered());
model.performFly();
}
}

 

转载于:https://www.cnblogs.com/leetcode/p/3208307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值