设计模式练习(15)——中介者模式

中介者模式

一、题目:

某软件公司需要开发一套图形用户界面类库,该类库包含若干个预定义的窗格对象,例如TextPane,ListPane,GraphicPane,窗口之间不允许直接调用,具有该类库的应用由一个包含一组窗格的窗口(Window)组成,窗口需要协调窗格之间的行为,采用中介者模式收集系统
(1)绘制中介者模式结构视图
(2)给出实例类图并实现代码。

二、所用模式结构视图:

设计模式练习(15)——中介者模式_第1张图片

三、实例类图:

设计模式练习(15)——中介者模式_第2张图片

四、实例实现代码:

(因为区分,所以在类的前面加了Gj19)

抽象中介者 -窗口

package gjMediatorPattern;

/**
 * 抽象中介者 -窗口
 * @author gongjian
 *
 */
public abstract class Gj19Window {
    public abstract void doSomeThing(Gj19Pane pane,String order);
}

具体中介者

package gjMediatorPattern;
/**
 * 具体中介者 
 * @author gongjian
 */
public class Gj19ConcreteWindow extends Gj19Window{
    private Gj19GraphicPane graphicPane;
private Gj19ListPane listPane;
    private Gj19TextPane textPane;

    public Gj19GraphicPane getGraphicPane() {
        return graphicPane;
    }
    public void setGraphicPane(Gj19GraphicPane graphicPane) {
        this.graphicPane = graphicPane;
    }
    public Gj19ListPane getListPane() {
        return listPane;
    }
    public void setListPane(Gj19ListPane listPane) {
        this.listPane = listPane;
    }
    public Gj19TextPane getTextPane() {
        return textPane;
    }
    public void setTextPane(Gj19TextPane textPane) {
        this.textPane = textPane;
    }

    public Gj19ConcreteWindow(Gj19GraphicPane graphicPane,
            Gj19ListPane listPane,Gj19TextPane textPane) {
        this.graphicPane = graphicPane;
        this.listPane = listPane;
        this.textPane = textPane;
    }
    @Override
    public void doSomeThing(Gj19Pane pane, String order) {
        pane.update(order);
    }
}

抽象同事类-窗口格

package gjMediatorPattern;

/**
 * 抽象同事类-窗口格
 * 
 * @author gongjian
 *
 */
public abstract class Gj19Pane {

    private Gj19Window window;



    public Gj19Window getWindow() {
        return window;
    }



    public void setWindow(Gj19Window window) {
        this.window = window;
    }



    public abstract void update(String order);

}

具体同事类

package gjMediatorPattern;
/**
 * 具体同事类
 * @author gong
 *
 */
public class Gj19TextPane extends Gj19Pane{


    @Override
    public void update(String order) {
        System.out.println("TextPane接收到指令"+order);
    }

}

具体同事类

package gjMediatorPattern;
/**
 * 具体同事类
 * @author gong
 *
 */
public class Gj19GraphicPane extends Gj19Pane{


    @Override
    public void update(String order) {
        System.out.println("GraphicPane接收到指令"+order);
    }


}

具体同事类

package gjMediatorPattern;
/**
 * 具体同事类
 * @author gong
 *
 */
public class Gj19ListPane extends Gj19Pane{

    @Override
    public void update(String order) {
        System.out.println("ListPane接收到指令"+order);
    }

}

中介者模式客户端

package gjMediatorPattern;
/**
 * 中介者模式客户端
 * @author gong
 *
 */
public class Client {

    public static void main(String[] args) {

        Gj19ListPane gj19ListPane =new Gj19ListPane();
        Gj19TextPane gj19TextPane = new Gj19TextPane();
        Gj19GraphicPane gj19GraphicPane = new Gj19GraphicPane();
        Gj19ConcreteWindow concreteWindow = new Gj19ConcreteWindow(gj19GraphicPane,gj19ListPane, gj19TextPane);
        System.out.println("============系统发出请求===============");
        concreteWindow.doSomeThing(gj19GraphicPane, "图像放大1.2倍");
        concreteWindow.doSomeThing(gj19TextPane, "文本缩小到原来的0.8倍");
        concreteWindow.doSomeThing(gj19ListPane, "列表左移10px");
        System.out.println("=============系统结束===================");
    }

}

五、运行结果:

设计模式练习(15)——中介者模式_第3张图片

你可能感兴趣的:(设计模式)