Design Pattern Learning Notes (vii)-OBSERVER Observer mode

Source: Internet
Author: User

"Design Patterns," a book on Observer is described in this way:

Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated.

For instance, in real life, parents and children are the closest people. Parents as guardians of the Child (observed) (viewer), when the child and others fight, will tell his parents this matter (hehe, when the child is very small, usually will tell parents, when growing up, may not, the child here refers to the child), when the child won the scholarship, also will tell his parents. Below I use observer to implement this program. The code is as follows:

Import Java.util.vector;class children{
static private vector<observer> Obs;
static private String state=null;
static{
Obs=new vector<observer> ();
}
public static void Attach (Observer o) {
Obs.addelement (o);
}
public static void Detach (Observer o) {
Obs.removeelement (o);
}
public void SetState (String str) {
STATE=STR;
}
Public String getState () {
return state;
}
public void Notifyobs () {
for (Observer o:obs) {
O.update (this);
}
}
}
Interface observer{
public void Update (Children child);
}
Class Parent implements observer{
public void Update (Children child) {
if (Child.getstate (). Equals ("Fight")) {
System.out.println ("Parent, he fights with others");
}else if (Child.getstate (). Equals ("scholarship")) {
System.out.println ("Tell the parent that he got a scholarship");
}
}
}
Class Mother implements observer{
public void Update (Children child) {
if (Child.getstate (). Equals ("Fight")) {
System.out.println ("Tell mother he was in a fight with someone else");
}else if (Child.getstate (). Equals ("scholarship")) {
System.out.println ("Tell mother that he got a scholarship");
}
}
}
public class Client {

public static void Main (string[] args) {
Children child=new Children ();
Observer parent=new parent ();
Observer mother=new mother ();
Child.attach (parent);
Child.attach (mother);
Child.setstate ("Fight");
Child.notifyobs ();
Child.setstate ("scholarship");
Child.notifyobs ();

}

}

The output is as follows:

Tell the parent that he was in a fight with someone.

Tell mother he had a fight with someone else.

Tell the parent that he got a scholarship

Tell mother he got a scholarship.

Summary: For observer mode, the object-subject object that triggers the event cannot predict all the objects that might need to know the event. To solve this problem, we create a observer interface that requires all observer to register themselves on the subject.

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.