Java Design Pattern Case Study: Observer pattern

Source: Internet
Author: User

Application scenarios for observer patterns:
1. Updates to an object state require other objects to be updated synchronously, and the number of other objects is dynamically variable.
2. Objects only need to notify their own updates to other objects without needing to know the details of other objects.

For example, this example tells the story of a eldest brother commanded to do things, as long as the eldest brother commanded to start work, younger brother members can increase or decrease. Based on the schema definition, using two interfaces, multiple implementations

 Package Main.java.com.learn.observer; /**  @author*/Publicinterface  Worker         {void  dosomething ();}
 Package Main.java.com.learn.observer; /**  */Publicinterface  mandator    {void  Addworker (Worker Worker);     void Removeworker (worker worker);     void notifyworker ();}
 PackageMain.java.com.learn.observer.impl;ImportMain.java.com.learn.observer.Mandator;ImportMain.java.com.learn.observer.Worker;Importjava.util.ArrayList;Importjava.util.List;/*** To achieve the command, methods have to add and expel the younger brother, and notify younger brother work * Created by Garfield on 2016/6/13.*/ Public classMandatorimplImplementsMandator {PrivateList<worker> workers =NewArraylist<>(); @Override Public voidAddworker (worker worker) {workers.add (worker); } @Override Public voidRemoveworker (worker worker) {workers.remove (worker); } @Override Public voidNotifyworker () { for(Worker worker:workers) {worker.dosomething (); }    }}
 Package Main.java.com.learn.observer.impl; Import Main.java.com.learn.observer.Worker; /**  */Publicclassimplements  worker{    @Override      Public void dosomething () {        System.out.println ("End tea");}    }
 package   Main.java.com.learn.observer.impl;  import   Main.java.com.learn.observer.Worker;  /**   * implement work interface, each younger brother work content is different * Created by Garfield on 2016/6/13.  */ public  class  workimpl_2 implements   worker{@Override  public  void      DoSomething () {System.out.println (" Send Water "
 Package Main.java.com.learn.observer.impl; Import Main.java.com.learn.observer.Worker; /**  */Publicclassimplements  worker{    @Override      Public void dosomething () {        System.out.println ("Cooking");}    }

Test code:

 PackageMain.java.com.learn.observer;ImportMain.java.com.learn.observer.impl.MandatorImpl;ImportMain.java.com.learn.observer.impl.WorkImpl_1;Importmain.java.com.learn.observer.impl.WorkImpl_2;ImportMain.java.com.learn.observer.impl.WorkImpl_3;/*** Created by Garfield on 2016/6/13.*/ Public classObservertest { Public Static voidMain (string[] args) {mandator mandator=NewMandatorimpl (); Worker Worker1=NewWorkimpl_1 (); Worker Worker2=Newworkimpl_2 (); Worker Worker3=NewWorkimpl_3 ();        Mandator.addworker (Worker1);        Mandator.addworker (WORKER2);        Mandator.addworker (WORKER3);    Mandator.notifyworker (); }}

So the three little boys began to work. Output Result:

End tea and water for cooking

is not clear!

Java Design Pattern Case Study: Observer pattern

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.