Mga tala ng pag-aaral ng pattern ng disenyo (34) -ang pagsasakatuparan ng pattern ng tagamasid

Design Pattern Study Notes Realization Observer Pattern



Mode ng tagamasid
Ang bawat tao'y gumagawa ng iba pang mga bagay sa opisina, at biglang dumating ang boss at kailangang ipaalam.
1. Two-way na code ng pagkabit

package operation import java.util.ArrayList class Secretary{//Front desk secretary private ArrayList<StockObserver>observers=new ArrayList<StockObserver>() private String action public void Attach(StockObserver observer) { observers.add(observer) } public void Notify() { for(int i=0i<observers.size()i++) { observers.get(i).Update() } } public String getAction() { return action } public void setAction(String action) { this.action=action } } class StockObserver{//Look at the stock colleague class private String name private Secretary sub public StockObserver(String name,Secretary sub) { this.name=name this.sub=sub } public void Update() {//Get notification from the front desk System.out.println(sub.getAction()+name+'Close stock quotes and continue working!') } } public class Main{ public static void main(String[] args){ Secretary tongzhizhe=new Secretary() StockObserver tongshi1=new StockObserver('Little King',tongzhizhe) StockObserver tongshi2=new StockObserver('Little Li',tongzhizhe) tongzhizhe.Attach(tongshi1) tongzhizhe.Attach(tongshi2) tongzhizhe.setAction('The boss is back') tongzhizhe.Notify() } }

Dalawa, decoupling code
imahe

package operation import java.util.ArrayList abstract class Subject{ abstract void Attach(Observer observer) abstract void Detach(Observer observer) abstract void Notify() String action public String getAction() { return action } public void setAction(String action) { this.action=action } } class Boss extends Subject{ private ArrayList<Observer>observers=new ArrayList<Observer>() void Attach(Observer observer) { observers.add(observer) } void Detach(Observer observer) { observers.remove(observer) } void Notify() { for(int i=0i<observers.size()i++) { observers.get(i).Update() } } } class Secretary extends Subject{ private ArrayList<Observer>observers=new ArrayList<Observer>() void Attach(Observer observer) { observers.add(observer) } void Detach(Observer observer) { observers.remove(observer) } void Notify() { for(int i=0i<observers.size()i++) { observers.get(i).Update() } } } abstract class Observer{ protected String name protected Subject sub public Observer(String name,Subject sub) { this.name=name this.sub=sub } public abstract void Update() } class StockObserver extends Observer{ public StockObserver(String name,Subject sub) { super(name,sub) } public void Update() { System.out.println(sub.getAction()+name+'Close stock quotes and continue working!') } } class NBAObserver extends Observer{ public NBAObserver(String name,Subject sub) { super(name,sub) } public void Update() { System.out.println(sub.getAction()+name+'Close the NBA live broadcast and continue working!') } } public class Main{ public static void main(String[] args){ Secretary tongzhizhe=new Secretary() StockObserver tongzhizhe1=new StockObserver('Little Li',tongzhizhe) NBAObserver tongzhizhe2=new NBAObserver('Xiao Zhang',tongzhizhe) tongzhizhe.Attach(tongzhizhe1) tongzhizhe.Attach(tongzhizhe2) tongzhizhe.setAction('The boss is back') tongzhizhe.Notify() } }

Tatlo, ang tagamasid mode
imahe



package operation import java.util.ArrayList abstract class Subject{ private ArrayList<Observer>observers=new ArrayList<Observer>() public void attach(Observer observer) { observers.add(observer) } public void detach(Observer onserver) { observers.remove(observer) } void Notify() { for(int i=0i<observers.size()i++) { observers.get(i).update() } } } abstract class Observer{ public abstract void update() } class ConcreteSubject extends Subject{ private String subjectState public String getSubjectState() { return subjectState } public void setSubjectState(String subjectState) { this.subjectState = subjectState } } class ConcreteObserver extends Observer{ private String name private String observerState private ConcreteSubject subject public ConcreteObserver(ConcreteSubject subject,String name) { this.name=name this.subject=subject } @Override public void update() { // TODO Auto-generated method stub observerState=subject.getSubjectState() System.out.println('The Observer'+name+'The new status is'+observerState) } } public class Main{ public static void main(String[] args){ ConcreteSubject s=new ConcreteSubject() s.attach(new ConcreteObserver(s,'X')) s.attach(new ConcreteObserver(s,'Y')) s.attach(new ConcreteObserver(s,'Z')) s.setSubjectState('ABC') s.Notify() } }