Observer Pattern

Description

this week we have a dojo about Observer Pattern. We have do a footballGame.When game is fininshed, fans and reports need to give reactions about the results.This is the first design pattern I have learned.

Question

  1. Why this dojo is about Observer Pattern?
    Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.In this dojo, when the game results modified, fans and reports are notified automatically.So this dojo need to use Observer Pattern.

  1. How many actor classes observer pattern uses ?
    Subject, ConcreteSubject, Observer,ConcreteObserver. Subject, Observer and Client. Subject is an object having methods to attach and detach observers to a client object. We have created an abstract class Observer and a concrete class Subject that is extending class Observer.

  1. how to implemente it?
observe partern.jpg

Step1: create subject class

public class FootballGame {
    private List<Spectator> spectators;
    public FootballGame(List<Spectator> spectators) {
        if (spectators == null) {
            spectators = new ArrayList<>();
        }
        this.spectators = spectators;
    }
    public void attach(Spectator spectator) {
        spectators.add(spectator);
    }
    public void notifyAllObservers(String scoringTeam) {
        for (Spectator reporter : spectators) {
            reporter.reactToGoal(scoringTeam);
        }
    }

Step 2
Create Observer class. Observer.java

public abstract class Observer {
     public String reactToGoal(String scoreTeam);
}

Step3
create a speccify observe

public class Reporter implements Spectator {
    public String reactToGoal(String scoringTeam) {
        return "GOAL by " + scoringTeam;
    }
}

  1. observer pattern applies?
    an abstract model has two aspects, one of which depends on the other; an object change will cause the other one or more objects to change, without knowing how many objects will change; an object must notify other objects , And do not know who these objects are; need to create a trigger chain in the system.

Thinking

Althought, observer pattern is a good way .but I am thinking that
If an observer has a lot of direct and indirect observers, it will take a lot of time to notice all the observers...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,220評(píng)論 0 23
  • 最反感三類人。第一類,自我感覺特別良好總覺得自己超懂得別人想法,明明交集甚少話不過三句但是就是一副自己超成熟超懂得...
    竹里er閱讀 339評(píng)論 0 0
  • 最普通的奢華是遺忘, 像離別前輕聲訴回的四季般漫長(zhǎng)。 經(jīng)鋪冗長(zhǎng),用最華麗的詞藻, 如何敘寫最平凡的糟糠。 年輕人裹...
    爬山貓閱讀 1,036評(píng)論 97 62
  • 我對(duì)自己說(shuō),我是唯一的 我也是這么以為的 就是有幾個(gè)不經(jīng)意的場(chǎng)景 讓一切成為了狗屁 我對(duì)自己說(shuō) 我掌控了朋友圈的目...
    門中人閱讀 360評(píng)論 0 0

友情鏈接更多精彩內(nèi)容