这实际上是对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
分享到:
相关推荐
《Head First Design Patterns》是一本深受开发者喜爱的设计模式书籍,由Eric Freeman和Elisabeth Robson合著。这本书以其独特的教学方式,通过丰富的图像、幽默的插图以及易于理解的实例,深入浅出地介绍了设计模式...
《Head First设计模式》是一本深受开发者喜爱的设计模式入门书籍,其官方源码库"Head-First-Design-Patterns-master.zip"包含了书中所讲解的各种设计模式的实际代码示例,旨在帮助读者更深入地理解并应用这些模式。...
在"Observer HeadFirst design pattern"中,作者通过生动的比喻和互动式的例子,帮助读者深入理解观察者模式的核心概念和实现方式。 观察者模式的核心思想是将"主题"(Subject)与"观察者"(Observer)解耦,主题...
在本文中,我们将深入探讨设计模式的核心概念,并结合"Head First DesignPattern_src"中的源码,详细解析一些关键的设计模式。 1. 单例模式(Singleton): 单例模式确保一个类只有一个实例,并提供全局访问点。在...
《Head First设计模式》是一本深受程序员喜爱的经典书籍,它以非扫描的原生版形式呈现,确保了阅读的清晰度和舒适性。这本书主要针对Java编程语言,深入浅出地讲解了设计模式这一核心软件工程概念。设计模式是经验...
《Head First Design Patterns》是一本深入浅出介绍设计模式的图书,由Eric Freeman、Elisabeth Freeman、Bert Bates和Kathy Sierra联合编写。本书结合认知科学原理和神经生物学研究,采用引导式教学方法,通过故事...
根据提供的文件信息,“Head First Design Patterns中文版”这一标题与描述明确指出了这是一本关于设计模式的书籍中文版本。由于具体内容部分并没有提供书中的实际信息,因此将基于标题、描述以及一般理解来生成相关...
《HeadFirst设计模式》是一本深受读者喜爱的设计模式入门书籍,以其独特的图文并茂、通俗易懂的讲解...下载提供的"Oreilly - Head First Design Patterns.pdf"文件,你将开启一段充满乐趣和启示的设计模式学习之旅。
《HeadFirst设计模式源代码》压缩包包含了作者Eric Freeman和Elisabeth Robson在《HeadFirst设计模式》一书中所讲解的各种设计模式的实例代码。这本书是面向初学者的,用直观、生动的方式介绍了设计模式的概念,使得...
撤销功能的实现——备忘录模式(三) 撤销功能的实现——备忘录模式(四) 撤销功能的实现——备忘录模式(五) 观察者模式-Observer Pattern 对象间的联动——观察者模式(一) 对象间的联动——观察者模式(二) ...
《Head First设计模式》是一本深受设计师们喜爱的经典著作,由Eric Freeman和Elisabeth Robson合著。这本书以其独特的视觉呈现方式和易懂的语言,深入浅出地讲解了设计模式这一复杂的概念,使得初学者也能快速掌握。...
在这个名为"JAVA design pattern-java设计模式"的CHM文件中,我们可能找到了关于如何在Java开发中应用设计模式的详细信息。 设计模式通常分为三大类:创建型、结构型和行为型。创建型模式关注对象的创建,如单例...
《Head First Design Patterns》是设计模式领域内一本非常知名的书籍,以其独特的教学方式深受程序员喜爱。这本书通过生动、直观的方式讲解了23种经典的设计模式,帮助开发者提升软件设计能力和可维护性。随书附带的...
### Head First Design Patterns 英文版 #### 书籍概述 《Head First Design Patterns》是一本在软件开发领域广受好评的设计模式入门书籍。本书由Kathy Sierra与Bert Bates共同编写,采用了一种独特而富有创意的...
《Head-First 设计模式》是一本非常著名的设计模式书籍,它以易理解、生动有趣的方式介绍了23种经典的设计模式。源码是基于Java语言编写的,这为读者提供了实际操作和学习设计模式的实践机会。以下是这些设计模式的...
"DesignPattern-master.zip"这个压缩包很可能包含了一个关于设计模式的项目或教程资源,用于学习和理解各种设计模式。 在这个“DesignPattern-master”目录中,我们可以期待找到不同类型的资源,如源代码示例、解释...
设计模式(Design Pattern)是软件工程中的一种经验总结,它是在特定上下文中为解决常见问题而提出的一套可复用的解决方案。设计模式并不直接实现为代码,而是提供了一种在面向对象设计中如何处理常见问题的指南。...
本资料包“DesignPattern - DesignPattern.zip”提供了对C++11中23种设计模式的全面讲解,特别是结合指针使用的部分,以下是对这些知识点的详细阐述: 1. **单例模式(Singleton)**:确保一个类只有一个实例,并...
本资源"23种设计模式的C++实现DesignPattern-master.zip"包含了以下设计模式的C++实现: 1. **创建型模式**: - **单例模式(Singleton)**:保证一个类只有一个实例,并提供全局访问点。 - **工厂方法模式...