Java Design Patterns Learning notes (viewer mode)

Source: Internet
Author: User

The observer pattern is simple to say, a pattern of newspaper-ordering. But in fact this Part I think is still very interesting, "Head first design mode" There are still some not read, but also because the understanding is not deep enough.

The observer pattern consists of two components: The Observer and the subject.

This code is the interface of the topic:

 Package Obeserver;  Public Interface Subject {    publicvoid  registerobserver (Observer Observer);      Public void removeobserver (Observer Observer);      Public void notifyobserver ();}

Here is the Observer's interface:

 Package Obeserver;  Public Interface Observer {    publicvoid  update (String str);}

Then there is the specific theme implementation:

 PackageObeserver;Importjava.util.ArrayList;Importjava.util.List; Public classConcreteSubjectImplementsSubject {List<Observer> list =NewArraylist<observer>(); @Override Public voidregisterobserver (Observer Observer) {//TODO auto-generated Method StubLIST.ADD (Observer); } @Override Public voidremoveobserver (Observer Observer) {//TODO auto-generated Method StubLIST.REMOVE (Observer); } @Override Public voidNotifyobserver () {//TODO auto-generated Method Stub         for(Observer o:list) {o.update ("Hello, world!."); }    }}

The following code is specific to the observer implementation:

 Package Obeserver;  Public class Implements Observer {    @Override    publicvoid  update (String str) {        //  TODO auto-generated method stub        System.out.println (str);}    }

Write a test code below to see:

 PackageObeserver; Public classObservertest { Public Static voidMain (string[] args) {Subject S1=NewConcreteSubject (); Observer O1=NewConcreteobserver (); Observer O2=NewConcreteobserver (); Observer O3=NewConcreteobserver ();        S1.registerobserver (O1);        S1.registerobserver (O2);        S1.registerobserver (O3);    S1.notifyobserver (); }}

This test code will output three lines of "Hello, world!".

In fact, I think the Update method here can not explain the problem of writing Hello,world. In fact, this method should be to update the observer's other States, such as the book is written in the weather station some data (temperature, humidity, etc.), this is called update.

To summarize, the observer pattern is very much like a newspaper, and the newspaper (subject) maintains the subscription method and the Unsubscribe method, while maintaining the method of delivering the newspaper. and the newspaper people (observers) after receiving the newspaper, read the newspapers can understand the news, broaden their knowledge, this is the update.

Java Design Patterns Learning notes (viewer mode)

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.