What is the Observer pattern.
Definition: The Observer pattern defines a one-to-many dependency between objects, and when the observer changes, all objects that depend on the observer are notified and the corresponding
Change.
Observer mode (sometimes called publish (publish)-subscribe (Subscribe) mode, model-view mode, source-Listen
(Listener) mode or slave mode is one of the software design patterns. In this mode, a target object manages all observations that depend on it.
object, and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This pattern is usually
is used to implement the event handling system.
The following example blog site blog For example, when a user posted a blog, all concerned about the blogger's users will receive this blog.
First step: Define an Observer interface: Observermodelinterface
package com;
/**
*
* @author YANGCQ
* @description Observer Mode-Viewer interface * *
/Public
interface observermodelinterface {public
void Update ();
}
The second step: the specific observer, implement the Observermodelinterface interface, here we define 2 implementation classes, these 2 implementation classes, can be regarded as 2 observers.
package com;
/**
*
* @author YANGCQ
* @description Observer Mode-Viewer interface Implementation class * */Public
class Observermodelimpl_a Implements observermodelinterface{public
void Update () {
System.out.println ("A refreshed the blog list, saw the" Java EE Specification "article);
}
}
package com;
/**
*
* @author YANGCQ
* @description Observer Mode-Viewer interface Implementation class * *
/Public
class Observermodelimpl_b implements observermodelinterface{public
void Update () {
System.out.println (" b Refresh the blog list, see the "Java EE specification" this article ");
}
}
Step three: Define the Observer abstract class: Observermodelsubject
package com;
Import Java.util.Vector;
/**
*
* @author YANGCQ
* @description Observer pattern-the Observer abstract class * *
*/public
abstract class Observermodelsubject {
//vector is thread-safe, so use vector instead of list
private vector<observermodelinterface> OMI = new vector<observermodelinterface> ();
Add observer public
void Addobserver (Observermodelinterface omi) {
this.omi.add (OMI);
}
Remove observer public
void Delobserver (Observermodelinterface omi) {
this.omi.remove (OMI);
}
Notifies all observers in the vector
protected void notifyallobserver () {for
(Observermodelinterface Oberver:omi) {
Oberver.update ();
}
}
An abstract method, implemented by the implementation class, to implement specific details public
abstract void dosomething ();
}
Fourth step: The observed implementation class: Observermodelsubjectimpl
package com;
/**
*
* @author YANGCQ
* @description Observer Mode-Viewer interface Implementation class * *
/Public
class Observermodelsubjectimpl extends observermodelsubject{
@Override public
void DoSomething () {
System.out.println ("Yangcq published a blog, named:" Java EE specification ");
This.notifyallobserver ();
}
}
OK, so the implementation of a simple observer pattern is written, and next, let's test it to see the effect:
package com;
/**
*
* @author YANGCQ
* @description Viewer mode-Test class * */Public
class Observermodeldemo {
// Observer pattern, a total of four roles: Observer interface, observer implementation class, observer abstract class, observer implementation class
//1, Observer Interface Observermodelinterface
// This interface defines a method update () , the Observer's Update method is invoked when the observed person changes,
and//2, the Observer implements the class Observermodelimpl_a and Observermodelimpl_b
// If the number of observers is large, There will be more implementations class
//3, the Observer abstract class Observermodelsubject
// This class is key, defines three methods common method and an abstract method
//4, the observer implements the class Observermodelsubjectimpl
// This implementation class mainly implements the details of the dosomething () method public
static void Main (string[] args) {
observermodelsubject oms = new Observermodelsubjectimpl ();
Add Observer list
Oms.addobserver (New Observermodelimpl_a ());
Oms.addobserver (New Observermodelimpl_b ());
Oms.dosomething ();
/**
* Console output:
*
* YANGCQ published a blog, named: "Java EE specification"
* A refresh the blog list, see the "Java EE specification" This article
* b refreshed the blog list, saw the The "Java EE specification" This article
*/
}
}