Observer mode _ observer _ case description

Source: Internet
Author: User

I have talked about a single-column Pattern Design. Let's explain the observer pattern today. In 23 designs, the observer pattern is the queen of pattern design, especially in the process of software design, we have embodied an infinite foothold. Besides, it is better to come to an instance than simply or clearly, I wrote the conceptual explanation in the code annotation method, as shown below:

// First, we need to define an interface as: Abstract observer

Public interface watcher {

// Define another method for receiving updated information

Public void updatenoworkflow (content );

}

// Define an interface as follows: Abstract observer

Public interface watched {

// Define a method in its interface to add an observer

Public void add (watcher );

// Define another method to delete the observer's rights

Public void remove (watcher );

// Define another method for realizing behavior monetization and transmitting information to the observer

Public void policywatcher (content );

}

// Define a class as follows: a specific observer and implement its interface to abstract the observer.

Public class concretewatcher implements watcher {

// And overwrite the Method

Public void updatenoworkflow (content ){

Int id = content. GETID ();

String name = content. getname ();

String address = content. getaddress ();

System. Out. println ("ID:" + id ++ "/n name:" + name + "/n address:" + address );

}

}

// Define a class as follows: the class is observed and its abstract interface is implemented.

Public class concretewatched implements watched {

// Define a list to encapsulate watcher

Private list <watcher> List = new arraylist <watcher> ();

// And overwrite the Method

Public void add (watcher ){

List. Add (watcer );

}

Public void remove (watcher ){

List. Remove (watcher );

}

Public void policywatcher (content ){

For (watcher: List ){

Watcher. updatewatcher (content );

}

}

}

// Operations are performed in an object-oriented manner. An object must be defined to perform operations.

Public class content {

Private int ID;

Private string name;

Private string address;

Public void setid (int id ){

This. ID = ID;

}

Public int GETID (){

Return ID;

}

Public void setname {string name }{

This. Name = Name;

}

Public String getname (){

Return name;

}

Public void setaddress (string address ){

This. Address = address;

}

Public String getaddress (){

Return address

}

}

// Test class

Public classs test {

Public static void main (string ARGs []) {

// An observer of the Instance

Watched watched = new watched ();

// The first observer of the Instance observes the observer.

Watcher watcher1 = New watcher ();

// The second observer of the Instance observes the observer.

Watcher watcher2 = New watcher ();

// The third observer of the Instance observes the observer.

Watcher watcher3 = New watcher ();

// Add observation

Watched. Add (watcher1 );

Watched. Add (watcher2 );

Watched. Add (watchre3 );

// When the observer changes: call its Information Method

Content content = new content ();

Content. setid ("0001 ");

Content. setname ("jiangshide ");

Content. setaddress ("Beijing ");

Watched. policywatchers (content );

// The observer can call the delete method.

Watched. Remove (watchr2 );

Content. setname = "litian ";

Watched. policywatchers (content );

}

}

Well, the above is a complete core code embodiment of the Observer Pattern Design. As we can see above, the relationship between the observer and the observer is many-to-one, that is, an observer can be followed by multiple observers at the same time. When the observer updates the information, the observer automatically updates the information, for this reason, I have thoroughly understood and constructed a UML diagram:

 

 

The core framework code is:

Package com. JSD. Observer. app;

/**

* The Master abstraction is observed.

* @ Author jiangshide

*

*/

Abstract class mainsubject {

Abstract void addobserver (mainobserver );

Abstract void removeobserver (mainobserver );

Abstract void policyobservers (maincontent );

Public void localnofity (){}

}

Package COM. JSD. observer. APP;/*** main abstract Observer * @ author jiangshide **/abstract class mainobserver {abstract void updatanotyfy (maincontent); Public void localpolicy () {}} package COM. JSD. observer. APP;/*** Main Content support * @ author jiangshide **/public class maincontent {private int ID; private string name; Public int GETID () {return ID ;} public void setid (int id) {id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}} package COM. JSD. observer. APP;/*** MSN abstract observer class and inherit from main abstract Observer * @ author jiangshide **/abstract class msnobserver extends mainobserver {abstract void msnnotify ();} package COM. JSD. observer. APP;/*** MSN abstraction is observed and inherits the main abstraction is observed * @ author jiangshide **/abstract class msnsubject extends mainsubject {} package COM. JSD. observer. APP;/*** MSN content provider * @ author jiangshide **/public class msncontent extends maincontent {private int localid; private string localname; private string msncontent; private string fromresouce; private string fromtime; Public int getlocalid () {return localid;} public void setlocalid (INT localid) {This. localid = localid;} Public String getlocalname () {return localname;} public void setlocalname (string localname) {This. localname = localname;} Public String getmsncontent () {return msncontent;} public void setmsncontent (string msncontent) {This. msncontent = msncontent;} Public String getfromresouce () {return fromresouce;} public void setfromresouce (string fromresouce) {This. fromresouce = fromresouce;} Public String getfromtime () {return fromtime;} public void setfromtime (string fromtime) {This. fromtime = fromtime ;}} package COM. JSD. observer. APP; import Java. util. arraylist; import Java. util. list; /*** MSN is absolutely inherited by the observer class. MSN abstract is the observer class * @ author jiangshide **/public class msnconcretesubject extends msnsubject {private list <mainobserver> List = new arraylist <mainobserver> (); mainobserver msnobserver = new Queue (); mainobserver newsobserver = new newsconcreteobserver (); @ overrisponid addobserver (mainobserver) {// todo auto-generated method stubif (mainobserver. equals (msnobserver) {// Add a list of people who follow MSN messages. add (msnobserver);} else if (mainobserver. equals (newsobserver) {// Add a list of people who follow news. add (newsobserver);} else {// No followers} @ overrisponid policyobservers (maincontent) {// todo auto-generated method stubfor (mainobserver mainobservers: list) {If (mainobservers. equals (msnobserver) {maincontent = new msncontent () ;}}@ overrisponid removeobserver (mainobserver) {// todo auto-generated method stubif (mainobserver. equals (msnobserver) {// Delete the list of people who are interested in MSN messages at the moment. remove (msnobserver);} else if (mainobserver. equals (newsobserver) {// Delete the list of people who are interested in new news at the moment. remove (newsobserver) ;}} package COM. JSD. observer. APP;/*** MSN absolute observer inherits MSN abstract Observer * @ author jiangshide **/public class msnconcreteobserver extends msnobserver {@ overridepublic void updatanotyfy (maincontent) {// todo auto-generated method stub} @ overrisponid msnnotify () {// todo auto-generated method stub} package COM. JSD. observer. APP;/*** news abstract observer and inherit the main abstract Observer * @ author jiangshide **/abstract class newsobserver extends mainobserver {} package COM. JSD. observer. APP;/*** the news abstraction is inherited by the observer. The main abstraction is replaced by the Observer * @ author jiangshide **/abstract class newssubject extends mainsubject {} package COM. JSD. observer. APP;/*** news content provider and inherit the main content information * @ author jiangshide **/public class newscontent extends maincontent {private int localid; private string localname; private string newscontent; private string fromresource; private string linkweb; Public int getlocalid () {return localid;} public void setlocalid (INT localid) {This. localid = localid;} Public String getlocalname () {return localname;} public void setlocalname (string localname) {This. localname = localname;} Public String getnewscontent () {return newscontent;} public void setnewscontent (string newscontent) {This. newscontent = newscontent;} Public String getfromresource () {return fromresource;} public void setfromresource (string fromresource) {This. fromresource = fromresource;} Public String getlinkweb () {return linkweb;} public void setlinkweb (string linkweb) {This. linkweb = linkweb;} package COM. JSD. observer. APP;/*** news is definitely inherited by the observer. News abstraction is replaced by the Observer * @ author jiangshide **/public class newsconcretesubject extends newssubject {@ overriworkflow ID addobserver (mainobserver) {// todo auto-generated method stub} @ overrisponid policyobservers (maincontent) {// todo auto-generated method stub} @ overrisponid removeobserver (mainobserver) {// todo auto-generated method stub} package COM. JSD. observer. APP;/*** news absolute observer inherits news abstract Observer * @ author jiangshide **/public class newsconcreteobserver extends newsobserver {@ overridepublic void updatanotyfy (maincontent) {// todo auto-generated method stub}. The above is my deep understanding of the observer mode. Please read the code and see the structure, I hope to raise more questions .....

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.