《图解设计模式》 第七部分 简单化

Facade 模式

public class Main{
  public static void main(String[] args){
    PageMaker.makeWelcomePage("someone@email.com","welcom.html");
  }
}

public class PageMaker{
    public static void makeWelcompage(String mailaddr, String filename){
        try{
            Properties mailprop = Database.getProperties("maildata");
            String username = mailprop.getProperty(mailaddr);
            HtmlWriter writer = new HtmlWriter(new FileWriter(filename));
            writer.title("Welcome to " + username + "'s page!");
            writer.paragraph(username + "のページへようこそ。");
            writer.paragraph("メール待っていますね。");
            writer.mailto(mailaddr, username);
            writer.close();
            System.out.println(filename + " is created for " + mailaddr + " (" + username + ")");
        } catch (IOException e){
            e.printStackTrace();
        }
    }
  }

个人感觉这个模式没什么好讲的。
主要是为了通过一个类的方法,构建足够多的参数,将这些参数分发给需要的类。来完成任务。
其实工作中即使没学过这种设计模式,也会经常用到。了解即可

Mediator 模式

这个会经常用到,但是从没想过的设计模式。
通过一个 中间人 各个组件向中间人报告。然后中间人来下达指示。

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

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

public class ColleagueButton implements Colleague{
    private Mediator mediator;
    private boolean enabled;
    //指派中间人
    public void setMediator(Mediator mediator){
        this.mediator = mediator;
    }

    //听从 Mediator 控制
    public void setColleagueEnabled(boolean enabled){
        this.enabled = enabled;
    }
}

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

    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);
        }
    }
}
posted @ 2024-11-05 11:11  大俗XD  阅读(0)  评论(0编辑  收藏  举报