Design Patterns Learning Note II: Observer patterns

Source: Internet
Author: User

The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and updated automatically.

Observer

 Package Com.singland.dp.observer;  Public Interface Observer {    publicvoid  update (Subject Subject);}

MyObserver1

 Package Com.singland.dp.observer;  Public class Implements Observer {    @Override    publicvoid  update (Subject Subject) {        System.out.println (String.Format ("Myobserver1:you is%s?") , Subject.getmsg ()));}    }

MyObserver2

 Package Com.singland.dp.observer;  Public class Implements Observer {    @Override    publicvoid  update (Subject Subject) {        System.out.println (String.Format ("Myobserver2:you is%s?") , Subject.getmsg ()));}    }

Subject

 PackageCom.singland.dp.observer;Importjava.util.ArrayList;Importjava.util.List; Public classSubject {PrivateString msg; PrivateList<observer> observers =NewArraylist<observer>();  PublicString getmsg () {returnmsg; }     Public voidsetmsg (String msg) { This. msg =msg; }         Public voidregisterobserver (Observer Observer) {observers.add (Observer); }         Public voidremoveobserver (Observer Observer) {observers.remove (Observer); }         Public voidnotifyobservers () { for(Observer observer:observers) {observer.update ( This); }    }}

MyTest

 package   Com.singland.dp.observer;  import   Org.junit.Test;  public  class   MyTest {@Test  public  void   Test () {Subject Subject  = new Subject (); Subject.registerobserver ( new          MyObserver1 ()); Subject.registerobserver ( new          MyObserver2 ());        Subject.setmsg ( Linda ");    Subject.notifyobservers (); }}

When the business logic in the project has a concept about publishers and subscribers, we should first think about using the observer design pattern to handle the problem, the observer design pattern should be the most common design pattern, and Java even provides a built-in observer design pattern for developers to use, But there are some limitations to the built-in observer design patterns in Java:

1. Java.util.Observable is a class rather than an interface, which violates object-oriented design principles: interface-oriented programming rather than implementation programming. 2. The Setchanged method is protected, which means that if we want to use this method must inherit from the observable class, and Java can only inherit, contrary to the combination better than the inheritance principle of the shortcomings of the Java built-in observer mode of the reusability. Of course, if the above drawbacks do not cause any problems with the business logic in your project, use Java's built-in observer pattern boldly.

Design Patterns Learning Note II: Observer 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.