博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试总结之 oop desing 之 The Strategy Pattern
阅读量:6572 次
发布时间:2019-06-24

本文共 2596 字,大约阅读时间需要 8 分钟。

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

 

1.Duck 问题(head frist oop)

 

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

//Duck.javapublic 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

你可能感兴趣的文章
android消息机制—Looper
查看>>
中台之上(五):业务架构和中台的难点,都是需要反复锤炼出标准模型
查看>>
为什么中台是传统企业数字化转型的关键?
查看>>
使用模板将Web服务的结果转换为标记语言
查看>>
inno setup 打包脚本学习
查看>>
php 并发控制中的独占锁
查看>>
禁止微信浏览器的下拉滑动
查看>>
从pandas到geopandas
查看>>
LOL设计模式之「策略模式」
查看>>
用express搭建网站
查看>>
如何在 Swift 中进行错误处理
查看>>
[Leetcode] Factor Combinations 因数组合
查看>>
用tinypng插件创建gulp task压缩图片
查看>>
浅谈DOMContentLoaded事件及其封装方法
查看>>
BetaMeow----利用机器学习做五子棋AI
查看>>
APM终端用户体验监控分析(下)
查看>>
React Native 0.20官方入门教程
查看>>
JSON for Modern C++ 3.6.0 发布
查看>>
Tomcat9.0部署iot.war(环境mysql8.0,centos7.2)
查看>>
Powershell进阶学习(6) 部署 Windows PowerShell Web 访问
查看>>