观察者模式
观察者模式(Observer Pattern)
维基百科:The observer pattern is a software design pattern
in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods
.
写清楚点:subject object maintains a list of observers object,when subject object any state changes,subject object calling observers object one of their methods to notifies observers object.
翻译:主题对象维护观察者对象的列表,当主题对象发生任何状态变化时,主题对象调用观察者对象的一种方法来通知观察者对象。
也就是说:
- subject object 负责维护一系列 observer object;
- subject object的某个状态更改,就会调用observer object里面的方法来更新observer object;
- observer object 有更新自己的方法;
How
示例:根据上面写出一个例子
import java.util.*;
//测试类
public class Test {
public static void main(String []args) {
Subject subject = new Subject();
Observer aob = new AObserver();
Observer bob = new BObserver();
Observer cob = new CObserver();
subject.attach(aob);
subject.attach(bob);
subject.attach(cob);
subject.setState(1);
}
}
interface Observer{
void update(int state);
}
//Observer A
class AObserver implements Observer{
@Override
public void update(int state){
System.out.println("AObserver update state = [" + state + "]");
}
}
//Observer B
class BObserver implements Observer{
@Override
public void update(int state){
System.out.println("BObserver update state = [" + state + "]");
}
}
//Observer C
class CObserver implements Observer{
@Override
public void update(int state){
System.out.println("CObserver update state = [" + state + "]");
}
}
//Subject object
class Subject{
//一系列Observer
private List<Observer> observers = new ArrayList<Observer>();
//状态
private int state = 0;
public int getState(){
return this.state;
}
//状态更改
public void setState(int state) {
this.state = state;
this.notifyAllObservers();
}
//添加到observers里
public void attach(Observer observer) {
this.observers.add(observer);
}
//通知所有Observer
public void notifyAllObservers() {
for ( Observer observer : observers ) {
observer.update(this.state);
}
}
}
执行结果:
示例:复仇者联盟篇
import java.util.*;
//测试类
public class Test {
public static void main(String []args) {
Advengers advengers = new Advengers();
new AntMan(advengers);
new IronMan(advengers);
new SpideMan(advengers);
advengers.setState(0);
}
}
interface Hero{
void attack();
}
//复仇者联盟英雄
abstract class AdvengersHero implements Hero{
private Advengers advengers;
public AdvengersHero(Advengers advengers){
this.advengers = advengers;
this.advengers.attach(this);
}
}
class AntMan extends AdvengersHero{
public AntMan(Advengers advengers){
super(advengers);
}
@Override
public void attack(){
System.out.println("AntMan : become bigger");
}
}
class IronMan extends AdvengersHero{
public IronMan(Advengers advengers){
super(advengers);
}
@Override
public void attack(){
System.out.println("IronMan : boom boom");
}
}
class SpideMan extends AdvengersHero{
public SpideMan(Advengers advengers){
super(advengers);
}
@Override
public void attack(){
System.out.println("SpideMan : biu biu");
}
}
//复仇者联盟基地
class Advengers {
private List<Hero> heroList = new ArrayList<Hero>();
private int state = 0;
public void setState(int state){
this.state = state;
if ( 0 == state ) {
advice();
}
}
public void attach (Hero hero) {
this.heroList.add(hero);
}
public void advice(){
for (Hero hero : heroList) {
hero.attack();
}
}
}
Question
- 观察者模式与发布订阅者模式的区别?
后面再补充。
结束语:优秀是一种习惯。