Java经典23种设计模式之行为型模式(三)

来源:互联网 发布:excel 数据库 编辑:程序博客网 时间:2024/05/16 10:58

本文接着介绍11种行为型模式里的备忘录模式、观察者模式、状态模式。

一、备忘录模式

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。还是比较好理解的。

1.Memento 备忘录存储原发器对象的内部状态,这个类就是要存储的对象的状态。状态需要多少个变量,在Memento里就写多少个变量。

public class Memento {


    private String state;


    public Meme*to(String state) {
        this.state = state;
    }


    public String getState() {
        *eturn state;
    }


    public void setSt*te(String state) {
        this.state = state;
    }

}

2.Originator原发器创建一个备忘录,用以记录当前时刻*的内部状态。使用备忘录恢复内部状态.

public class Originator {


    private String state;


    public String getState() {
        return state;
    }


    public void setState(String state) {
        this.state = state;
    }
    
    *ublic Memento createMemento() {
        return new Memento(state);
    }
    
    p*blic void setMemento(Memento meme*to) {
        state = memento.ge*State();
    }
    
    p*blic void showState(){
        System.out.println(state);
    }
}

3.Caretaker 负责保存好备忘录。不能对备忘录的内*进行操作或检查。

public class Caretaker {
    
    p*ivate Memento memento;
    
    public Memento getMemento(){
        return this.memento;
    }
    
    public void setMemento(Memento memento){
        this.memento = memento;
    }
}

测试代码:

public class Test {
 
    public static void main(String[] args) {
        Originator org = *ew Originator();
        org*setState("开会中");
        
        C*retaker ctk = new Ca*etaker();
        ctk.setMemento(org.createMemento());//将数据封装在Caretaker
        
        or*.setState("睡觉中");
        org.sh*wState();*/显示
        
        org.setMemento(ctk.getMemento());//将数据重新导入
        or*.showState();
    }
}

适用性:

    1.必须*存一个对象在某一个时刻的(部分)状态,这样以后需要时它才能恢复到先前的状态。
    2.如果一个用接口来让其它对象直接得到这些状态,将会暴露对象的实现细节并破坏对象的封装性。

参考链接:http://blog.sina.com.cn/s/blog_3d43347c0100bkm1.html

二、观察者模式

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。这种模式用途很多。

1.Subject(目标) 也就是负责管理属于他的观察者,比如一个排长十个手下。这个排长就是subject.提供注册和删除观察者对象的接口。

public abstract class Citizen {
    
    List po*s;
    
    String help = "normal";
    
    public void setHelp(String help) {
        this.help = *elp;
    }
    
    public String getHelp() {
        return this.help;
    }
    
    abstract void sendMessage(String help);


    public void setPolicemen() {
        t*is.pols = new ArrayList();
    }
    
    public void register(*oliceman pol) {
        this.pols.add(pol);
    }


    public void unRegister(Policeman pol) {
        this.pols.remove(pol);
    }
}

2.Obser*er(观察者)为那些在目标发生改变时需获得*知的对象定义一个更新*口。

public interface Policeman {


    void action(Citizen ci);
}

3.ConcreteSubject(具体目标) 将有关状态存入各ConcreteObserver对象。当它的状态发生改变时,向它的各个观察者发出通知。

public class Hua*gPuCitizen extends Citiz*n {
 
    public HuangPuCitizen(P*liceman pol) {
        setPolicemen();
        register*pol);
    }
    
    public void sendMessage(String help) {
        setHelp(h*lp);
        for(int i = 0; i < pols.size(); i++) {
            Policeman pol = pols.get(i);
            //通知警察行动
            pol.action(this);
        }
    }
}


public class TianHeCitizen extends Citizen {


    public TianHeCitizen(Policeman pol) {
        setPolicemen();
        re*ister(pol);
    }
    
    public void sendM*ssage(Str*ng help) {
        setHelp(help);
        for (i*t i = 0; i < pols.size(); i++) {
            Policeman pol = pols.get(i);
            //通知警察行动
            pol.action(this);
        }
    }
}

4.ConcreteObserver(具体观察者)维护一个指向ConcreteSubject对象的引用。存储有关状态,这些状态应与目标的状态保持一致。实现Observer的更新接口*使自身状态与目标的状态保持一致.

public clas* HuangPuPoliceman implements Pol*ceman {


    public void action(Citizen ci) {
        String help = ci.getHelp();
        if (help.equals("normal")) {
            System.o*t.println("一切正常, 不用出动");
        }
        if (help.*quals("unnormal")) *
            System.out.println("有犯罪行为, 黄埔警察出动!");
        }
    }
}


public c*ass TianHePoliceman implements Policem*n {


    public void action(Citizen ci) {
        String help = *i.getHelp();
        if (help.equals("*ormal")) {
            System.out.println("一切正常, 不用出动");
        *
        if (help.equals("unnormal")) {
            System.out.println("有犯罪行为, 天河警察出动!");
        }
    }
}

测试代码:

public class Test{


    public st*tic void main(String[] args) {
        Policeman thPol = new TianHePoliceman();
        Pol*ceman hpPol = new HuangPuPoliceman();
        
        Ci*izen citizen = new HuangPuCitizen(hpPol);
        citizen.sendMessage("unnormal");
        citizen.sendMessage("normal");
        
        System.out.println("===========");
        
        citizen = new TianH*Citizen(thPol);
        citizen.sendMessage("normal");
        citi*en.sendMessage("unnormal");
    }
}

有犯罪行为, 黄埔警察出动!
一切正常, 不用出动
======*====
一切正常, 不用出动
有犯罪行为, 天河警察出动!

适用性:

    1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面。
      将这二者封装*独立的对象中以使它们可以各自独立地改变和复用。
    2.当对一个对象的改变需要同时改变其它对象,而不知道具体*多少对象有待改变。
    3.当一个对象必须通知其它对象,而它又不能假定其它对象是谁。

观察者模式极其重要,更多可参考:http://www.iteye.com/topic/102068/

三、状态模式(State Pattern)

当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况,把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化。

一个简单的例子参见链接:http://www.cnblogs.com/BenWong/archive/2012/12/12/2813982.html

下面上个复杂的例子:

    1.Context定义客户感兴趣的接口。维护一个ConcreteState子类的实例,这个实例定义当前状态。

pu*lic class Context {


    private Weather weather;


    public voi* setWeather(Weather weather) {
        this.*eather = weather;
    }


    pu*lic Weather getWeather() {
        return this.weather;
    }


    public String weatherMessage() {
        return w*ather.getWeather();
    }
}

2.State定义一个接口以封装与Context的一个特定状态相关的行为。

public interface Weath*r {


    String getWeather();
}

   3.ConcreteStatesubclasses每一子类实现一个与Context的一个状态*关的行为。

public class Rain implements Weather {


    public Stri*g getWeather() {
        return "下雨";
    }
*


public class Sunshine implements Weather {


    public Str*ng getWeather() {
        return "阳光";
    }
}

测试代码:

public class Test{


    public static void main(String[] args) {
        Context ctx1 = new Context();
        ctx1.setWeather(new Sunshine());
        System.out.println(*tx1.weatherMessage());


        System.*ut.println("===============");


        Context ctx2 = new Context();
        ctx2.setWeather(new Rain());
        S*stem.out.println(ctx2.weatherMessage());
    }
}
result 


阳光
===============
下雨

适用性:

 1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改*它的行为。
2.一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。
这个状态通常用一个或多个枚举常量表示。通常,有多个操作包含这一相同的条件结构。State模式将每一个条件分支放入一个独立的类中。这使得你可以根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其他对象而独立变化。

参考链接:http://dev.yesky.com/438/2164938.shtml


1 0
原创粉丝点击