Design Pattern learning in Android-2. Observer Pattern Learning

Source: Internet
Author: User

Observer mode definition:

Defines one-to-multiple dependencies between objects. When the status of an object changes, all objects dependent on it are notified and updated automatically.

Subject: target object

A target object has multiple observer observations.

Provides maintenance for adding and deleting observers.

Notify all observers when the target changes

Observer: The Observer interface that provides the update method corresponding to the target notification.

Concretesubject: target object

Concreteobserver: a specific observer object

Subject Class:

package com.light.com;import java.util.ArrayList;import java.util.List;public class Subject {private List<Observer> observers = new ArrayList<Observer>();public void attach(Observer o) {observers.add(o);}public void detach(Observer o) {observers.remove(o);}protected void notifyObservers() {for (Observer o : observers) {o.update(this);}}}

Observer interface:

Package com.light.com; public interface observer {// update message public void Update (subject );}

Concretesubject class:

Package com.light.com; public class concretesubject extends subject {// new message private string news; Public String getnews () {return news;} public void setnews (string News) {This. news = News; system. out. println ("the probe is back, there are important messages"); this. yyobservers ();}}

Concreteobserver class:

Package com.light.com; public class concreteobserver implements observer {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}@ overridepublic void Update (subject) {// print the new message system. out. println (name + "received message, content:" + (concretesubject) subject ). getnews ());}}

Client:

Package com.light.com; public class client {/*** @ Param ARGs */public static void main (string [] ARGs) {concretesubject paper = new concretesubject (); concreteobserver reader1 = new concreteobserver (); encrypt ("Sun Quan"); concreteobserver reader2 = new concreteobserver (); encrypt ("Liu Bei"); concreteobserver reader3 = new concreteobserver (); reader3.setname (""); paper. attach (reader1); paper. attach (reader2); paper. attach (reader3); paper. setnews ("Cao"); system. out. println ("--------------"); paper. setnews ("Cao back ");}}

Running result:

The son has returned and has important messages.
Sun Quan received a message about Cao's arrival.
Liu Bei received a message about Cao's arrival.
Zhitian received a message about Cao's arrival.
--------------
The son has returned and has important messages.
Sun Quan received a message about Cao's return.
Liu Bei received a message about Cao's return.
Zhitian received a message about Cao's return.


Analysis process: Assume that a hacker holds a military order from the third-party commander. Once a new message is sent, the hacker immediately updates his information base and notifies all the commanders that my information base is updated. Come and have a look, the commander-in-chief has a probe reference to facilitate observation. Once the information library is updated, the commander can view the latest information of the probe update based on this reference. The coach relied on him to obtain intelligence, but he did not rely on him. For example, he thought that the salary of a certain commander was low and he did not want to help him, so he removed the commander, the removed commander cannot obtain information. The coach always passively waits for the son to return information.


Generally, we implement the observer mode in Java. We only need to inherit the two class observable classes and observer classes provided by the system. Other work systems have encapsulated them for us.

Concretesubject class

Package com.light.com; public class linglingqi extends Java. util. observable {private string news; Public String getnews () {return news;} public void setnews (string News) {This. news = News; // The message library is updated with a new message. This. setchanged (); // inform the commander. Here you can choose to only notify the new message without referencing the new message. Other messages may be valuable and cannot be reserved for the commander himself. notifyobservers (News); // exposes all your information to the commander without reservation. // This. yyobservers ();}}

Concreteobserver class

Package com.light.com; import Java. util. observable; public class General implements Java. util. observer {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}@ overridepublic void Update (observable o, object Arg) {system. out. println (name + "received message:" + Arg );}}

Client client

Package com.light.com; public class client {/*** @ Param ARGs */public static void main (string [] ARGs) {linglingqi L = new linglingqi (); general wutian = new general (); wutian. setname (""); General shangshan = new general (); shangshan. setname ("Shang shanqiexin"); General Yida = new general (); Yida. setname ("Ida Zheng Zong"); L. addobserver (wutian); L. addobserver (shangshan); L. addobserver (Yida); L. setnews ("The message is down"); system. out. println ("------ a few days later"); L. setnews ("Yu Chai xiuji ");}}

Running result:

Ida zhengzheng received the message: the Chief of the letter is down.
Shang Shanqian received the message: the Chief of the letter is down.
Wu Tianxin received the message: the Chief of the letter is down.
------ A few days later
Ida zhengzong received a message: Yu Chai xiuji
Shang Shanqian received the message: Yu Chai xiuji
Wu Tianxin received a message: Yu Chai xiuji


The observer mode is used for event listening in Android, but an observer observes multiple target objects, just like the commander sends multiple probes, each probe notifies the commander of changes through different interfaces.





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.