博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
十六、Mediator 仲载者设计模式
阅读量:6420 次
发布时间:2019-06-23

本文共 4602 字,大约阅读时间需要 15 分钟。

原理:

代码清单:

Mediator

public interface Mediator {     void createColleagues();     void colleagueChanged();}

Colleague

public interface Colleague {    void setMediator(Mediator mediator);    void setColleagueEnabled(boolean enabled);}

ColleagueButton

public class ColleagueButton extends Button implements  Colleague{    private Mediator mediator;    public ColleagueButton(String caption){        super(caption);    }    @Override    public void setMediator(Mediator mediator) {        this.mediator = mediator;    }    @Override    public void setColleagueEnabled(boolean enabled) {        setEnabled(enabled);    }}

ColleagueCheckbox

public class ColleagueCheckbox extends Checkbox implements ItemListener,Colleague {    private Mediator mediator;    public ColleagueCheckbox(String caption,CheckboxGroup group,boolean state){        super(caption,group,state);    }    @Override    public void setMediator(Mediator mediator) {        this.mediator = mediator;    }    @Override    public void setColleagueEnabled(boolean enabled) {        setEnabled(enabled);    }    @Override    public void itemStateChanged(ItemEvent e) {        mediator.colleagueChanged();    }}

ColleagueTextField

public class ColleagueTextField extends TextField implements TextListener,Colleague{    private Mediator mediator;    public ColleagueTextField(String text,int columns){        super(text,columns);    }    @Override    public void setMediator(Mediator mediator) {        this.mediator = mediator;    }    @Override    public void setColleagueEnabled(boolean enabled) {        setEnabled(enabled);        setBackground(enabled? Color.WHITE:Color.lightGray);    }    @Override    public void textValueChanged(TextEvent e) {        mediator.colleagueChanged();    }}

LoginFrame

public class LoginFrame extends Frame implements ActionListener,Mediator{    private ColleagueCheckbox checkGuest;    private ColleagueCheckbox checkLogin;    private ColleagueTextField textUser;    private ColleagueTextField textPass;    private ColleagueButton buttonOk;    private ColleagueButton buttonCancel;    public LoginFrame(String title){        super(title);        setBackground(Color.lightGray);        setLayout(new GridLayout(4,2));        createColleagues();        add(checkGuest);        add(checkLogin);        add(new Label("Username:"));        add(textUser);        add(new Label("Password:"));        add(textPass);        add(buttonOk);        add(buttonCancel);        colleagueChanged();        pack();        show();    }    @Override    public void createColleagues() {        CheckboxGroup g = new CheckboxGroup();        checkGuest = new ColleagueCheckbox("Guest",g,true);        checkLogin = new ColleagueCheckbox("Login",g,false);        textUser = new ColleagueTextField("",10);        textPass = new ColleagueTextField("",10);        textPass.setEchoChar('*');        buttonOk = new ColleagueButton("OK");        buttonCancel = new ColleagueButton("Cancel");        checkGuest.setMediator(this);        checkLogin.setMediator(this);        textUser.setMediator(this);        textPass.setMediator(this);        buttonOk.setMediator(this);        buttonCancel.setMediator(this);        checkGuest.addItemListener(checkGuest);        checkLogin.addItemListener(checkLogin);        textUser.addTextListener(textUser);        textPass.addTextListener(textPass);        buttonOk.addActionListener(this);        buttonCancel.addActionListener(this);    }    @Override    public void colleagueChanged() {        if(checkGuest.getState()){            textUser.setColleagueEnabled(false);            textPass.setColleagueEnabled(false);            buttonOk.setColleagueEnabled(true);        }else {            textUser.setColleagueEnabled(true);            userpassChanged();        }    }    private  void userpassChanged(){        if(textUser.getText().length()>0){            textPass.setColleagueEnabled(true);            if(textPass.getText().length()>0){                buttonOk.setColleagueEnabled(true);            }else{                buttonOk.setColleagueEnabled(false);            }        }else {            textPass.setColleagueEnabled(false);            buttonOk.setColleagueEnabled(false);        }    }    @Override    public void actionPerformed(ActionEvent e) {        System.out.println(e.toString());        System.exit(0);    }}

Main:

public class Main {    public static void main(String[] args){        new LoginFrame("Mediator Sample");    }}

结果:

1.Guest 不允许输入账户

2.Login 只有输入username 才可以输入password

3.Login 只有username 与 password 输全才可以 OK

转载于:https://www.cnblogs.com/baizhuang/p/10516557.html

你可能感兴趣的文章
ef core 数据迁移命令
查看>>
dedecms--二次开发之会员帐号过期无法登录
查看>>
四则运算
查看>>
uva 10269(floyd+Dijkstra)
查看>>
Codeforces Round #230 (Div. 1) 解题报告
查看>>
WCF 中wsHttpBinding配置实例程序
查看>>
Android Matrix
查看>>
438. Find All Anagrams in a String - Easy
查看>>
vue路由的优化
查看>>
C++ 运算符重载
查看>>
myclipse里有感叹号的问题,希望可以帮到各位
查看>>
OneZero第四周第二次站立会议(2016.4.12)
查看>>
iphone 如何获取iphone的硬件版本以及系统信息
查看>>
回收站参数介绍
查看>>
读书笔记
查看>>
node.js之内存机制特性
查看>>
bzoj3355[Usaco2004 Jan]有序奶牛*
查看>>
eclipse修改jdk后版本冲突问题
查看>>
微信小程序开发工具快捷键
查看>>
html加载时事件触发顺序
查看>>