`

Head First Design Pattern - 2 - A Subject and Observer spar

阅读更多
这实际上是对Observer模式中使用push方式还是pull方式的讨论,我从第一句话就开始笑个不停了,你要不要来试试看?

Subject: I'm glad we're finally getting a chance to chat in person.
Observer: Really? I thought you didn't care much about us Observers.
Subject: Well, I do my job, don't I? I always tell you what's going on... Just because I don't really know who you are doesn't mean I don't care. And besides, I do know the most important thing about you --- you implement the Observer interface.
Observer: Well yeah, but that's just a small part of who I am. Anyway, I know a lot more about you...
Subject: Oh yeah, like what?
Observer: Well, you're always passing your state around to us Observers so we can see what's goning on inside you. Which gets a little annoying at times...
Subject: Well excuuuse me. I have to send my state with my notifications so all you lazy Observers will know What happened!
Observer: Ok, wait just a minute here; first, we're not lazy, we just have other stuff to do in between your oh-so-important notifications, Mr. Subject, and second, why don't you let us come to you for the state we want rather than pushing it out to just everyone?
Subject: Well... I guess that might work. I'd have to open myself up even more though to let all you Observers come in and get the state that you need. That might be kind of dangerous. I can't let you come in and just snoop around looking at everything I've got.
Observer: Why don't you just write some public getter methods that will let us pull out the state we need?
Subject: Yes, I could let you pull my state. But won't that be less convenient for you? If you have to come to me every time you want something, you might have to make multiple method calls to get all the state you want. That's why I like push better... then you have everything you need in one notification.
Observer: Don't be so pushy! There's so many different kinds of us Observers, there's no way you can anticipate everything we need. Just let us come to you to get the state we need. That way, if some of us only need a little bit of state, we aren't forced to get it all. It also makes things easier to modify later. Say, for example, you expand yourself and add some more state, well if you use pull, you don't have to go around and change the update calls on every observer, you just need to change yourself to allow more getter methods to access you additional states.
Subject: Well, I can see the advantages to doing it both ways. I have noticed that there is a built-in Java Observer Pattern that allows you to use either push or pull.
Observer: Oh really? I think we're going to look at that next...
Subject: Great... maybe I'll get to see a good example of pull and change my mind.
Observer: What, us agree on something? I guess there's always hope
分享到:
评论

相关推荐

    Observer HeadFirst design pattern

    Observer HeadFirst design pattern C++ version

    design-pattern-java.pdf

    撤销功能的实现——备忘录模式(三) 撤销功能的实现——备忘录模式(四) 撤销功能的实现——备忘录模式(五) 观察者模式-Observer Pattern 对象间的联动——观察者模式(一) 对象间的联动——观察者模式(二) ...

    02-observer-pattern

    head-first-design-pattern—02-observer-pattern(观察者模式),融入了个人的见解,里面包含错误的实现和正确的标准实现,你可以对比学习,加深对模式的理解

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》编辑推荐:强大的写作阵容。《Head First Design Patterns》作者Eric Freeman;ElElisabeth Freeman是作家、讲师和技术顾问。Eric拥有耶鲁大学的计算机科学博士学位,E1isabath拥有...

    Head First Design Patterns

    Head First设计模式,非常经典的设计模式教程。 《Head First设计模式》共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍这本书的用法; 第1章到第11章陆续介绍的设计模式...

    Head First Design Patterns 高清英文版

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    observer-pattern-demo 观察者模式示例

    observer-pattern-demo 观察者模式示例

    head first 设计模式-设计模式必读

    《Head First设计模式》(中文版)共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍这本书的用法;第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、...

    resize-observer-polyfill:用于Resize Observer API的polyfill

    安装从NPM: npm install resize-observer-polyfill --save-dev 从凉亭: (将在下一个主要版本中删除) bower install resize-observer-polyfill --save-dev浏览器支持Polyfill已在以下浏览器中经过测试: 注意

    Head First Design Patterns(英文,无水印,完整版)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、State、Proxy。*后三章比较...

    ExtremeLearningMachine资源共享-Nonlinear-discrete-time-neural-network-observer_2013_Neurocomputing.pdf

    ExtremeLearningMachine资源共享-Nonlinear-discrete-time-neural-network-observer_2013_Neurocomputing.pdf 小弟准备学习ELM,才收集到一些相关资料,发现论坛中并无相关资料,因此把自己手头上收集到的共享给...

    vue-resize-observer:Vue调整大小观察器

    Vue调整大小观察器 English |安装Vue3.0 npm install --save vue-resize-observer@next Vue2.0 npm install --save vue-resize-observer 模块导入和示例导入软件包并将其安装到Vue中: const VueResizeObserver = ...

    2016-Design Method of Adaptive Full Order Observer With or Witho

    2016_Design Method of Adaptive Full Order Observer With or Without Estimated Flux Error in Speed Estimation Algorithm 王教授的论文; 2016_Design Method of Adaptive Full Order Observer With or ...

    com.designpattern.state_observer.rar

    用state模式和observer模式一些模式QQ在线状态 com.designpattern.state_observer.rar

    深入浅出设计模式中文

    Head First Design Pattern 中文版本 1 Welcome to Design Patterns: an introduction 1 2 Keeping your Objects in the know: the Observer Pattern 37 3 Decorating Objects: the Decorator Pattern 79 4 Baking ...

    Head First设计模式中文版及示例源码

    《Head First设计模式》作者Eric Freeman;ElElisabeth Freeman是作家、讲师和技术顾问。本书的产品设计应用神经生物学、认知科学,以及学习理论,这使得这本书能够将这些知识深深地印在你的脑海里,不容易被遗忘。...

    design-pattern-observer-api

    观察者模式 行为模式 在对象之间定义一对多的依赖关系,以便当一个对象更改状态时,将自动通知和更新其所有依赖关系。 在此示例中,我们要监视节点上的子创建/删除事件(可用于监视文件夹) 源代码源于以下UML图:

    Node.js Design Patterns Second Edition[July 2016]

    Chapter 2, Node.js Essential Patterns, introduces the first steps towards asynchronous coding and design patterns with Node.js discussing and comparing callbacks and the event emitter (observer ...

Global site tag (gtag.js) - Google Analytics