Reference Documentation:
1. Design mode-the basis of reusable object-oriented software
2.http://blog.csdn.net/zhshulin/article/details/38708351 (design mode-observer mode (OBSERVER))
Observer Mode (OBSERVER) (behavior mode)
Detailed knowledge of the theory, can refer to the reference document.
Look at the structure of the Observer:
How to collaborate:
If, in the implementation, you need to encapsulate complex update semantics, you can use Mediator (5.5) mode, Changemanager is an instance of the Mediator (5.5) pattern.
Code implementation, case for the case of the clock spoken in the book.
Subject.java:
Package com.rick.designpattern.observer;
Import java.util.ArrayList;
Import java.util.List;
/**
* Created by MyPC on 2017/6/19.
*
/Public abstract class Subject {
private list<observer> observers = new arraylist<observer> ();
public void Attach (Observer Observer) {
observers.add (Observer);
}
public void Detach (Observer Observer) {
observers.remove (Observer);
}
public void Notify () {for
(Observer os:observers) {
os.update (this);
}
}
}
Clocktimer.java:
Package com.rick.designpattern.observer;
Import Javax.xml.crypto.Data;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
/** * Created by MyPC on 2017/6/19.
*/public class Clocktimer extends Subject {private int hour, minute, second;
Public Clocktimer () {} public int gethour () {return hour;
} public int Getminute () {return minute;
} public int Getsecond () {return second;
} public void Tick () {New Thread (new Runnable () {@Override public void run () {
while (true) {Date date = new Date (System.currenttimemillis ());
hour = Date.gethours ();
minute = Date.getminutes ();
Second = Date.getseconds ();
ClockTimer.this.Notify ();
try {thread.sleep (1000);
} catch (Interruptedexception e) { E.printstacktrace ();
}}}). Run (); }
}
Observer.java:
Package com.rick.designpattern.observer;
/**
* Created by MyPC on 2017/6/19.
*
/Public abstract class Observer {public
abstract void update (Subject Subject);
}
Analogclock.java:
Package com.rick.designpattern.observer;
/**
* Created by MyPC on 2017/6/19.
*/Public
class AnalogClock extends Observer implements Widget {
private clocktimer subject;
Public AnalogClock (Clocktimer subject) {
this.subject = subject;
Subject.attach (this);
}
public void Detachsubject () {
Subject.detach (this);
}
@Override public
Void Draw () {
System.out.println ("AnalogClock: Current time:" + subject.gethour () + ":" + Subject.getminute () + ":" + Subject.getsecond ());
}
@Override public
void Update (Subject Subject) {
if (Subject instanceof Clocktimer) {
draw ();
}
}
}
Digitalclock.java:
Package com.rick.designpattern.observer;
/**
* Created by MyPC on 2017/6/19.
*/Public
class DigitalClock extends Observer implements Widget {
private clocktimer subject;
Public DigitalClock (Clocktimer subject) {
this.subject = subject;
Subject.attach (this);
}
public void Detachsubject () {
Subject.detach (this);
}
@Override public
Void Draw () {
System.out.println ("DigitalClock: Current time:" + subject.gethour () + ":" + Subject.getminute () + ":" + Subject.getsecond ());
}
@Override public
void Update (Subject Subject) {
if (Subject instanceof Clocktimer) {
draw ();
}
}
}
Widget.java:
Package com.rick.designpattern.observer;
/**
* Created by MyPC on 2017/6/19.
*
/public interface Widgets {public
void Draw ();
}
Client.java:
Package com.rick.designpattern.observer;
/**
* Created by MyPC on 2017/6/19.
*/Public
class Client {public
static void Main (string[] args) {
Clocktimer clocktimer = new Clocktimer ();
digitalclock DigitalClock = new DigitalClock (clocktimer);
AnalogClock AnalogClock = new AnalogClock (clocktimer);
Clocktimer.tick ();
}
}