• Observer Mode introduction

  • Business applications

  • discuss

The observer pattern is the behavior pattern of an object, also known as publish-subscribe, model-view, source-listener, or slave. The observer pattern defines a one-to-many dependency that allows multiple observer objects to listen on a subject object simultaneously. When the topic object changes in its state, all observer objects are notified so that they can update themselves automatically. (Explain references in -Java and patterns)

The client

package Observer; /* * @auther * @mail [email protected] * @date 2019-12-17 14:56 * @notify Class Client {// If in a Spring project, we can register subject with the container. The current practice only wants to indicate initialization once. static OrderSubject subject = null; static { subject = new OrderSubject(); subject.attach(new ChangeAccount()); subject.attach(new ChangeInventory()); } public static void main(String[] args) {Order Order = new Order(" 三", "三"); // Call all listeners, executing their respective operations subjy.notifyobservers (order); }}Copy the code

View Code

Business pojo

package Observer; /* * @date 2019-12-17 14:45 * @notify Used to simulate order information * @version 1.0 */ public class Order { public Order(String userId, String goodsId) { this.userId = userId; this.goodsId = goodsId; } // userId private String userId; // Commodity ID private String goodsId; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this.goodsId = goodsId; }}Copy the code

View Code

Subscribe to the topic

package Observer; import java.util.ArrayList; import java.util.List; /* * @auther * @mail [email protected] * @date 2019-12-17 10:49 * @notify Order subject * @version 1.0 */ public class OrderSubject { private List<Observer> observersList = new ArrayList<>(); Public void attach(Observer Observer) {observersList.add(Observer); // Call this method to register a new Observer object. } public void detach(Observer Observer) {observersList.remove(Observer); } // Call this method to notify all registered observers of public void notifyObservers(Order Order) {for (Observer Observer: observersList) { observer.excuse(order); }}}Copy the code

View Code

Observer interface

package Observer; /* * @date 2019-12-17 10:56 * @notify * @version 1.0 */ public Interface Observer {// Perform specific business void excuse(Order Order); }Copy the code

View Code

Account balance business observer implementation

package Observer; /* * @date 2019-12-17 14:51 * @notify change account balance * @version 1.0 */ public Class ChangeAccount implements Observer {public void excuse(Order Order) {String userId = order.getUserId();  System.out.println(" changing user: + userId + "account balance "); System.out.println(" account balance changed successfully "); }}Copy the code

View Code

Commodity inventory business observer implementation

 

package Observer; /* * @date 2019-12-17 14:51 * @notify * @version 1.0 */ public Class ChangeInventory implements Observer {// Implement specific business public void Excuse (Order Order) {String goodsId = order.getGoodsId(); System.out.println(" changing goods: "+ goodsId +" stock quantity "); System.out.println(" Inventory changed successfully "); }}Copy the code

View Code

 

 

When writing CRUD, you will often encounter the above business situation. When an object changes, it is often necessary to modify several other objects associated with it, and it is more accurate to modify other tables. In the above example, generally speaking, we call the debit interface and the inventory interface in turn to carry out corresponding operations after the user succeeds in payment. This actually increases the coupling between the payment business and the debit business and the inventory business. With the Observer model, the payment business does not need to know what the user needs to do once the payment is successful. It just needs to find the topic and publish the message. Second, we can put different businesses into different subscription groups, with no correlation between observers or between subscription groups. \

The disadvantages of the observer mode are also obvious. When an active object has multiple observers, multiple observers are executed in a loop. If one observer fails, the whole ecology may collapse.