Behavioral mode: Observer Mode

Source: Internet
Author: User

Behavioral mode: Observer Mode
1. Intention

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

2. Alias

Dependencies, Publish-Subscribe)

3. Motivation

Splitting a system into a series of collaborative classes has a common side effect: maintain consistency between related objects. We do not want to make the classes tightly coupled to maintain consistency, so this reduces their reusability.

4. Applicability

The ObServer mode is used in the following cases:
-When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.
-When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.
-When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.

5. Structure

The next four models, including this mode, are the relationships between classes and classes, which do not involve inheritance. Remember to sum up when learning. Remember the figure at the beginning of this article. The observer mode is easy to understand, similar to email subscription and RSS subscription. When we browse some blogs or wikis, we often see RSS icons. This means that when you subscribe to this article, you will be notified of any updates later. In fact, simply put, when an object changes, other objects dependent on this object will be notified and will change accordingly! An object is a one-to-multiple relationship. Let's take a look at the relationship diagram:

I will explain the role of these classes: MySubject class is our main object, Observer1 and Observer2 are dependent on MySubject objects. When MySubject changes, Observer1 and Observer2 will inevitably change. The AbstractSubject class defines the list of objects to be monitored. You can modify the list to add or delete Monitored Objects. When MySubject changes, it notifies the objects in the list. The implementation code is as follows:

6. Sample Code

Implementation Code:
An Observer interface:

public interface Observer {    public void update();}

Two implementation classes ::

public class Observer1 implements Observer {    @Override    public void update() {        System.out.println(observer1 has received!);    }}public class Observer2 implements Observer {    @Override    public void update() {        System.out.println(observer2 has received!);    }}

Subject interface and implementation class ::

Public interface Subject {/* add Observer */public void add (observer Observer);/* Delete observer */public void del (Observer observer ); /* notify all Observers */public void policyobservers ();/* operations */public void operation ();} public abstract class AbstractSubject implements Subject {private Vector
  
   
Vector = new Vector
   
    
(); @ Override public void add (Observer observer) {vector. add (observer) ;}@ Override public void del (Observer observer) {vector. remove (observer) ;}@ Override public void policyobservers () {Enumeration
    
     
Enumo = vector. elements (); while (enumo. hasMoreElements () {enumo. nextElement (). update () ;}} public class MySubject extends AbstractSubject {@ Override public void operation () {System. out. println (update self !); Yyobservers ();}}
    
   
  

Test class:

public class ObserverTest {    public static void main(String[] args) {        Subject sub = new MySubject();        sub.add(new Observer1());        sub.add(new Observer2());        sub.operation();    }}

Output:
Update self!
Observer1 has already ed!
Observer2 has already ed !.

 

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.