Java之美[从菜鸟到高手演变]之设计模式四

在阅读过程中有任何问题,请及时联系:egg。
邮箱:
xtfggef@gmail.com 微博:http://weibo.com/xtfggef
转载请说明出处:http://blog.csdn.net/zhangerqing

其实每个设计模式都是很重要的一种思想,看上去很熟,是因为我们在学到的东西中都有涉及,尽管有时我们并不知道,其实在Java本身的设计之中处处都有体现,像AWT、JDBC、集合类、IO管道或者是Web框架,里面设计模式无处不在。


本章不出意外的话,应该是设计模式最后一讲了,首先还是上图:

本章讲讲第三类和第四类。

19、备忘录模式(Memento)

主要目的是保存一个对象的某个状态,以便在适当的时候恢复对象,个人觉得叫备份模式更形象些。

通俗的讲下:假设有原始类A,A中有各种属性,A可以决定需要备份的属性,备忘录类B是用来存储A的一些内部状态,类C呢,就是一个用来存储备忘录的,且只能存储,不能修改等操作。做个图来分析一下:


Original类是原始类,里面有需要保存的属性value及创建一个备忘录类,用来保存value值。Memento类是备忘录类,Storage类是存储备忘录的类,持有Memento类的实例,该模式很好理解。直接看源码:

[java]  view plain copy
  1. public class Original {  //原始类,类中有带保存的属性
  2.       
  3.     private String value;  
  4.       
  5.     public String getValue() {  
  6.         return value;  
  7.     }  
  8.   
  9.     public void setValue(String value) {  
  10.         this.value = value;  
  11.     }  
  12.   
  13.     public Original(String value) {  //构造函数
  14.         this.value = value;  
  15.     }  
  16.   
  17.     public Memento createMemento(){  //创建一个备忘录类,用来保存value值
  18.         return new Memento(value);  
  19.     }  
  20.       
  21.     public void restoreMemento(Memento memento){  //
  22.         this.value = memento.getValue();  
  23.     }  
  24. }  
[java]  view plain copy
  1. public class Memento {  //备忘录类,
  2.       
  3.     private String value;  
  4.   
  5.     public Memento(String value) {  //构造函数
  6.         this.value = value;  
  7.     }  
  8.   
  9.     public String getValue() {  
  10.         return value;  
  11.     }  
  12.   
  13.     public void setValue(String value) {  
  14.         this.value = value;  
  15.     }  
  16. }  
[java]  view plain copy
  1. public class Storage {  //用来存储备忘录类
  2.       
  3.     private Memento memento;  
  4.       
  5.     public Storage(Memento memento) {  
  6.         this.memento = memento;  
  7.     }  
  8.   
  9.     public Memento getMemento() {  
  10.         return memento;  
  11.     }  
  12.   
  13.     public void setMemento(Memento memento) {  
  14.         this.memento = memento;  
  15.     }  
  16. }  

测试类:

[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         // 创建原始类  
  6.         Original origi = new Original("egg");  
  7.   
  8.         // 创建备忘录  
  9.         Storage storage = new Storage(origi.createMemento());  
  10.   
  11.         // 修改原始类的状态  
  12.         System.out.println("初始化状态为:" + origi.getValue());  
  13.         origi.setValue("niu");  
  14.         System.out.println("修改后的状态为:" + origi.getValue());  
  15.   
  16.         // 回复原始类的状态  
  17.         origi.restoreMemento(storage.getMemento());  
  18.         System.out.println("恢复后的状态为:" + origi.getValue());  
  19.     }  
  20. }  

输出:

初始化状态为:egg
修改后的状态为:niu
恢复后的状态为:egg

简单描述下:新建原始类时,value被初始化为egg,后经过修改,将value的值置为niu,最后倒数第二行进行恢复状态,结果成功恢复了。其实我觉得这个模式叫“备份-恢复”模式最形象。



20、状态模式(State)

核心思想就是:当对象的状态改变时,同时改变其行为!

就拿QQ来说,有几种状态,在线、隐身、忙碌等,当你切换状态的时候,你的好友会看到行为的变化。

所以,状态模式就两点:1、可以通过改变状态来获得不同的行为。2、你的好友能同时看到你的变化。看图:


State类是个状态类,Context类可以实现切换,我们来看看代码:

[java]  view plain copy
  1. package com.xtfggef.dp.state;  
  2.   
  3. /** 
  4.  * 状态类的核心类 
  5.  * 2012-12-1 
  6.  * @author erqing 
  7.  * 
  8.  */  
  9. public class State {  //状态类
  10.       
  11.     private String value;  
  12.       
  13.     public String getValue() {  
  14.         return value;  
  15.     }  
  16.   
  17.     public void setValue(String value) {  
  18.         this.value = value;  
  19.     }  
  20.   
  21.     public void method1(){  
  22.         System.out.println("execute the first opt!");  
  23.     }  
  24.       
  25.     public void method2(){  
  26.         System.out.println("execute the second opt!");  
  27.     }  
  28. }  
[java]  view plain copy
  1. package com.xtfggef.dp.state;  
  2.   
  3. /** 
  4.  * 状态模式的切换类   2012-12-1 
  5.  * @author erqing 
  6.  *  
  7.  */  
  8. public class Context {  //可以实现切换
  9.   
  10.     private State state;  
  11.   
  12.     public Context(State state) {  
  13.         this.state = state;  
  14.     }  
  15.   
  16.     public State getState() {  
  17.         return state;  
  18.     }  
  19.   
  20.     public void setState(State state) {  
  21.         this.state = state;  
  22.     }  
  23.   
  24.     public void method() {  
  25.         if (state.getValue().equals("state1")) {  
  26.             state.method1();  
  27.         } else if (state.getValue().equals("state2")) {  
  28.             state.method2();  
  29.         }  
  30.     }  
  31. }  
测试类:

[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         State state = new State();  
  6.         Context context = new Context(state);  
  7.           
  8.         //设置第一种状态  
  9.         state.setValue("state1");  
  10.         context.method();  
  11.           
  12.         //设置第二种状态  
  13.         state.setValue("state2");  
  14.         context.method();  
  15.     }  
  16. }  
输出:

execute the first opt!
execute the second opt!

根据这个特性,状态模式在日常开发中用的挺多的,尤其是做网站的时候,我们有时希望根据对象的某一属性,区别开他们的一些功能,比如说简单的权限控制等。



21、访问者模式(Visitor)


简单来说,访问者模式就是一种分离对象数据结构与行为的方法,通过这种分离,可达到为一个被访问者动态添加新的操作而无需做其它的修改的效果。简单关系图:


来看看原码:一个Visitor类,存放要访问的对象,

[java]  view plain copy
  1. public interface Visitor {  
  2.     public void visit(Subject sub);  
  3. }  
[java]  view plain copy
  1. public class MyVisitor implements Visitor {  
  2.   
  3.     @Override  
  4.     public void visit(Subject sub) {  
  5.         System.out.println("visit the subject:"+sub.getSubject());  
  6.     }  
  7. }  
Subject类,accept方法,接受将要访问它的对象,getSubject()获取将要被访问的属性,
[java]  view plain copy
  1. public interface Subject {  
  2.     public void accept(Visitor visitor);  
  3.     public String getSubject();  
  4. }  
[java]  view plain copy
  1. public class MySubject implements Subject {  
  2.   
  3.     @Override  
  4.     public void accept(Visitor visitor) {  
  5.         visitor.visit(this);  
  6.     }  
  7.   
  8.     @Override  
  9.     public String getSubject() {  
  10.         return "love";  
  11.     }  
  12. }  
测试:

[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         Visitor visitor = new MyVisitor();  
  6.         Subject sub = new MySubject();  
  7.         sub.accept(visitor);      
  8.     }  
  9. }  
输出:visit the subject:love

该模式适用场景:如果我们想为一个现有的类增加新功能,不得不考虑几个事情:

1、新功能会不会与现有功能出现兼容性问题?

2、以后会不会再需要添加?

3、如果类不允许修改代码怎么办?

面对这些问题,最好的解决方法就是使用访问者模式,访问者模式适用于数据结构相对稳定的系统,把数据结构和算法解耦。



22、中介者模式(Mediator)

中介者模式也是用来降低类类之间的耦合的,因为如果类类之间有依赖关系的话,不利于功能的拓展和维护,因为只要修改一个对象,其它关联的对象都得进行修改。如果使用中介者模式,只需关心和Mediator类的关系,具体类类之间的关系及调度交给Mediator就行,这有点像spring容器的作用。先看看图:


User类统一接口,User1和User2分别是不同的对象,二者之间有关联,如果不采用中介者模式,则需要二者相互持有引用,这样二者的耦合度很高,为了解耦,引入了Mediator类,提供统一接口,MyMediator为其实现类,里面持有User1和User2的实例,用来实现对User1和User2的控制。这样User1和User2两个对象相互独立,他们只需要保持好和Mediator之间的关系就行,剩下的全由MyMediator类来维护!基本实现:

[java]  view plain copy
  1. public interface Mediator {  
  2.     public void createMediator();  
  3.     public void workAll();  
  4. }  
[java]  view plain copy
  1. public class MyMediator implements Mediator {  
  2.   
  3.     private User user1;  
  4.     private User user2;  
  5.       
  6.     public User getUser1() {  
  7.         return user1;  
  8.     }  
  9.   
  10.     public User getUser2() {  
  11.         return user2;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void createMediator() {  
  16.         user1 = new User1(this);  
  17.         user2 = new User2(this);  
  18.     }  
  19.   
  20.     @Override  
  21.     public void workAll() {  
  22.         user1.work();  
  23.         user2.work();  
  24.     }  
  25. }  
[java]  view plain copy
  1. public abstract class User {  
  2.       
  3.     private Mediator mediator;  //为什么??
  4.       
  5.     public Mediator getMediator(){  
  6.         return mediator;  
  7.     }  
  8.       
  9.     public User(Mediator mediator) {  
  10.         this.mediator = mediator;  
  11.     }  
  12.   
  13.     public abstract void work();  
  14. }  
[java]  view plain copy
  1. public class User1 extends User {  //User1也是持有MyMediator引用的
  2.   
  3.     public User1(Mediator mediator){  
  4.         super(mediator);  
  5.     }  
  6.       
  7.     @Override  
  8.     public void work() {  
  9.         System.out.println("user1 exe!");  
  10.     }  
  11. }  
[java]  view plain copy
  1. public class User2 extends User {  
  2.   
  3.     public User2(Mediator mediator){  
  4.         super(mediator);  
  5.     }  
  6.       
  7.     @Override  
  8.     public void work() {  
  9.         System.out.println("user2 exe!");  
  10.     }  
  11. }  
测试类:

[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Mediator mediator = new MyMediator();  
  5.         mediator.createMediator();  
  6.         mediator.workAll();  
  7.     }  
  8. }  
输出:

user1 exe!
user2 exe!
23、解释器模式(Interpreter)
解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄。


Context类是一个上下文环境类,Plus和Minus分别是用来计算的实现,代码如下:

[java]  view plain copy
  1. public interface Expression {  
  2.     public int interpret(Context context);  
  3. }  
[java]  view plain copy
  1. public class Plus implements Expression {  
  2.   
  3.     @Override  
  4.     public int interpret(Context context) {  
  5.         return context.getNum1()+context.getNum2();  
  6.     }  
  7. }  
[java]  view plain copy
  1. public class Minus implements Expression {  
  2.   
  3.     @Override  
  4.     public int interpret(Context context) {  
  5.         return context.getNum1()-context.getNum2();  
  6.     }  
  7. }  
[java]  view plain copy
  1. public class Context {  
  2.       
  3.     private int num1;  
  4.     private int num2;  
  5.       
  6.     public Context(int num1, int num2) {  
  7.         this.num1 = num1;  
  8.         this.num2 = num2;  
  9.     }  
  10.       
  11.     public int getNum1() {  
  12.         return num1;  
  13.     }  
  14.     public void setNum1(int num1) {  
  15.         this.num1 = num1;  
  16.     }  
  17.     public int getNum2() {  
  18.         return num2;  
  19.     }  
  20.     public void setNum2(int num2) {  
  21.         this.num2 = num2;  
  22.     }  
  23.       
  24.       
  25. }  
[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         // 计算9+2-8的值  
  6.         int result = new Minus().interpret((new Context(new Plus()  
  7.                 .interpret(new Context(92)), 8)));  
  8.         System.out.println(result);  
  9.     }  
  10. }  
最后输出正确的结果:3。

基本就这样,解释器模式用来做各种各样的解释器,如正则表达式等的解释器等等!

设计模式基本就这么大概讲完了,总体感觉有点简略,的确,这么点儿篇幅,不足以对整个23种设计模式做全面的阐述,此处读者可将它作为一个理论基础去学习,通过这四篇博文,先基本有个概念,虽然我讲的有些简单,但基本都能说明问题及他们的特点,如果对哪一个感兴趣,可以继续深入研究!同时我也会不断更新,尽量补全遗漏、修正不足,欢迎广大读者及时提出好的建议,我们一起学习!项目中涉及到的代码,已经放到了我的资源里:http://download.csdn.net/detail/zhangerqing/4835830(因为我不喜欢不劳而获,所以没有免积分,只设置了5个,如果有人实在没积分又急要,那么联系我吧,我给你发过去)。

在阅读的过程中,有任何问题,请联系:egg。

邮箱:xtfggef@gmail.com   微博:http://weibo.com/xtfggef


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值