设计模式-观察者模式(Observer)
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
<p>Observer.java</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:613cf5c0-e32f-4bc0-9128-4562a774f1f0" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 400px; height: 116px;" style=" width: 400px; height: 116px;overflow: auto;">package cn.foxeye.design.observer;
public interface Observer {
void update(String state);} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>Subject.java</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:85481259-b3a5-4731-9e08-0edbd5847752" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 602px; height: 418px;" style=" width: 602px; height: 418px;overflow: auto;">package cn.foxeye.design.observer;
import java.util.ArrayList; import java.util.List;
public abstract class Subject {
private List<Observer> observers = new ArrayList<Observer>();public void attach(Observer observer) {this.observers.add(observer);System.out.println("Attached an Observer. "); }public void dettach(Observer observer) {this.observers.remove(observer);System.out.println("Remove an Observer."); }public void notifyObservers(String state) {for (Observer observer : observers) {observer.update(state);} }} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>ConcreteObserver.java</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:20b56b5f-32bd-4aa2-a61c-cc433a457380" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 605px; height: 540px;" style=" width: 605px; height: 540px;overflow: auto;">package cn.foxeye.design.observer;
public class ConcreteObserver implements Observer {
private String name; private String state;public ConcreteObserver(String name) {this.name = name; }public void update(String state) {setState(state);System.out.println(name + "狀態(tài)修改為:" + state); }public String getState() {return state; }public void setState(String state) {this.state = state; }public String getName() {return name; }public void setName(String name) {this.name = name; }} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>ConcreteSubject.java</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d7aa01ec-f20a-4b25-ae4b-0563db4042ea" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 605px; height: 264px;" style=" width: 605px; height: 264px;overflow: auto;">package cn.foxeye.design.observer;
public class ConcreteSubject extends Subject {
private String state;public String getState() {return state; }public void changeState(String state) {this.state = state;System.out.println("主題狀態(tài)變?yōu)?#xff1a;" + state);this.notifyObservers(state); }} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>ObserverDesignDemo.java</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:c2553ba7-f9fb-4b0f-bc8a-6bc25875ff04" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 628px; height: 309px;" style=" width: 628px; height: 309px;overflow: auto;">package cn.foxeye.design.observer;
public class ObserverDesignDemo {
public static void main(String[] args) {ConcreteSubject subject = new ConcreteSubject();for (int i = 0; i < 10; i++) {Observer observer = new ConcreteObserver("張三" + i);subject.attach(observer);}subject.changeState("已讀");}} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
轉(zhuǎn)載于:https://my.oschina.net/foxeye/blog/140331
總結(jié)
以上是生活随笔為你收集整理的设计模式-观察者模式(Observer)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: http staus汇总
- 下一篇: 统一指令集架构的思考