Observer mode

Observer mode means that the state of an object changes, and the corresponding object will act accordingly

Algorithm framework

The main roles of the algorithm are: client, subject and observer; A list of observers is maintained in the topic. The client maintains the observers into the topic. When the data of the topic changes, all observers are notified to perform corresponding actions

Algorithm example


As shown in the figure above, when the RMB exchange rate changes, the import and export companies will make corresponding profits and losses; The corresponding relationship here is as follows:

  • Client: RateClient
  • Subject: rmrate
  • Observers: ImportCompany, ExportCompany
    First define the observer interface
public interface ICompany {
    void update(BigDecimal change);
}

Then define two implementation classes as real observers

public class ImportCompany implements ICompany {
    private String name;

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

    @Override
    public void update(BigDecimal change) {
        BigDecimal zero = new BigDecimal(0);
        if (change.compareTo(zero) > 0) {
            System.out.println("RMB exchange rate rise" + change + "," + name + "The import company made money");
        } else {
            System.out.println("The RMB exchange rate fell" + change + "," + name + ",The import company lost money");
        }
    }
}
public class ExportCompany implements ICompany {
    private String name;

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

    @Override
    public void update(BigDecimal change) {
        BigDecimal zero = new BigDecimal(0);
        if (change.compareTo(zero) > 0) {
            System.out.println("RMB exchange rate rise" + change + "," + name + "The export company lost money");
        } else {
            System.out.println("The RMB exchange rate fell" + change + "," + name + "Export companies are making money");
        }
    }
}

Abstract exchange rate class

public abstract class AbstractRate {
    protected List<ICompany> companyList = new ArrayList<>();
    private BigDecimal base = new BigDecimal("6.8");

    void addCompany(ICompany company) {
        this.companyList.add(company);
    }

    void removeCompany(ICompany company) {
        this.companyList.remove(company);
    }

    abstract void change(BigDecimal value);

    public BigDecimal getBase() {
        return this.base;
    }
}

RMB integrates this abstract class

public class RmbRate extends AbstractRate {
    @Override
    void change(BigDecimal value) {
        System.out.println("The RMB exchange rate has changed.......");
        BigDecimal diff = value.subtract(getBase());
        for (ICompany company:companyList) {
            company.update(diff);
        }
    }
}

Client definition

public class RateClient {
    public static void main(String[] args ) {
        RmbRate rmbRate = new RmbRate();
        rmbRate.addCompany(new ExportCompany("tencent"));
        rmbRate.addCompany(new ExportCompany("Alibaba"));
        rmbRate.addCompany(new ExportCompany("Meituan"));
        rmbRate.change(new BigDecimal("6.9"));
        rmbRate.change(new BigDecimal("6.0"));
        rmbRate.change(new BigDecimal("6.98"));
        rmbRate.change(new BigDecimal("8.9"));
    }
}

Operation results

Differences among observer mode, subscription mode and listener mode

Differences among observer mode, subscription mode and listener mode

  • Observer mode: it is a bit similar to the mode of wechat circle of friends. When you publish a circle of friends dynamic, your contact will see your newly published article. The key here is that you have a contact list, at least you know the contact ID.

  • Subscription mode: subscribers and publishers are weakly related, and even publishers do not need to know the existence of subscribers, which is essentially different from the observer mode. The coupling of subscription mode is low, but the quality of communication is poor, and there is no need to ensure the quality of communication.

  • Listener mode: it is actually the implementation of two observer modes. It is used to decouple the observer and the observed in the observer mode. It is equivalent to adding a messenger in the middle to decouple business logic or code logic.

Observer mode is generally used for in-process communication, while subscription mode can be used across processes.

Reference link:
Detailed explanation of Observer mode

Observer mode and listener

Listener mode and observer mode

Keywords: Design Pattern

Added by kcgame on Thu, 10 Mar 2022 17:16:12 +0200