The observer pattern for Java design patterns

Source: Internet
Author: User

Android Development If you do not understand the design patterns, then deep research will be difficult, the author deeply understand, the research design mode is imperative:

FIRST: Observer pattern:

Observer pattern Definition: defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and updated automatically.

In the observer pattern, there are 2 objects , the target object (the observed person) and the Observer .

This article uses the newspaper (target object) and the reader (Observer) to image instead of the above 2 objects, the implementation process is as follows:

Readers apply for newspaper---> newspapers to retain readers ' information---> newspaper has a new newspaper---> Sent to multiple readers---> readers get a new newspaper (data Update).

The implementation code is as follows:


Target object:

Package Com.hongri.observerpatterns;import java.util.arraylist;import java.util.list;/** * Target Object * @author Zhongyao */ public class Subject {//used to store the readers who have applied for the newspaper private list<observer> readers = new arraylist<observer> ();// Add reader public void attach (Observer reader) {readers.add (reader);} Delete reader public void detach (Observer reader) {readers.remove (reader);} Notify all readers of new newspaper information (data Update) protected void Notifyobervers () {for (Observer reader:readers) {reader.update (this);}}


The specific implementation class for the target object:
Package com.hongri.observerpatterns;/** * Specific target implementations * @author Zhongyao */public class newspaper extends Subject {private Str ing content;public String getcontent () {return content;} public void SetContent (String content) {this.content = Content;notifyobervers ();}}


Viewer interface:
Package com.hongri.observerpatterns;/** * Viewer * @author Zhongyao */public interface Observer {public void update (Subject s Ubject);}


The specific implementation class for the Observer interface:

Package com.hongri.observerpatterns;/** * Viewer Implementation *  * @author Zhongyao */public class Reader implements Observer {privat E string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic void Update (Subject Subject) {System.out.println (name +) received the newspaper and read it. The content is = = = "+ ((newspaper) subject). GetContent ());}}

Java Client Test Portal:

Package Com.hongri.main;import Com.hongri.observerpatterns.newspaper;import Com.hongri.observerpatterns.Reader;  public class Observerpclient {/** * (Viewer mode) client * @param args */public static void main (string[] args) {//newspaper (Observer) newspaper Subject = new newspaper ();//Reader (viewer) Reader reader1 = new Reader () reader1.setname ("Zhang San"); Reader reader2 = new Reader (); Reader2.setname ("John Doe"); Reader Reader3 = new Reader (); Reader3.setname ("Harry"); Reader reader4 = new Reader (); Reader4.setname ("Zhao Liu");//Add Reader (Viewer) to the newspaper (target object) Subject.attach (reader1); Subject.attach ( READER2); Subject.attach (Reader3); Subject.attach (reader4) subject.setcontent ("This issue is the Observer mode ...");}

Implementation results:




The observer pattern for Java design patterns

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.