Definition: Defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and updated automatically.
UML diagram:
code example: Take examples of projects that have been done before, Bo mainly realizes the function of the restaurant printing order, as shown.
Through the obvious we found that the order center and the printer has a one-to-many relationship, according to the above for the observer definition, using the Observer mode, you can make the status of the order center changes at the same time, so that all printers receive state changes. Let's look at a UML diagram.
Defining the Subject Interface
Public interface Subject {
?
???? public void Registerobserver (Observer obs);
????
???? public void Removeobserver (Observer obs);
????
???? public void notifyobervers ();
}
Define Order center, implement subject interface
public class Ordercenter implements subject{
?
???? Private arraylist<observer> mobservers = null;
????
???? Private String order = null;
????
???? Public Ordercenter () {
???????? TODO auto-generated Constructor stub
???????? Mobservers = new arraylist<observer> ();
????}
????
[Email protected]
???? public void Registerobserver (Observer obs) {
???????? TODO auto-generated Method Stub
???????? if (OBS! = null) {
???????????? Mobservers.add (OBS);
????????}
????}
?
[Email protected]
???? public void Removeobserver (Observer obs) {
???????? TODO auto-generated Method Stub
???????? if (OBS! = null) {
???????????? Mobservers.remove (OBS);
????????}
????????
????}
?
[Email protected]
???? public void Notifyobervers () {
???????? TODO auto-generated Method Stub
???????? if (mobservers! = null && mobservers.size () > 0) {
???????????? for (Observer obs:mobservers)
???????????????? Obs.update ();
????????}
????}
????
???? public void Setorder (String order) {
???????? This.order = order;
???????? Notifyobervers ();
????}
????
???? Public String GetOrder () {
???????? return order;
????}
?
}
?
?
Defining the Print interface
Public interface Print {
?
???? public void print ();
}
Defining the Observer interface
Public interface Observer {
?
???? public void update ();
????
}
Front-desk printer for printing, viewer interface
public class Receptionprint implements print,observer{
?
???? Private Ordercenter mordercenter = null;
????
???? Public Receptionprint (Ordercenter oc) {
???????? TODO auto-generated Constructor stub
???????? Mordercenter = OC;
????}
?
[Email protected]
???? public void Update () {
???????? TODO auto-generated Method Stub
???????? Print ();
????}
?
[Email protected]
???? public void print () {
???????? TODO auto-generated Method Stub
???????? SYSTEM.OUT.PRINTLN ("Reception print Order:" + Mordercenter.getorder ());
????}
?????
????
}
Lobby printer for printing, viewer interface
public class Hallprint implements observer,print{
?
???? Private Ordercenter mordercenter = null;
????
???? Public Hallprint (Ordercenter oc) {
???????? TODO auto-generated Constructor stub
???????? Mordercenter = OC;
????}
[Email protected]
???? public void print () {
???????? TODO auto-generated Method Stub
???????? System.out.println ("Hall Print Order:" + Mordercenter.getorder ());
????}
?
[Email protected]
???? public void Update () {
???????? TODO auto-generated Method Stub
???????? Print ();
????}
?
}
Test code
public class Main {
?
???? public static void Main (string[] args) {
???????? Ordercenter oc = new Ordercenter ();
???????? Observer hallobser = new Hallprint (OC);
???????? Observer receptionobser = new Receptionprint (OC);
???????? Oc.registerobserver (Hallobser);
???????? Oc.registerobserver (Receptionobser);
???????? Oc.setorder ("Take Out order!");
???????? Oc.removeobserver (Receptionobser);
???????? Oc.setorder ("Hall order");
????}
}
?
Test results
Hall print Order:take out order!
Reception print Order:take out order!
Hall Print Order:hall Order
above, we define the interface of the viewer and the subject, in fact The JDK class package Java.util contains the most basic Observer interfaces and Observable classes, almost as much as the interfaces we define, and you can even Notifyobservers () or notifyobservers (Object Arg) method, select pull or push to let the observer get the data. Note the Setchanged () method is called before the survey notifyobservers .
?
Design Pattern-Viewer