行为变化模式

来源:互联网 发布:蒙特卡洛算法matlab 编辑:程序博客网 时间:2024/06/06 01:51

行为变化模式

        在组件的构建过程中,组件行为的变化经常导致组件本身剧烈的变化。“行为变化”模式将组件的行为和组件本身进行解耦,从而支持组件行为的变化,实现两者之间的解耦。

一、Command模式

        在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合(比如需要对行为进行记录、撤销、重复、事务等处理),这种无法抵御变化的紧耦合是不适合的。我们要将一组行为抽象为对象,可以实现二者之间的松耦合。
        定义:将一个请求(行为)封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
实例代码:
public interface Command {    void execute();}public class CutCommand implements Command {    String arg;    public CutCommand(String arg) {        this.arg = arg;    }    @Override    public void execute() {        System.out.print("#cut process..." + arg + "\n");    }}public class CopyCommand implements Command {    String arg;    public CopyCommand(String arg) {        this.arg = arg;    }    @Override    public void execute() {        System.out.print("#copy process..." + arg + "\n");    }}public class PasteCommand implements Command {    String arg;    public PasteCommand(String arg) {        this.arg = arg;    }    @Override    public void execute() {        System.out.print("#paste process..." + arg + "\n");    }}public class DeleteCommand implements Command {    String arg;    public DeleteCommand(String arg) {        this.arg = arg;    }    @Override    public void execute() {        System.out.print("#delete process..." + arg + "\n");    }}public class MacroCommand implements Command {    Vector<Command> commands=new Vector<Command>();    public void addCommand(Command c) {        commands.add(c);    }    @Override    public void execute() {        for (Command c : commands) {            c.execute();        }    }}public class ClientApp{    public static void main(String args[]) {        CutCommand command1=new CutCommand("Arg ###");        CopyCommand command2=new CopyCommand("Arg $$$");        MacroCommand macro=new MacroCommand();        macro.addCommand(command1);        macro.addCommand(command2);        macro.execute();    }}
        实现Command接口的具体命令对象ConcreteCommand有时候根据需要可能会保存一些额外的状态信息。通过使用Composite模式,可以将多个命令封装为一个“复合命令”MacroCommand;

二、Visitor访问模式

        在软件构建过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法),如果直接在基类中这样的更改,将会给子类带来很繁重的变更负担,甚至破坏原有设计。Visitor模式可以在不更改类层次结构的前提下,在运行时根据需要透明地为类层次结构上的各个类动态添加新的操作。
        定义:表示一个作用于某对象结构中的各元素的操作。使得可以在不改变(稳定)各元素的类的前提下定义(扩展)作用于这些元素的新操作(变化)。
示例代码:
public abstract class Element{    //......    public abstract void accept(Visitor visitor);//第一次多态辨析}public class ElementA extends Element{    //......    @Override    public void accept(Visitor visitor) {        visitor.visitElementA(this);    }}public class ElementB extends Element{    @Override    public void accept(Visitor visitor) {        visitor.visitElementB(this);//第二次多态辨析    }}public abstract class Visitor{    public abstract void visitElementA(ElementA element);    public abstract void visitElementB(ElementB element);}//==================================//扩展1public class Visitor1 extends Visitor{    @Override    public void visitElementA(ElementA element) {        System.out.println("Visitor1 is processing ElementA");    }    @Override    public void visitElementB(ElementB element) {        System.out.println("Visitor1 is processing ElementB");    }}//扩展2public class Visitor2 extends Visitor{    @Override    public void visitElementA(ElementA element) {        System.out.println("Visitor2 is processing ElementA");    }    @Override    public void visitElementB(ElementB element) {        System.out.println("Visitor2 is processing ElementB");    }}public class ClientApp{    public static void main(String args[]) {        Visitor2 visitor=new Visitor2();        ElementB elementB=new ElementB();        elementB.accept(visitor);// double dispatch        ElementA elementA=new ElementA();        elementA.accept(visitor);    }}
        ●  Visitor模式通过双重分发(double dispatch)来实现在不更改(不添加新的操作-编译时)Element类层次结构的前提下,在运行时透明地为类层次结构上的各个类动态添加新的操作(支持变化);
        ●  所谓双重分发即Visitor模式中间包括两个多态分发(注意其中的多态机制):第一个为accept方法的多态辨析;第二个为visitElementX方法的多态辨析;
        ●  Visitor模式的最大确定在于扩展类层次结构(增添新的Element子类),会导致Visitor类的改变。因此Vistor模式适用于“Element类层次结构稳定,而其中的操作却经常面临频繁改动”。
0 0
原创粉丝点击