Analysis on the observer mode of bank processing business

Source: Internet
Author: User

Project Link: https://github.com/stackisok/Design-Pattern/tree/master/src/observer

Project Background: Go to the bank to handle the business. When there are many people, we need to wait in order. It is not the bank's turn to handle the issue until it calls its account.

Observer Mode

  The observer mode (also known as the Model-View Mode, the source-listener mode, or the dependent mode) is a software design mode. In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes. This is usually done by calling the methods provided by various observers. This mode is usually used to implement the event processing system.

In short, the observer mode defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and automatically updated.

 

 

Structure 

The observer pattern structure usually includes two inherited hierarchies: The observation object and the observer. The structure is as follows:

 

 

The observer mode defines four roles: Abstract topic, specific topic, abstract observer, and specific observer.

Subject (target): A target is also called a topic. It refers to the object to be observed. An observer set is defined in the target. An observation target can be observed by any number of observers. It provides a series of methods to add and delete observer objects, it also defines the notification method notify (). The target class can be an interface, an abstract class, or a specific class.
Concretesubject (target): The specific target is a subclass of the target class. Generally, it contains frequently changed data. When its status changes, it sends a notification to its observers; it also implements the abstract business logic methods defined in the target class (if any ). If you do not need to extend the target class, the specific target class can be omitted.
Observer (observer): The observer will respond to changes to the observed object. The observer is generally defined as an interface, which declares the method Update () for updating data. Therefore, it is also called an abstract observer.
Concreteobserver (Specific observer): Maintain a reference pointing to a specific target object in a specific observer, which stores the relevant statuses of the specific observer. These statuses must be consistent with those of the specific target; it implements the update () method defined in the abstract observer. Generally, you can call the attach () method of the target class to add yourself to the collection of the target class or delete yourself from the collection of the target class by using the detach () method.

 

 

Mode Parsing

 This project defines the abstract observer interface (observer). Click to view the GitHub source code.

Package observer;/***** @ author chenjunwang * @ Description: Observer interface * @ Date: created in 16:10 2018/4/11 * @ modified: **/public interface observer {void Update (string MSG );}

 

 

The specific observer class (client) is as follows. Click to view the GitHub source code.

 

 

Public class client implements observer {string callno; Public client (string callno) {This. callno = callno;} @ override public void Update (string MSG) {If (MSG. equals (this. callno) {system. out. println ("I am a" + callno + "customer, now it's me! ");} Else {system. Out. println (" I am a "+ callno +" customer. I haven't arrived yet! ");}}}

 

 

Define the abstract topic interface (subject) and click to view the GitHub source code

 

Public interface subject {// register the observer void registerobserver (Observer observer); // remove the observer void removeobserver (Observer observer); // notify the observer void notifyobserver (string MSG );}

 

Define the specific theme (bankcallcenter) class, click to view the GitHub source code

 

public class BankCallCenter implements Subject {    List<Observer> list = new ArrayList<>();    @Override    public void registerObserver(Observer observer) {        list.add(observer);    }    @Override    public void removeObserver(Observer observer) {        list.remove(observer);    }    @Override    public void notifyObserver(String msg) {        for (Observer item : list)            item.update(msg);    }}

 

Run the test result. Click to view the source code.

 

Public class test {public static void main (string [] ARGs) {bankcallcenter = new bankcallcenter (); client Client client = new client ("1"); bankcallcenter. registerobserver (client); client Client2 = new client ("2"); bankcallcenter. registerobserver (Client2); client client3 = new client ("3"); bankcallcenter. registerobserver (client3); system. out. println ("---------- center is called No. 1 to handle business ----------"); bankcallcenter. notifyobserver ("1"); system. out. println ("---------- remove 1 ------------" from the observer list); bankcallcenter. removeobserver (client); system. out. println ("----------- Center Number 2 to handle business -------------"); bankcallcenter. notifyobserver ("2"); system. out. println ("------------ Remove 2nd -----------");}

 

Bank call projects are jointly constructed with four categories, two of which are the observer interface and the observer interface, and the other two are the implementation class, one is the bank call center and the customer who has obtained the number. The following figure shows the structure of the observer mode by creating a UML diagram for a bank call Project:

 

 

Advantages and disadvantages

Benefits:

1. The observer uses the notifyobserver group to send messages. The bank call project processes the received messages in the update method. If a useful message is received, the client performs the operation.

2. The observer mode is suitable for a software system that requires that some other objects change when the state of an object changes. The observer mode reduces the coupling between objects and facilitates system reuse.

3. When sending a call notification, the Bank project owner does not need to specify a specific customer. The observer can decide whether to subscribe to the notification from the bankcallcenter subject.

Disadvantages:

1. If a bankcenter subject is subscribed to by a large number of client observers, it may be efficient during call notification.
2. I have to say that the observer mode makes the bank call project code relationship complex.

Analysis on the observer mode of bank processing business

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.