利用JDK方式和GuavaAPI方式实现观察者模式

Posted ningjj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用JDK方式和GuavaAPI方式实现观察者模式相关的知识,希望对你有一定的参考价值。

1、JDK方法实现案例需求:

去餐厅吃饭有时候需要排队,进行排队叫号。假如所有等待的人都是观察者,叫号系统就是一个被监听的主题对象。
当叫号系统发出叫号通知后,所有等待的人,都会收到通知,然后检查自己的号码是否匹配。匹配的话,就开始就餐,
不匹配的,继续等待

package com.gupao.jay.pattern.observer.jdk;

/**
 * @Author JAY
 * @Date 2019/6/2 8:41
 * @Description 排队的号码
 **/
public class NumberModel 

    private int number;
    private String deskType;

    public NumberModel(int number, String deskType) 
        this.number = number;
        this.deskType = deskType;
    

    public int getNumber() 
        return number;
    

    public void setNumber(int number) 
        this.number = number;
    

    public String getDeskType() 
        return deskType;
    

    public void setDeskType(String deskType) 
        this.deskType = deskType;
    

    @Override
    public String toString() 
        return "NumberModel" +
                "number=" + number +
                ", deskType=‘" + deskType + ‘\‘‘ +
                ‘‘;
    
 1 package com.gupao.jay.pattern.observer.jdk;
 2 
 3 import java.util.Observable;
 4 import java.util.Observer;
 5 
 6 /**
 7  * @Author JAY
 8  * @Date 2019/6/2 8:40
 9  * @Description 等待的顾客
10  **/
11 public class Consumer implements Observer 
12 
13     public Consumer(int number) 
14         this.number = number;
15     
16 
17     private int number;
18 
19     public int getNumber() 
20         return number;
21     
22 
23     public void setNumber(int number) 
24         this.number = number;
25     
26 
27     @Override
28     public void update(Observable o, Object arg) 
29         NumberModel numberModel = (NumberModel) arg;
30         if (numberModel.getNumber() != this.number)
31             System.out.println("号码不匹配,继续等待。。。。");
32             return;
33         
34         System.out.println("排到我们了,前往" + numberModel.getDeskType() + "用餐");
35     
36 
package com.gupao.jay.pattern.observer.jdk;

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.locks.Lock;

/**
 * @Author JAY
 * @Date 2019/6/2 8:42
 * @Description 叫号系统
 **/
public class CallNumberSystem extends Observable 

    private CallNumberSystem()

    private static CallNumberSystem callNumberSystem = null;

    public static CallNumberSystem getInstance()
        if (callNumberSystem == null)
            synchronized (CallNumberSystem.class)
                if (callNumberSystem == null)
                    callNumberSystem = new CallNumberSystem();
                
            
        
        return callNumberSystem;
    

    public void notifyNumber(NumberModel numberModel)
        System.out.println("系统播报:请" + numberModel.getNumber() + "号顾客到" + numberModel.getDeskType() + "就餐!");
        setChanged();
        notifyObservers(numberModel);
    
package com.gupao.jay.pattern.observer.jdk;

/**
 * @Author JAY
 * @Date 2019/6/2 8:55
 * @Description TODO
 **/
public class CallNumberSystemTest 

    public static void main(String[] args) 
        Consumer consumerA = new Consumer(10001);
        Consumer consumerB = new Consumer(10002);
        Consumer consumerC = new Consumer(10003);

        CallNumberSystem instance = CallNumberSystem.getInstance();
        instance.addObserver(consumerA);
        instance.addObserver(consumerB);
        instance.addObserver(consumerC);

        instance.notifyNumber(new NumberModel(10002,"小桌A11号"));
        
    

 

 

 

2、guavaAPI实现案例需求:模拟在社区中,学生向老师提问。

 

 1 package com.gupao.jay.pattern.observer.guava;
 2 
 3 /**
 4  * @Author JAY
 5  * @Date 2019/6/2 9:11
 6  * @Description 问题
 7  **/
 8 public class Question 
 9 
10     private String userName;
11     private String content;
12 
13     public String getUserName() 
14         return userName;
15     
16 
17     public void setUserName(String userName) 
18         this.userName = userName;
19     
20 
21     public String getContent() 
22         return content;
23     
24 
25     public void setContent(String content) 
26         this.content = content;
27     
28 

 

package com.gupao.jay.pattern.observer.guava;

import com.google.common.eventbus.Subscribe;

/**
 * @Author JAY
 * @Date 2019/6/2 9:05
 * @Description TODO
 **/
public class Teacher 

    private String name;

    public Teacher(String name) 
        this.name = name;
    

    @Subscribe
    public void subscribe(Question question)
    //业务逻辑
        System.out.println(this.name + "老师,学生" + question.getUserName() + "向你提了一个问题,问题的内容是:" + question.getContent());
    

  

 1 package com.gupao.jay.pattern.observer.guava;
 2 
 3 import com.google.common.eventbus.EventBus;
 4 
 5 /**
 6  * @Author JAY
 7  * @Date 2019/6/2 9:11
 8  * @Description TODO
 9  **/
10 public class GuavaTest 
11     public static void main(String[] args) 
12         Question question = new Question();
13         question.setUserName("JayNing");
14         question.setContent("如何阅读spring源码?");
15 
16         Teacher tom = new Teacher("Tom");
17         Teacher mic = new Teacher("Mic");
18 
19         EventBus eventBus = new EventBus();
20         eventBus.register(tom);
21         eventBus.register(mic);
22         eventBus.post(question);
23     
24 

 


 

以上是关于利用JDK方式和GuavaAPI方式实现观察者模式的主要内容,如果未能解决你的问题,请参考以下文章

利用Java提供的Observer接口和Observable类实现观察者模式

代理模式:利用JDK原生动态实现AOP

设计模式

代理模式-利用JDK原生动态实现AOP

牛逼啊!异步神器:CompletableFuture实现原理和使用场景

SOFA 源码分析 — 调用方式