Java common design Patterns-observer patterns

Source: Internet
Author: User

Viewer mode:

Roles designed to:

Topic: is an interface that must contain a list of observers, must have a method for adding, deleting, notifying the Observer

Specific topic: implementation classes for themes

Observer: is also an interface in which you need to define an action after receiving a notification

Specific Observer: The Observer's implementation class, while the observer is required to subscribe to the subject, which must be added to the subject's list of observers, and the unsubscribe is deleted

When the state of the subject changes, all observers are notified

Theme:

Package com.design2;

Public interface Subjectinterface {
VOID Add (Observerinterface observer);
VOID Delete (Observerinterface observer);
void Notifyobserver ();
void Givemess (String mess);
}

Specific topics:

Package com.design2;

Import java.util.ArrayList;
Import java.util.List;

public class Subject implements Subjectinterface {
List<observerinterface> observerlist;
String mess;
Boolean isnotify;
Public Subject () {
Observerlist = new arraylist<observerinterface> ();
Mess = "";
Isnotify = false;
}
@Override
public void Add (Observerinterface observer) {
IF (Observer! = NULL &&!OBSERVERLIST.CONTAINS (Observer)) {
OBSERVERLIST.ADD (Observer);
}

}

@Override
public void Delete (Observerinterface observer) {
if (Observerlist.contains (Observer)) {
OBSERVERLIST.REMOVE (Observer);
}
}

@Override
public void Notifyobserver () {
if (isnotify) {
for (Observerinterface object:observerlist) {
Object.update (mess);
}
}
}

@Override
public void givemess (String message) {
if (mess.equals (message)) {
Isnotify = false;
} else {
Mess = message;
Isnotify = true;
}
}
}

Viewer interface:

Package com.design2;

Public interface Observerinterface {
void update (String mess);
}

Observed by:

Package com.design2;

public class Observer implements Observerinterface {
private String name;
Private Subjectinterface subject;
Public Observer (String name,subjectinterface subject) {
THIS.name = name;
Subject.add (this);
}

@Override
public void Update (String mess) {
SYSTEM.OUT.PRINTLN (name + "received message:" + mess);
}

}

Test:

Package com.design2;

public class Application {
public static void Main (string[] args) {
Subjectinterface subject = new subject ();
Observerinterface Zhangsan = new Observer ("Zhangsan", subject);
Observerinterface lisi = new Observer ("Zhangsan", subject);
Subject.givemess ("123");
Subject.notifyobserver ();
Subject.givemess ("123");
Subject.notifyobserver ();
Subject.givemess ("456");
Subject.notifyobserver ();
}
}

Java common design Patterns-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.