------Observer patterns for Java 23 design Patterns

Source: Internet
Author: User

One Observer Mode (Observer)

The observer pattern is very well understood, similar to mail subscriptions and RSS feeds, when we look at some blogs or wikis, we often see the RSS icon, which means that when you subscribe to the article, if there are updates, you will be notified in time. In fact, a simple sentence: When an object changes, other objects dependent on the object will be notified, and with the change! Objects are a one-to-many relationship. Take a look at the diagram first:

I explain the effects of these classes: The Mysubject class is our main object, and Observer1 and Observer2 are objects that depend on Mysubject, and Mysubject and Observer1 are bound to change when Observer2 changes. The Abstractsubject class defines a list of objects that need to be monitored, which can be modified to add or remove monitored objects, and to notify objects that exist within the list when Mysubject changes. Let's look at the implementation code:

One observer interface:

 
    1. Public interface Observer {
    2. public void update ();
    3. }

Two implementation classes:

 
    1. public class Observer1 implements Observer {
    2. @Override
    3. public void Update () {
    4. System.out.println ("Observer1 has received!");
    5. }
    6. }
 
    1. public class Observer2 implements Observer {
    2. @Override
    3. public void Update () {
    4. System.out.println ("Observer2 has received!");
    5. }
    6. }

Subject interface and implementation class:

 
    1. Public interface Subject {
    2. /* Increase the viewer */
    3. public void Add (Observer Observer);
    4. /* Delete the viewer */
    5. public void del (Observer Observer);
    6. /* Notify all observers */
    7. public void notifyobservers ();
    8. /* Self-operation */
    9. public void operation ();
    10. }
 
  1. Public abstract class Abstractsubject implements Subject {
  2. Private vector<observer> Vector = new vector<observer> ();
  3. @Override
  4. public void Add (Observer Observer) {
  5. VECTOR.ADD (Observer);
  6. }
  7. @Override
  8. public void del (Observer Observer) {
  9. VECTOR.REMOVE (Observer);
  10. }
  11. @Override
  12. public void Notifyobservers () {
  13. enumeration<observer> Enumo = vector.elements ();
  14. while (Enumo.hasmoreelements ()) {
  15. Enumo.nextelement (). Update ();
  16. }
  17. }
  18. }
 
    1. public class Mysubject extends Abstractsubject {
    2. @Override
    3. public void operation () {
    4. System.out.println ("Update self!");
    5. Notifyobservers ();
    6. }
    7. }


Test class:

 
    1. public class Observertest {
    2. public static void Main (string[] args) {
    3. Subject sub = new Mysubject ();
    4. Sub.add (New Observer1 ());
    5. Sub.add (New Observer2 ());
    6. Sub.operation ();
    7. }
    8. }

Output:

Update self!
Observer1 has received!
Observer2 has received!

Reprint https://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html

------Observer patterns for Java 23 design Patterns

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.