第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

如果觀察者數(shù)量有許多,有人需要接受下雨通知,有人需要下雪,有人需要晴天,該如何來設(shè)計,肯定不能在目標借口寫邏輯了

如果觀察者數(shù)量有許多,有人需要接受下雨通知,有人需要下雪,有人需要晴天,該如何來設(shè)計,肯定不能在目標借口寫邏輯了,

正在回答

6 回答

觀察者:

com.lone.watcherlombok.lombok.java.util.function.BiConsumerWeatherObserver?Observer?{
????String?BiConsumer<WeatherSubjectWeatherObserver>?(Subject?subject)?{
????????(!(subject?WeatherSubject))?{
????????????IllegalArgumentException()}
????????.accept((WeatherSubject)?subject)}
}


測試:

com.lone.watchercom.lone.watcher.WeatherSubject.Statelombok.extern.slf4j.Main?{
????(String[]?args)?{
????????Observer?girlfriend?=?WeatherObserver((subjectobserver)?->?{
????????????State?state?=?subject.getSubjectState()(State.==?state)?{
????????????????.info(observer.getName()subject.getContent())}
????????})Observer?mother?=?WeatherObserver((subjectobserver)?->?{
????????????State?state?=?subject.getSubjectState()(State.==?state?||?State.==?state)?{
????????????????.info(observer.getName()subject.getContent())}
????????})WeatherSubject?subject?=?WeatherSubject()subject.attach(girlfriend)
????????????????.attach(mother)subject.setSubjectState(State.)subject.setContent()}
}


可否這樣寫,用戶自定義自己的行為,Subject的廣播仍然存在,但在用戶端過濾了。

0 回復 有任何疑惑可以回復我~

實際訂閱者 肯定是在數(shù)據(jù)庫里面的? ? 根據(jù)訂閱者訂閱的天氣分類等 查出? 訂閱者進行發(fā)布

0 回復 有任何疑惑可以回復我~

定義三個observer列表~

0 回復 有任何疑惑可以回復我~

其實就下雨下雪晴天等等這些信息,是來源于數(shù)據(jù),即來自己被觀察者的依賴。通常是交給第三方類處理。

0 回復 有任何疑惑可以回復我~

設(shè)計一種數(shù)據(jù)結(jié)構(gòu),方便用戶訂閱和系統(tǒng)反饋

0 回復 有任何疑惑可以回復我~
public?class?WheatherSubject?{

????private?List<WheatherObserver>?mObservers?=?new?ArrayList<>();
????private?String?wheather;

????public?final?static?String?SNOW?=?"下雪";
????public?final?static?String?RAIN?=?"下雨";
????public?final?static?String?SUNNY?=?"晴";

????public?String?getWheather()?{
????????return?wheather;
????}

????public?void?setWheather(String?wheather)?{
????????this.wheather?=?wheather;
????????notifyObservers(wheather);
????}

????public?void?attach(WheatherObserver?observer)?{
????????mObservers.add(observer);
????}

????public?void?detach(WheatherObserver?observer)?{
????????mObservers.remove(observer);
????}

????public?void?notifyObservers(String?wheather)?{
????????for?(WheatherObserver?observer?:?mObservers)?{
????????????if?(observer.getWheathers().size()?<?1?||?observer.getWheathers().contains(wheather))?{
????????????????observer.update(wheather);
????????????}
????????}
????}
}
public?class?WheatherObserver?{

????private?ArrayList<String>?mWheathers;

????public?WheatherObserver(String...?wheathers)?{
????????mWheathers?=?new?ArrayList<>();
????????mWheathers.addAll(Arrays.asList(wheathers));
????}

????public?ArrayList<String>?getWheathers()?{
????????return?mWheathers;
????}

????public?void?setWheathers(ArrayList<String>?wheathers)?{
????????if?(mWheathers?!=?null)?{
????????????mWheathers.clear();
????????????mWheathers.addAll(wheathers);
????????}
????}

????public?void?update(String?wheather){
????????System.out.print(wheather);
????????Log.i("fy",wheather);
????}
}
public?class?WheaterClint?{

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

????????WheatherSubject?subject?=?new?WheatherSubject();
????????WheatherObserver?observerAll?=?new?WheatherObserver();
????????WheatherObserver?observerSnow?=?new?WheatherObserver(WheatherSubject.SNOW);
????????WheatherObserver?observerSnowAndRain?=?new?WheatherObserver(WheatherSubject.SNOW,WheatherSubject.RAIN);

????????subject.attach(observerAll);
????????subject.attach(observerSnow);
????????subject.attach(observerSnowAndRain);

????????subject.setWheather(WheatherSubject.RAIN);
????}
}


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
觀察者模式
  • 參與學習       55335    人
  • 解答問題       38    個

解決發(fā)布/訂閱問題的觀察者模式,以及何時使用觀察者模式

進入課程

如果觀察者數(shù)量有許多,有人需要接受下雨通知,有人需要下雪,有人需要晴天,該如何來設(shè)計,肯定不能在目標借口寫邏輯了

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號