Design Patterns Observer Pattern Notes

illustrate

Record the writing method of learning design pattern-observer pattern. The JDK version used is version 1.8.

Observer

Intent : Define a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
Structure :
Insert image description here

in:

  • Subject knows its observers, and there can be any number of observers observing the same target; it provides an interface for registering and deleting observer objects.
  • Observer defines an update interface for objects that need to be notified when the target sends changes.
  • ConcreteSubject (specific target) stores the relevant status in each ConcreteObserver object; when its status changes, it sends a notification to each of its observers.
  • ConcreteObserver (specific observer) maintains a reference to the ConcreteSubject object; stores relevant states, which should be consistent with the state of the target; implements the Observer's update interface to make its own state consistent with the state of the target.

applicability:

  • When an abstract model has two aspects, one of which depends on the other, encapsulate the two in separate objects so that they can be changed and reused independently.
  • When changes to one object require changes to other objects at the same time, and it is not known how many objects need to be changed.
  • When one object must notify other objects, but it cannot assume who the other objects are, it does not want these objects to be tightly coupled.

Table of contents

Insert image description here

Observer pattern example class diagram

Insert image description here
Implement the Observer pattern example with this UML class diagram.

Abstract theme role class

package com.example.deesign_patterns.observer;

//抽象主题角色类
public interface Subject {
    
    

    //添加订阅者(添加观察者对象)
    void attach(Observer observer);

    //删除订阅者
    void detach(Observer observer);

    //通知订阅者更新消息
    void notify(String message);
}

abstract observer class

package com.example.deesign_patterns.observer;

//抽象观察者类
public interface Observer {
    
    

    void update(String message);
}

Specific theme role classes

package com.example.deesign_patterns.observer;

import java.util.ArrayList;
import java.util.List;

//具体主题角色类
public class SubscriptionSubject implements Subject{
    
    

    //定义一个集合用来存储多个观察者对象
    private List<Observer> weiXinUserList=new ArrayList<Observer>();

    @Override
    public void attach(Observer observer) {
    
    
        weiXinUserList.add(observer);
    }

    @Override
    public void detach(Observer observer) {
    
    
        weiXinUserList.remove(observer);
    }

    @Override
    public void notify(String message) {
    
    
        //遍历集合
        for(Observer observer:weiXinUserList){
    
    
            //调用观察者对象中的update方法
            observer.update(message);
        }
    }
}

Concrete observer role class

package com.example.deesign_patterns.observer;

//具体的观察者角色类
public class WeiXinUser implements Observer{
    
    

    private String name;

    public WeiXinUser(String name) {
    
    
        this.name = name;
    }

    @Override
    public void update(String message) {
    
    
        System.out.println(name+"-"+message);
    }
}

Test class

package com.example.deesign_patterns.observer;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建公众号对象
        SubscriptionSubject subject=new SubscriptionSubject();
        //订阅公众号
        subject.attach(new WeiXinUser("张三"));
        subject.attach(new WeiXinUser("李四"));
        subject.attach(new WeiXinUser("王五"));
        //公众号更新,发出消息给订阅者(观察者对象)
        subject.notify("微信内容的专栏更新了!");
    }
}

Insert image description here

benefit:

  • The coupling relationship between the target and the observer is reduced, and there is an abstract coupling relationship between the two.
  • The observed person sends a notification, and all registered observers will receive the information [a broadcast mechanism can be implemented].

shortcoming:

  • If there are many observers, it will take time for all observers to receive notifications sent by the observed.
  • If the observer has cyclic dependencies, then the notification sent by the observer will cause the observer to call in a loop, which will cause the system to crash.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131363012