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 MySubject {
public 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 MyObserver {
public void response(); }
public class Teacher {
public void response() { //具体更新代码 } }
public class Student {
public void response() { //具体更新代码 } }