Original articles, reproduced please be sure to put the following paragraph at the beginning of the article.
This article is forwarded from Jason's Blog, the original link http://www.jasongj.com/design_pattern/observer/Observer pattern to introduce The Observer pattern definition
Observer mode is also called the Publish-subscribe model, which defines a one-to-many dependency, where multiple observer objects can listen to a subject object at the same time, and when the subject object's state changes, all the corresponding observer objects can be notified. Observer Pattern class diagram
The Observer pattern class diagram is as follows (click to view larger image)
An observer pattern role is divided into topics, abstract classes, or interfaces, such as the Abstractsubject specific topics in the above class diagram, such as the Subject1,subject2 Observer in the above class diagram, such as the IOBSERVER specific observer in the above class diagram, Example examples of OBSERVER1,OBSERVER2,OBSERVER3 observer patterns in the above class diagram
Headhunters or HR often have a lot of job information, job seekers can register in headhunting or HR, and when headhunters or HR have new post information, they will notify those registered job seekers. This is a typical observer mode usage scenario. Instance class diagram
The Observer pattern instance class diagram is as follows (click to view larger image)
Instance Resolution
This example code can be downloaded from author GitHub
The Observer interface (or abstract observer, as in this case, italent) needs to define the callback interface, as follows
Package com.jasongj.observer;
Public interface Italent {
void Newjob (String job);
}
A specific observer, such as the juniorengineer,seniorengineer,architect in this example, implements its response to the event in the callback interface, such as
Package com.jasongj.observer;
Import Org.slf4j.Logger;
Import org.slf4j.LoggerFactory;
public class Architect implements Italent {
private static final Logger LOG = Loggerfactory.getlogger (architect.class );
@Override public
void Newjob (String job) {
log.info ("Architect get new Position {}", job);
}
The abstract subject class, such as the abstracthr in this example, defines notifying the observer interface and implementing an increase in observer and deletion of the Observer method (which can be shared by the quilt, so it is implemented in an abstract class), as
Package com.jasongj.subject;
Import java.util.ArrayList;
Import java.util.Collection;
Import com.jasongj.observer.ITalent;
Public abstract class Abstracthr {
protected collection<italent> alltalents = new arraylist<italent> (); Public
abstract void Publishjob (String job);
public void Addtalent (italent talent) {
alltalents.add (talent);
}
public void Removetalent (italent talent) {
alltalents.remove (talent);
}
}
The specific subject class (such as the Headhunter in this example) simply implements the notification observer interface, in which all registered specific observers are notified. The code is as follows
Package com.jasongj.subject;
public class Headhunter extends Abstracthr {
@Override public
void Publishjob (String job) {
Alltalents.foreach (Talent-> talent.newjob (Job));
}
When the subject class is updated (as in this case, the recruiter has a new job), call its notification interface to notify all observers (job seekers) of their status (POST)
Package com.jasongj.client;
Import Com.jasongj.observer.Architect;
Import com.jasongj.observer.ITalent;
Import Com.jasongj.observer.JuniorEngineer;
Import Com.jasongj.observer.SeniorEngineer;
Import Com.jasongj.subject.HeadHunter;
Import Com.jasongj.subject.AbstractHR;
public class Client1 {public
static void Main (string[] args) {
italent juniorengineer = new Juniorengineer ();
italent Seniorengineer = new Seniorengineer ();
Italent architect = new architect ();
Abstracthr subject = new Headhunter ();
Subject.addtalent (juniorengineer);
Subject.addtalent (seniorengineer);
Subject.addtalent (architect);
Subject.publishjob ("Top data position");
}
The pros and cons of the Observer model
Observer Mode AdvantagesAbstract topics rely solely on the abstract observer observer pattern to support the broadcast communication observer pattern separating the information generation layer from the response layer
Observer Pattern DisadvantageIf a subject is registered by a large number of observers, notifying all observers would be costly. If some observer's response is blocked, the entire notification process is blocked and other observers are not notified in time.
observer patterns and OOP principles
principles that have been followedThe dependency inversion principle (subject class depends on the abstract observer rather than the specific observer) Dimitri principle on the Richter replacement principles Interface Isolation Principle Single duty principle open-closing principle
principles that are not followedNA
Java Design Pattern seriesJava design pattern (i) Simple Factory mode not simple Java design pattern (二) factory method pattern Java design Pattern (iii) abstract Factory mode Java design pattern (IV) Observer mode Java design pattern (v) Combinatorial mode Java design pattern (vi) proxy mode VS. Decorative mode Java design mode (vii) Spring AOP JDK dynamic agent vs. Cglib