成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

觀察者模式j(luò)ava代碼 java 觀察者模式 流程審批

JAVA觀察者模式(observer pattern)

import?java.util.ArrayList;

成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元普蘭做網(wǎng)站,已為上家服務(wù),為普蘭各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

import?java.util.HashMap;

import?java.util.Random;

import?javax.swing.event.ChangeEvent;

import?javax.swing.event.ChangeListener;

public?class?StockLists?implements?Runnable

{

private?HashMapString,?Stock?????????????????????hmStocks?=?new?HashMapString,?Stock();

private?HashMapString,?ArrayListChangeListener?hmCL?????=?new?HashMapString,?ArrayListChangeListener();

public?StockLists()

{

init(100);

new?Thread(this).start();

}

public?void?run()

{

Random?rand?=?new?Random();

while?(true)

{

try

{

Thread.sleep(100);

Stock?s?=?randStock();

s.price?=?rand.nextFloat();

ArrayListChangeListener?alCL?=?hmCL.get(s.symbol);

if?(alCL?==?null)

{

continue;

}

for?(ChangeListener?cl?:?alCL)

{

cl.stateChanged(new?ChangeEvent(s));

}

}

catch?(InterruptedException?e)

{

e.printStackTrace();

}

}

}

private?Stock?randStock()

{

Random?rand?=?new?Random();

int?idx?=?rand.nextInt(hmStocks.size());

String?sym?=?(String)?hmStocks.keySet().toArray()[idx];

Stock?s?=?hmStocks.get(sym);

return?s;

}

public?boolean?addChangeListener(ChangeListener?cl,?String?sym)

{

ArrayListChangeListener?al?=?hmCL.get(sym);

if?(al?==?null)

{

al?=?new?ArrayListChangeListener();

hmCL.put(sym,?al);

}

return?al.add(cl);

}

private?void?init(int?count)

{

Random?rand?=?new?Random();

for?(int?i?=?0;?i??count;?i++)

{

String?sym?=?randSym(rand);

float?p?=?rand.nextFloat();

Stock?s?=?new?Stock(sym,?p);

hmStocks.put(sym,?s);

}

}

private?String?randSym(Random?rand)

{

char[]?ca?=?new?char[3];

for?(int?i?=?0;?i??ca.length;?i++)

{

ca[i]?=?(char)?('A'?+?rand.nextInt(26));

}

return?new?String(ca);

}

public?static?void?main(String[]?args)

{

StockLists?sl?=?new?StockLists();

sl.addChangeListener(new?Investor("111"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("222"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("333"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("444"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("555"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("666"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("777"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("888"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("999"),?sl.randStock().symbol);

sl.addChangeListener(new?Investor("000"),?sl.randStock().symbol);

}

}

class?Stock

{

public?String?symbol?=?null;

public?float??price??=?0.0f;

public?Stock(String?sym,?float?p)

{

symbol?=?sym;

price?=?p;

}

public?String?toString()

{

return?symbol?+?":"?+?price;

}

}

class?Investor?implements?ChangeListener

{

public?String?name?=?"";

public?Investor(String?name)

{

this.name?=?name;

}

@Override

public?void?stateChanged(ChangeEvent?e)

{

Stock?s?=?(Stock)?e.getSource();

System.out.println("Hello,"?+?name?+?"?:?STOCK?CHANGED:?"?+?s);

}

}

java 設(shè)計(jì)模式之 觀察者模式(Observer)

//Subject java

package youngmaster model Observer;

/**

* @author youngmaster

* @E mail:young * @version myEclipse

* @create time 上午 : :

*/

/**

* 察者模式屬于行為型模式 其意圖是定義對(duì)象間的一種一對(duì)多的依賴(lài)關(guān)系

* 當(dāng)一個(gè)對(duì)象的狀態(tài)發(fā)生改變時(shí) 所有依賴(lài)于它的對(duì)象都得到通知并被自動(dòng)更新

* 在制作系統(tǒng)的過(guò)程中 將一個(gè)系統(tǒng)分割成一系列相互協(xié)作的類(lèi)有一個(gè)常見(jiàn)的副作用

* 需要維護(hù)相關(guān)對(duì)象間的一致性 我們不希望為了維持一致性而使各類(lèi)緊密耦合

* 因?yàn)檫@樣降低了他們的可充用性 這一個(gè)模式的關(guān)鍵對(duì)象是目標(biāo)(Subject)和觀察者(Observer)

* 一個(gè)目標(biāo)可以有任意數(shù)目的依賴(lài)它的觀察者 一旦目標(biāo)的狀態(tài)發(fā)生改變 所有的觀察者都得到通知

* 作為對(duì)這個(gè)通知的響應(yīng) 每個(gè)觀察者都將查詢(xún)目標(biāo)以使其狀態(tài)與目標(biāo)的狀態(tài)同步 這種交互也稱(chēng)為發(fā)布 訂閱模式

* 目標(biāo)是通知的發(fā)布者 他發(fā)出通知時(shí)并不需要知道誰(shuí)是他的觀察者 可以有任意數(shù)據(jù)的觀察者訂閱并接收通知

*/

/**

* subject

*目標(biāo)接口

*/

public interface Subject {

public void addObserver(Observer o);

public void deletObserver(Observer o);

public void notice();

}

//Observer java

package youngmaster model Observer;

/**

* @author youngmaster

* @E mail:young * @version myEclipse

* @create time 上午 : :

*/

/**

*觀察者接口

*/

public interface Observer {

public void update();

}

//Teacher java

package youngmaster model Observer;

import java util Vector;

/**

* @author youngmaster

* @E mail:young * @version myEclipse

* @create time 上午 : :

*/

/**

*目標(biāo)接口實(shí)現(xiàn)

*/

public class Teacher implements Subject {

private String phone;

@SuppressWarnings( unchecked )

private Vector students;

@SuppressWarnings( unchecked )

public Teacher() {

phone = ;

students = new Vector();

}

@SuppressWarnings( unchecked )

@Override

public void addObserver(Observer o) {

students add(o);

}

@Override

public void deletObserver(Observer o) {

students remove(o);

}

@Override

public void notice() {

for (int i = ; i students size(); i++) { ((Observer) students get(i)) update();

}

}

public void setPhone(String phone) {

this phone = phone;

notice();

}

public String getPhone() {

return phone;

}

}

//Student java

package youngmaster model Observer;

/**

* @author youngmaster

* @E mail:young * @version myEclipse

* @create time 上午 : :

*/

/**

*觀察者接口實(shí)現(xiàn)

*/

public class Student implements Observer {

private String name;

private String phone;

private Teacher teacher;

public Student(String name Teacher teacher) { this name = name; this teacher = teacher;

}

public void show() {

System out println( Name: + name + \nTeacher s phone: + phone);

}

@Override

public void update() {

phone = teacher getPhone();

}

}

//Client java

package youngmaster model Observer;

import java util Vector;

/**

* @author youngmaster

* @E mail:young * @version myEclipse

* @create time 上午 : :

*/

/**

*測(cè)試類(lèi)

*/

public class Client {

/**

* @param args

*/

@SuppressWarnings( unchecked )

public static void main(String[] args) {

Vector students = new Vector();

Teacher teacher = new Teacher();

for (int i = ; i ; i++) {

Student student = new Student( student + i teacher); students add(student); teacher addObserver(student);

}

teacher setPhone( );

for (int i = ; i ; i++)

((Student) students get(i)) show();

System out println( \n============================\n );

teacher setPhone( );

for (int i = ; i ; i++)

((Student) students get(i)) show();

}

lishixinzhi/Article/program/Java/gj/201311/27566

Java中使用觀察者模式實(shí)現(xiàn)自定義事件

MyEventTest java:

package wintys event;

import javax swing event EventListenerList;

import java util Date;

import java text DateFormat;

import java text SimpleDateFormat;

/**

* Java的事件機(jī)制/自定義事件

運(yùn)行結(jié)果:

do something interesting in source here

listener detects [event]:wintys event MyEvent[source=wintys event MySource@

] [occur at]: : :

listener detects [event]:wintys event MyEvent[source=wintys event MySource@

] [occur at]: : :

* @version

* @author 天堂露珠 ()

* @see

*/

class MyEventTest{

public static void main(String[] args){

MySource source = new MySource();

MyListener myListener = new MyListener(){

public void doMyAction(MyEvent e){

System out println( listener detects + e);

}

};

source addMyListener(myListener);

source addMyListener(myListener);

source addMyListener(myListener);

source removeMyListener(myListener);

source doSomething();

}

}

/**

* 自定義的事件

* @version

* @author 天堂露珠()

* @see

*/

class MyEvent extends java util EventObject{

private Date date;//記錄事件發(fā)生的時(shí)間

public MyEvent(Object source Date date){

super(source);

this date = date;

}

public String toString(){

DateFormat df = new SimpleDateFormat( yyyy MM dd HH:mm:ss );

String dt = df format(date);

return [event]: + super toString() + [occur at]: + dt;

}

}

/**

* 自定義事件監(jiān)聽(tīng)器接口

* @version

* @author 天堂露珠()

* @see

*/

interface MyListener extends java util EventListener{

void doMyAction(MyEvent e);

}

/**

* 自定義事件源

* @version

* @author 天堂露珠()

* @see

*/

class MySource{

/**

* 保存注冊(cè)的監(jiān)聽(tīng)器列表

* 子類(lèi)可以使用它保存自己的事件監(jiān)聽(tīng)器(非MyListener監(jiān)聽(tīng)器)列表

*/

protected EventListenerList listenerList = new EventListenerList();

private MyEvent myEvent = null;//fireDoMyAction()使用此變量

/**

* 沒(méi)有做任何事

*/

public MySource(){

}

/**

* 添加一個(gè)MyListener監(jiān)聽(tīng)器

*/

public void addMyListener(MyListener listener){

listenerList add(MyListener class listener);

}

/**

* 移除一個(gè)已注冊(cè)的MyListener監(jiān)聽(tīng)器

* 如果監(jiān)聽(tīng)器列表中已有相同的監(jiān)聽(tīng)器listener listener

* 并且listener ==listener

* 那么只移除最近注冊(cè)的一個(gè)監(jiān)聽(tīng)器

*/

public void removeMyListener(MyListener listener){

listenerList remove(MyListener class listener);

}

/**

* @return 在此對(duì)象上監(jiān)聽(tīng)的所有MyListener類(lèi)型的監(jiān)聽(tīng)器

*/

public MyListener[] getMyListeners(){

return (MyListener[])listenerList getListeners(MyListener class);

}

//Winty:Copy directly from javax swing event EventListenerList

/*Notify all listeners that have registered interest for

notification on this event type The event instance

is lazily created using the parameters passed into

the fire method

*/

protected void fireDoMyAction() {

// getListenerList() Guaranteed to return a non null array

Object[] listeners = listenerList getListenerList();

// Process the listeners last to first notifying

// those that are interested in this event

for (int i = listeners length ; i= ; i = ) {

if (listeners[i]==MyListener class) {

// Lazily create the event:

if (myEvent == null)

myEvent = new MyEvent(this new Date());

((MyListener)listeners[i+ ]) doMyAction(myEvent);

}

}

}

/**

* 做一些事件源應(yīng)該做的有意義的事 然后通知監(jiān)聽(tīng)器

* 這里只是一個(gè)示例方法

* 例如:MySource如果是一個(gè)按鈕 則doSomething()就可以命名為click()

* 當(dāng)用戶(hù)點(diǎn)擊按鈕時(shí)調(diào)用click()方法

*/

public void doSomething() {

System out println( do something interesting here );

fireDoMyAction();//通知監(jiān)聽(tīng)器

}

}

lishixinzhi/Article/program/Java/hx/201311/26237

本文標(biāo)題:觀察者模式j(luò)ava代碼 java 觀察者模式 流程審批
URL網(wǎng)址:http://jinyejixie.com/article2/dosecic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、云服務(wù)器用戶(hù)體驗(yàn)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)
苏尼特左旗| 茌平县| 连云港市| 夏河县| 赣榆县| 德格县| 馆陶县| 南宁市| 和林格尔县| 宝鸡市| 五台县| 濮阳市| 樟树市| 辽中县| 利辛县| 汝南县| 沙洋县| 中宁县| 樟树市| 桐柏县| 南安市| 周宁县| 大理市| 广德县| 勐海县| 元朗区| 卫辉市| 阿拉善盟| 巩义市| 屏东县| 田东县| 昂仁县| 封丘县| 衡东县| 弥勒县| 阳城县| 南涧| 轮台县| 玉田县| 滦平县| 互助|