东南大学Java设计模式作业观察者模式.doc
实例一:猫、狗与老鼠 假设猫是老鼠和狗的观察目标,老鼠和狗是观察者,猫叫老鼠跑,狗也跟着叫,使用观察者模式描述该过程。类图:代码:import java.util.*;public abstract class MySubject protected ArrayList observers = new ArrayList();public abstract void attach(MyObserver obs);public abstract void detach(MyObserver obs);public abstract void cry(); public class Cat extends MySubjectpublic void attach(MyObserver obs)obs.add(obs);public void detach(MyObserver obs)obs.remove(obs);public void cry()for(Object obs:obs)(MyObserver)obs).response(); public interface MyObserverpublic void response(); public class Mousepublic void response()/具体更新代码 public class Dogpublic void response()/具体更新代码 实例二:自定义登录控件 Java事件处理模型中应用了观察者模式,下面通过一个实例来学习如何自定义Java控件,并给该控件增加相应的事件。该实例基于Java Swing/AWT控件,在Swing/AWT的相关类中封装了对事件的底层处理。 类图:代码:import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.util.EventObject;import java.util.EventListener;public class LoginBean extends JPanel implements ActionListenerprivate LoginEventListener lel; private LoginEvent le;public LoginBean()/详细代码public void addLoginEventListener(LoginEventListener lel)this.lel=lel;private void fireLoginEvent(Object object,String userName,String password)le=new LoginEvent(btnLogin,userName,password);lel.validateLogin(le);public void actionPerformed(ActionEvent event)if(btnLogin=event.getSource()String userName=this.txtUserName.getText();String password=this.txtPassword.getText();fireLoginEvent(btnLogin,userName,password);if(btnClear=event.getSource()this.txtUserName.setText("");this.txtPassword.setText("");public class LoginEvent extends EventObjectprivate String userName;private String password;public LoginEvent(Object source,String userName,String password)super(source);this.userName=userName;this.password=password;public void setUserName(String userName)this.userName=userName;public String getUserName()return this.userName;public void setPassword(String password)this.password=password;public String getPassword()return this.password;public interface LoginEventListener extends EventListenerpublic void validateLogin(LoginEvent event);public class LoginValidatorA extends JFrame implements LoginEventListenerprivate LoginBean lb;public LoginValidatorA()super("Bank of China");p=new JPanel();this.getContentPane().add(p);lb=new LoginBean();lb.addLoginEventListener(this);public void validateLogin(LoginEvent event)/详细代码public class LoginValidatorB extends JFrame implements LoginEventListenerprivate LoginBean lb;public LoginValidatorB()super("China Mobile");p=new JPanel();this.getContentPane().add(p);lb=new LoginBean();lb.addLoginEventListener(this);public void validateLogin(LoginEvent event)String userName=event.getUserName();String password=event.getPassword();实例三 老师、学生、PPT代码:import java.util.*;public abstract class MySubject protected ArrayList observers = new ArrayList();public abstract void attach(MyObserver obs);public abstract void detach(MyObserver obs);public abstract void cry(); public class PPT extends MySubjectpublic void attach(MyObserver obs)obs.add(obs);public void detach(MyObserver obs)obs.remove(obs);public void look()for(Object obs:obs)(MyObserver)obs).response(); public interface MyObserverpublic void response(); public class Teacherpublic void response()/具体更新代码 public class Studentpublic void response()/具体更新代码