16、Mediator 仲裁者模式

Mediator

Mediator

Mediator Mediator
Colleague Colleague
ColleagueButton  Colleague  ConcreteColleague
ColleagueTextField  Colleague  ConcreteColleague
ColleagueCheckbox  Colleague  ConcreteColleague
LoginFrame  Mediator  ConcreteMediator
Main  

Mediator.java

 package mediator;
 
 public interface Mediator {
     public abstract void createColleague();
     public abstract void colleagueChanged();
 }

Colleague.java

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

ColleagueButton.java

 package mediator;
 
 import java.awt.*;
 
 public class ColleagueButton extends Button implements Colleague {
     private Mediator mediator;
     public ColleagueButton(String caption) {
         super(caption);
    }
     public void setMediator(Mediator mediator) {
         this.mediator = mediator;
    }
     public void setColleagueEnabled(boolean enabled) {
         setEnabled(enabled);
    }
 }

ColleagueTextField.java

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

ColleagueCheckbox.java

 package mediator;
 
 import java.awt.*;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 
 public class ColleagueCheckbox extends Checkbox implements ItemListener, Colleague {
     private Mediator mediator;
     public ColleagueCheckbox(String caption, CheckboxGroup group, boolean state) {
         super(caption, group, state);
    }
     public void setMediator(Mediator mediator) {
         this.mediator = mediator;
    }
     public void setColleagueEnabled(boolean enabled) {
         setEnabled(enabled)
        setBackground(enabled ? Color.WHITE : Color.lightGray)
   
    public void itemStateChanged(ItemEvent e) 
        mediator.colleagueChanged()
   
}

LoginFrame.java

 package mediator;
 
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
 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;
 
     // 
     // Colleague
     public LoginFrame(String title) 
        super(title)
        setBackground(Color.lightGray)
        setLayout(new GridLayout(4, 2))
        createColleague()
        add(checkGuest)
        add(checkLogin)
        add(new Label("Username:"))
        add(textUser)
        add(new Label("Password:"))
        add(textPass)
        add(buttonOk)
        add(buttonCancel)
        colleagueChanged()
        pack()
        show()
   }  

    public void createColleague() 
        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)
   }  

    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)
       
   }  

    public void actionPerformed(ActionEvent e) 
        System.out.println(e.toString())
        System.exit(0)
   
}

Main.java

 

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

Mediator

  1. Mediator()

    Mediator Colleague

  2. ConcreteMediator()

    ConcreteMediator Mediator

  3. Colleague()

    Colleague Mediator

  4. ConcreteColleague()

    ConcreteColleague Colleague

Bug

ConcreteColleague ConcreteMediator

公众号发哥讲

这是一个稍偏基础和偏技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。

img

如果你觉得文章还不错,就请点击右上角选择发送给朋友或者转发到朋友圈~

● 扫码关注我们

据说看到好文章不推荐的人,服务器容易宕机!

本文版权归发哥讲博客园共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

 

 

posted @ 2020-08-06 15:11  发哥讲Java  阅读(170)  评论(0编辑  收藏  举报