Pattern Design - Strategy Pattern
If you can disassemble code and you want to apply one method of an object
in another object that there is no inheritance between them, you can use Strategy
Pattern. Aha, actually it is good strategy that we can wrap different kinds of algorithms,
then pass any of them to solve problem in different kinds of situation.
Now let's dive into it and show how it works!
1. Duck Class
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* 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 void setFlyBehavior(FlyBehavior fb) {
flyBehavior = fb;
}
public void setQuackBehavior(QuackBehavior qb) {
quackBehavior = qb;
}
}
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* ModelDuck.java
*/
public class ModelDuck extends Duck {
public ModelDuck() {
flyBehavior = new FlyNoWay(); // Can not fly at first.
quackBehavior = new Quack();
}
public void display() {
System.out.println("I'm flying with a rocket.");
}
}
2.FlyBehavior Class
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* FlyBehavior.java
*/ /**
* We declare a flying behavior interface so that
* we can implement different kinds of behaviors
* to ducks.
*/ public interface FlyBehavior {
public void fly();
}
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* FlyNoWay.java
*/
public class FlyNoWay implements FlyBehavior {
public void fly() {
System.out.println("I can't fly.");
}
}
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
*/ /**
* FlyWithWings
* FlyWithWings.java
*/ public class FlyWithWings implements FlyBehavior {
public void fly() {
System.out.println("I'm flying");
}
}
3.QuackBehavior Class
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* QuackBehavior.java
*/
public interface QuackBehavior {
public void quack();
}
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* Quack.java
*/
public class Quack implements QuackBehavior {
public void quack() {
System.out.println("Quack");
}
}
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* MuteQuack.java
*/
public class MuteQuack implements QuackBehavior {
public void quack() {
System.out.println("<< Silence >>");
}
}
4. Test.
package Duck; /**
* Created by zhuangzebo on 2017/6/30.
* MultiDuckSimulator.java
*/
public class MultiDuckSimulator {
public static void main(String[] args) {
Duck model = new ModelDuck();
model.performFly();
model.setFlyBehavior(new FlyWithWings());
model.performFly(); model.performQuack();
model.setQuackBehavior(new MuteQuack());
model.performQuack();
}
}
I can't fly.
I'm flying
Quack
<< Silence >>
From the code above, We can know the apperance of strategy pattern. Here we can learn more for another example.
package processor; /**
* Created by zhuangzebo on 2017/6/30.
* Apply.java
*/ import java.util.*; class Processor {
public String name() {
return getClass().getSimpleName();
} Object process(Object input) {
return input;
}
} class Upcase extends Processor {
String process(Object input) {
return ((String)input).toUpperCase();
}
} class DownCase extends Processor {
String process(Object input) {
return ((String)input).toLowerCase();
}
} class Splitter extends Processor {
String process(Object input) {
return Arrays.toString(((String)input).split(" "));
}
} public class Apply {
public static void process(Processor p, Object s) {
System.out.println("Using Processor " + p.name());
System.out.println(p.process(s));
} public static String s = "Disagreement with beliefs is by definition incorrect.";
public static void main(String[] args) {
process(new Upcase(), s);
process(new DownCase(), s);
process(new Splitter(), s);
}
}
Pattern Design - Strategy Pattern的更多相关文章
- 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)
在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了“工厂模式”.“策略模式”.“状态模式”等.当然在重构时,有的地 ...
- 深入浅出设计模式——策略模式(Strategy Pattern)
模式动机 完成一项任务,往往可以有多种不同的方式,每一种方式称为一个策略,我们可以根据环境或者条件的不同选择不同的策略来完成该项任务.在软件开发中也常常遇到类似的情况,实现某一个功能有多个途径,此时可 ...
- [转]设计模式(22)-Strategy Pattern
一. 策略(Strategy)模式 策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化. 假 设现 ...
- 8.6 GOF设计模式四: 策略模式… Strategy Pattern
策略模式… Strategy Pattern 在POS系统中,有时需要实行价格优惠, 该如何处理? 对普通客户或新客户报全价 对老客户统一折扣5% 对大客户统一折扣10% 注:课件 ...
- 设计模式 - 策略模式(Strategy Pattern) 具体解释
策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全 ...
- 第 1 章 策略模式【Strategy Pattern】
第 1 章 策略模式[Strategy Pattern] 以下内容出自: 24种设计模式介绍与6大设计原则.pdf 刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手 ...
- [Design Pattern] Service Locator Pattern 简单案例
Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...
- [Design Pattern] Front Controller Pattern 简单案例
Front Controller Pattern, 即前端控制器模式,用于集中化用户请求,使得所有请求都经过同一个前端控制器处理,处理内容有身份验证.权限验证.记录和追踪请求等,处理后再交由分发器把请 ...
- HeadFirst设计模式读书笔记(1)-策略模式(Strategy Pattern)
策略模式(Strategy Pattern): 定义了了算法簇,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户端. 第一个设计原则:找出应用中可能需要变化之处,把他们独立 ...
随机推荐
- weex Mac创建项目
序言:本来在win 10 上创建项目真的很顺利!后来入手一个mac就从mac 上下载了最新的android studio开始搞起了weex,问题来了,weex-toolkit脚手架还是老的,我觉得是w ...
- Sql server 编写99乘法表
Sql 组织编写语句 declare @one int,@tow int,@str varchar(100),@num intselect @one=1while(@one<=9)beginse ...
- bootstrap-treeview初使用
<div id="tree">div> $(function () { function getTree() { var data = [{ text: &quo ...
- web socket
@ServerEndpoint("/my-websocket") @Component public class MyWebSocket { protected final Log ...
- adobe cc最新版 软件安装与激活
adobe cc最新版 软件安装与激活:https://m.weike.fm/lecture/4912961 说明#:Adobe CC2017的所有软件都可以按照以上方法进行安装,如:Premier ...
- 什么是python的全局解释锁(GIL)
GIL解决了Python中的什么问题? 为什么选取GIL作为解决方案? 对多线程Python程序的影响 为什么GIL还没有被删除? 为什么在Python 3 中GIL没有被移除? 如何处理Python ...
- django在centos部署
各种坑各种蛋疼,搞了两天终于能用了.整体分2步: 1. 通过uwsgi 启动django项目 在manage.py同级目录,新建uwsgi.ini文件 [uwsgi] # 配置服务器的监听ip和端口, ...
- React.js 学习笔记
React.js React.js 是时下最流行的前端 JavaScript 框架之一. 创建工程 # 安装 CLI $ npm install -g create-react-app # 创建新的应 ...
- 记账本,C,Github,util
package util; import java.awt.Component; import java.awt.Dimension; import javax.swing.JButton; impo ...
- Django_Form验证(二),ajax验证
还是一个简单的html提交页面,ajax提交就不需要form表单了: <p><input id="a" type="text" name=&q ...