设计模式之观察者设计模式

本文通过三个实例详细介绍了事件监听机制的实现方式,包括事件源、事件类和监听器的概念及其实现过程。案例覆盖学生行为监听、窗体事件监听及Spring框架启动时的监听等应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

▶ 事件源:就是被监听的对象,
▶ 事件类:里面封装了事件源对象,
▶ 监听器:里面绑定了事件类,在监听器里面拿到事件类对象,就可以监听事件源中的行为(调用了什么方法)

案例一

    //监听器
    public interface StudentListener {
        void preStudy(StudentEvent e);
        void preSleep(StudentEvent e);
    }
public class Student {
    private String name;
    //绑定监听器
    private StudentListener listener;
    public Student(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void study(){
        if(listener!=null){
            listener.preStudy(new StudentEvent(this));
        }
        System.out.println(name + "在学习");
    }

    public void sleep(){
        if(listener!=null){
            listener.preSleep(new StudentEvent(this));
        }
        System.out.println(name + "要睡觉");
    }

    public void addStudentListener(StudentListener listener){
        this.listener = listener;
    }
}
   //封装事件源对象:student事件
    public class StudentEvent {
        private Object source;
        public StudentEvent(Object source){
            this.source = source;
        }

        public Object getSource(){
            return source;
        }
    }
   //demo
    public class StudentListenerDemo {
    public static void main(String[] args) {
        Student s = new Student("小明明");
        //添加监听器
        s.addStudentListener(new StudentListener() {

            @Override
            public void preStudy(StudentEvent e) {
                Student s = (Student) e.getSource();
                String name = s.getName();
                System.out.println(name + "喝杯牛奶吧");
            }

            @Override
            public void preSleep(StudentEvent e) {
                Student s = (Student) e.getSource();
                String name = s.getName();
                System.out.println(name + "睡个好觉哦");
            }
        });

        s.sleep();/*打印:小明明睡个好觉小明明要睡觉*/
        s.study();/*打印小明明喝杯牛奶吧小明明在学习*/
    }

}

案例二

    //典型的窗体事件:
    public class FrameDemo {
    public static void main(String[] args) {
        //事件源:出发事件的对象
        Frame f = new Frame("我的窗户");
        f.setSize(1000, 1000);
        f.setVisible(true);
        f.addWindowListener(new MyWindowListener());
      }
   }

    class MyWindowListener extends WindowAdapter{

        @Override
        public void windowClosed(WindowEvent e) {//WindowEvent封装了事件源对象,就是哪个对象发生了改变
            Frame f = (Frame) e.getSource();//拿到源对象
            f.dispose();//执行方法
        }

        @Override
        public void windowClosing(WindowEvent e) {
            Frame f = (Frame) e.getSource();
            f.dispose();
        }

    }

案例三

 //Spring的启动时监听,就是使用的监听,它监听的是ServletContextEvent
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
    public ContextLoaderListener() {
    }
    public ContextLoaderListener(WebApplicationContext context) {
        super(context);
    }   
    @Override
    public void contextInitialized(ServletContextEvent event) {
        initWebApplicationContext(event.getServletContext());
    }   
    @Override
    public void contextDestroyed(ServletContextEvent event) {
        closeWebApplicationContext(event.getServletContext());
        ContextCleanupListener.cleanupAttributes(event.getServletContext());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值