设计模式之禅PK之行为类1
来源:互联网 发布:windows10 node sass 编辑:程序博客网 时间:2024/06/06 06:35
设计模式之禅PK之行为类1
行为类设计模式
- 行为类模式:
- 责任链模式
- 命令模式
- 迭代器模式
- 中介者模式
- 备忘录模式
- 观察者模式
- 状态模式
- 策略模式
- 模板方法模式
- 访问者模式
【命令模式】VS【策略模式】
- 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接受者(Receiver)角色
- 两者的区别
- 从压缩文件继续PK
- zip压缩/解压缩【Windows操作系统常用的压缩方法】
- gzip压缩/解压缩【Linus常用的压缩方法】
类图对比
代码具体实现
策略模式代码
Algorithm
package com.peng.pk_cl;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public interface Algorithm { // 压缩算法 public boolean compress(String source, String to); // 解压算法 public boolean uncompress(String source, String to);}
Zip
package com.peng.pk_cl;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public class Zip implements Algorithm { @Override public boolean compress(String source, String to) { System.out.println(source + "====>>zip压缩成功!======>>" + to); return true; } @Override public boolean uncompress(String source, String to) { System.out.println(source + "====>>zip解压缩成功!======>>" + to); return true; }}
Gzip
package com.peng.pk_cl;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public class Gzip implements Algorithm { @Override public boolean compress(String source, String to) { System.out.println(source + "====>>Gzip压缩成功!======>>" + to); return true; } @Override public boolean uncompress(String source, String to) { System.out.println(source + "====>>Gzip解压缩成功!======>>" + to); return true; }}
Context
package com.peng.pk_cl;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public class Context { // 指向抽象算法 private Algorithm al; // 构造函数传递具体的算法 public Context(Algorithm al) { super(); this.al = al; } // 执行压缩算法 public boolean compress(String source, String to) { return al.compress(source, to); } // 执行解压缩算法 public boolean uncompress(String source, String to) { return al.uncompress(source, to); }}
Client
package com.peng.pk_cl;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public class Client { public static void main(String[] args) { // 定义环境角色 Context context; // 执行zip的压缩算法 System.out.println("====对文件执行zip压缩算法====="); context = new Context(new Zip()); context.compress("a.txt", "a.txt.zip"); // 执行zip的解压缩算法 System.out.println("====对文件执行zip解压缩算法====="); context = new Context(new Zip()); context.uncompress("a.txt.zip", "a.txt"); // 执行Gzip的压缩算法 System.out.println("====对文件执行gZip压缩算法====="); context = new Context(new Gzip()); context.compress("a.txt", "a.txt.gzip"); // 执行zip的解压缩算法 System.out.println("====对文件执行Gzip解压缩算法====="); context = new Context(new Gzip()); context.uncompress("a.txt.gzip", "a.txt"); }}
执行结果
====对文件执行zip压缩算法=====a.txt====>>zip压缩成功!======>>a.txt.zip====对文件执行zip解压缩算法=====a.txt.zip====>>zip解压缩成功!======>>a.txt====对文件执行gZip压缩算法=====a.txt====>>Gzip压缩成功!======>>a.txt.gzip====对文件执行Gzip解压缩算法=====a.txt.gzip====>>Gzip解压缩成功!======>>a.txt
命令模式代码
AbstractCmd
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月13日 * @description */public abstract class AbstractCmd { // 对接收者的引用 protected IReceiver zip = new ZipReceiver(); protected IReceiver gzip = new GzipReceiver(); // 抽象方法,命令的具体单元 public abstract boolean execute(String source, String to);}
ZipCompressCmd
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class ZipCompressCmd extends AbstractCmd { @Override public boolean execute(String source, String to) { return super.zip.compress(source, to); }}
ZipUncompressCmd
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class ZipUncompressCmd extends AbstractCmd { @Override public boolean execute(String source, String to) { return super.zip.uncompress(source, to); }}
GzipCompressCmd
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class GzipCompressCmd extends AbstractCmd { @Override public boolean execute(String source, String to) { return super.gzip.compress(source, to); }}
GzipUncompressCmd
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class GzipUncompressCmd extends AbstractCmd { @Override public boolean execute(String source, String to) { return super.gzip.uncompress(source, to); }}
IReceiver
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public interface IReceiver { // 压缩 public boolean compress(String source, String to); // 解压缩 public boolean uncompress(String source, String to);}
ZipReceiver
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class ZipReceiver implements IReceiver { @Override public boolean compress(String source, String to) { System.out.println(source + "=====》压缩=====》" + to); return true; } @Override public boolean uncompress(String source, String to) { System.out.println(source + "=====》解压缩=====》" + to); return true; }}
GzipReceiver
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class GzipReceiver implements IReceiver { @Override public boolean compress(String source, String to) { System.out.println(source + "=====》压缩=====》" + to); return true; } @Override public boolean uncompress(String source, String to) { System.out.println(source + "=====》解压缩=====》" + to); return true; }}
Invoker
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class Invoker { // 抽象命令的引用 public AbstractCmd cmd; public Invoker(AbstractCmd cmd) { super(); this.cmd = cmd; } // 执行命令 public boolean execute(String source, String to) { return cmd.execute(source, to); }}
Client
package com.peng.pk_ml;/** * @author kungfu~peng * @data 2017年12月14日 * @description */public class Client { public static void main(String[] args) { // 定义一个命令,压缩一个文件 AbstractCmd cmd = new ZipCompressCmd(); Invoker invoker = new Invoker(cmd); invoker.execute("a.txt", "a.txt.zip"); // 解压缩 AbstractCmd cmd2 = new ZipUncompressCmd(); Invoker invoker2 = new Invoker(cmd2); invoker2.execute("a.txt.zip", "a.txt"); // 定义一个命令,压缩一个文件 AbstractCmd cmd3 = new GzipCompressCmd(); Invoker invoker3 = new Invoker(cmd3); invoker3.execute("a.txt", "a.txt.gzip"); // 解压缩 AbstractCmd cmd4 = new GzipUncompressCmd(); Invoker invoker4 = new Invoker(cmd4); invoker4.execute("a.txt.gzip", "a.txt"); }}
执行结果
a.txt=====》压缩=====》a.txt.zipa.txt.zip=====》解压缩=====》a.txta.txt=====》压缩=====》a.txt.gzipa.txt.gzip=====》解压缩=====》a.txt
最佳实践
声明
- 摘自秦小波《设计模式之禅》第2版;
- 仅供学习,严禁商业用途;
- 代码手写,没有经编译器编译,有个别错误,自行根据上下文改正;
阅读全文
1 0
- 设计模式之禅PK之行为类1
- 设计模式之禅PK之行为类2
- 设计模式之禅PK之行为类3
- 《设计模式之禅》——行为类模式大PK
- 设计模式之禅PK之创建类1
- 设计模式之禅PK之结构类1
- 设计模式之禅PK之创建类2
- 设计模式之禅PK之结构类2
- 设计模式之行为
- 设计模式之行为类模式
- 设计模式之行为模式
- 设计模式之行为模式
- 设计模式之行为模式
- 设计模式之“PO”PK"OO"
- 《设计模式之禅》——创建类模式大PK
- 《设计模式之禅》——结构类模式大PK
- 行为类模式大PK
- 行为类模式大PK
- myeclipse 如何显示序号
- 〖NOIP2003P〗栈
- Can't connect to local MySQL server through socket '/tmp/mysql.sock'
- 12.springboot使用redis的订阅功能实现消息队列
- 新建maven一直加载缓存 C:\Users\Administratol\AppData\Local\Temp\..
- 设计模式之禅PK之行为类1
- 51Nod-1050-循环数组最大子段和
- 求某个数组里连续子数组最大和的几个算法
- unity 4.x 全屏
- NOIP2008-03-传纸条
- socket
- demon14.4-14.5
- 写在年末 the end of 2017
- jQuery快速入门基础教程之效果(三)