Observer mode _java of Java design pattern

Source: Internet
Author: User

The observer pattern, also known as the Publish-subscribe (Publish/subscribe) pattern, defines a one-to-many dependency that allows multiple observer objects to listen to one subject object at the same time. When the state changes, the subject object notifies all the observer objects so that they can automatically update themselves. One of the bad side effects of splitting a system into a series of interacting classes is the need to maintain consistency among related objects. We do not want to keep all kinds of tight coupling in order to maintain consistency, which can bring inconvenience to maintenance, extension and reuse. The work done by the observer pattern is essentially decoupling, allowing both sides of the coupling to rely on abstraction rather than on concrete.

The Observer pattern is a widely used pattern in practice, its application scenario, for example, a production of rice factory, and n sales of rice merchants, n Merchants in this factory first registered their own contact, when the factory produced a certain amount of rice, and then contact the way to notify the N merchants to pick up the goods. This example uses the registration (Attach) and notification (NOTIFY) in the Observer pattern, which notifies each observer in turn when the notification's state changes.

Subject is an abstract observer, and Observer is an abstract watcher. If you are creating a derived class that is not a distinct object, consider using an interface to implement several of the same methods.

The Java code is as follows:

Abstract class Subject {private arraylist<observer> observerslist = new arraylist<observer> ();
  Add observers public void Attach (Observer ob) {observerslist.add (OB);
  }//Remove observers public void Detach (Observer ob) {observerslist.remove (OB); public void Notify () {for (Observer ob:observerslist) {ob.
    Update ();

}} abstract class Observer {public abstract void Update ();}
  public class Concreteobserver extends Observer {private String name;
  Private String Observerstatus;
  
  Private ConcreteSubject subject;
    Public Concreteobserver (concretesubject subject, String name) {this.subject = subject;
  THIS.name = name;
  public void Update () {observerstatus = Subject.getsubjectstatus ();

  } public class ConcreteSubject extends Subject {private String subjectstatus;
  Public String Getsubjectstatus () {return subjectstatus; public void Setsubjectstatus (String sUbjectstatus) {this.subjectstatus = Subjectstatus; public static void Main (string[] args) {//TODO auto-generated method stub concretesubject s = New Concretes

    Ubject ();
    S.attach (New Concreteobserver (S, "X"));
    S.attach (New Concreteobserver (S, "Y"));
    
    S.attach (New Concreteobserver (S, "Z"));
    S.setsubjectstatus ("Ready");
  S.notify ();
 }
}

In the code above, there are abstract observers and abstract notifications. When the state of the subject changes, the calling function notifies the observer who has been registered within it. The idea of this design in peacetime life is also more common, such as the first mentioned the production of rice manufacturers and sales of rice merchants. Another application scenario, such as a bookstore in a book out of stock, customers still want to buy, you can register, and so on arrival, the bookstore owner will call in turn to inform customers who want to buy books. This registration mechanism is also reflected in many other programming techniques. For example, a program registers multiple callback functions with the underlying library, and when conditions are met, the underlying library notifies (or invokes) the callback function provided at the top.

The above code is written in Java, C + + is similar, mainly subject save observer pointers. But C + + to consider the issue of freeing memory, note that when the observer itself to be destroyed, you must call the subject detach function, otherwise the update may be the use of wild pointers caused by crash problems. Consider using subject to manage the lifecycle of observer.

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.