这次的话题是Factory Method和Abstract Facotry的不同之处以及使用它们的正确时机。
HeadFirst: Wow, an interview with two
patters at once! This is a first for us.
Factory Method: Yeah, I’m not so sure I
like being lumped in with Abstract Factory, you know. Just because we’re both
factory patterns doesn’t mean we shouldn’t get our own interviews.
HeadFirst: Don’t be miffed, we wanted to
interview you together so we could help clear up any confusion about who’s who
for the readers. You do have similarities, and I’ve heard tht people sometimes
get you confused.
Abstract Factory: It is true, there have
been times I’ve been mistaken for Factory Method, and I know you’ve had similar
issues, Factory Method. We’ve both really good at decoupling applications from
specific implementations; we just do it in different ways. So I can see why
people might sometimes get us confused.
Factory Method: Well, it still ticks me
off. After all, I use classes to create and you use objects; that’s totally
different!
HeadFirst: Can you explain more about that,
Factory Method?
Factory Method: Sure. Both Abstract Factory
and I create objects – that’s our jobs. But I do it through inheritance…
Abstract Factory: … and I do it through
object composition.
Factory Method: Right. So that means, to
create objects using Factory Method, you need to extend a class and override a
factory method.
HeadFirst: And that factory method does
what?
Factory Method: It creates objects, of
course! I mean, the whole point of the Factory Method Pattern is that you’re
using a subclass to do your creation for you. In that way, clients only need to
know the abstract type they are using, the subclass worries about the concrete
type. So, in other words, I keep clients decoupled from the concrete types.
Abstract Factory: And I do too, only I do
it in a different way.
HeadFirst: Go on, Abstract Factory… you
said something about object composition?
Abstract Factory: I provide an abstract
type for creating a family of products. Subclasses of this type define how
those products are produced. To use the factory, you instantiate one and pass
it into some code that is written against the abstract type. So, like Factory
Method, my clients are decoupled from the actual concrete products they use.
HeadFirst: Oh, I see, so another advantage
is that you group together a set of related products.
Abstract Factory: That’s right.
HeadFirst: What happens if you need to
extend that set of related products, to say add another one? Doesn’t that
require changing your interface?
Abstract Factory: That’s true; my interface
has to change if new products are added, which I know people don’t like to do…
Factory Method: <snicker>
Abstract Factory: What are you snickering
at, Factory Method?
Factory Method: Oh, come on, that’s a big
deal! Changing your interface means you have to go in and change the interface
of every subclass! That sounds like a lot of work.
Abstract Factory: Yeah, but I need a big
interface because I am used to create entire families of products. You’re only
creating one product, so you don’t really need a big interface, you just need
one method.
HeadFirst: Abstract Factory, I heard that
you often use factory methods to implement your concrete factories?
Abstract Factory: Yes, I’ll admit it; my
concrete factories often implement a factory method to create their products. In
my case, they are used purely to create products…
Factory Method: …while in my case I usually
implement code in the abstract creator that makes use of the concrete types the
subclasses create.
HeadFirst: It sounds like you both are good
at what you do. I’m sure people like having a choice; after all, factories are
so useful, they’ll want to use them in all kinds of different situations. You
both encapsulate object creation to keep applications loosely coupled and less
dependent on implementations, which is really great, whether you’re using Factory
Method or Abstract Factory. May I allow you each a parting word?
Abstract Factory: Thanks. Remember me,
Abstract Factory, and use me whenever you have families of products you need to
create and you want to make sure your clients create products that belong
together.
Factory Method: And I’m Factory Method; use
me to decouple your client code from the concrete classes you need to
instantiate, or if you don’t know ahead of time all the concrete classes you
are going to need. To use me, just subclass me and implement my factory method!
分享到:
相关推荐
资源分类:Python库 所属语言:Python 资源全名:facto-1.0.0.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
virtio: Towards a De-Facto Standard For Virtual I/O Devices
Best-selling service technology author Thomas Erl has brought together the de facto catalog of design patterns for modern cloud-based architecture and solution design. More than two years in ...
In this paper we describe Google Vizier, a Google-internal service for performing black-box optimization that has become the de facto parameter tuning engine at Google. Google Vizier is used to ...
is threefold: first, the paper serves as a guideline for people seeking to design their own monocular SLAM according to specific environmental constraints. Second, it presents a survey that covers the...
新项目 :sparkles: 使用Create Snowpack应用程序(CSA)进行引导。可用脚本npm开始在开发模式下运行应用程序。 打开在浏览器中查看它。 如果进行编辑,页面将重新加载。 您还将在控制台中看到任何棉绒错误。...
Title: Mastering Apache ...Chapter 4: Build Lifecycles Chapter 5: Maven Plugins Chapter 6: Maven Assemblies Chapter 7: Maven Archetypes Chapter 8: Maven Repository Management Chapter 9: Best Practices
De facto 项目的目的是创建一个示例 Spring 实现,旨在通过提供基本的安全性、数据访问和其他框架组件来快速启动项目。
DeFacto是一个通过查找确认来源来验证语句的框架。 它以一条声明(例如“ Jamaica Inn由Alfred Hitchcock指挥”)作为输入,然后尝试通过在网络上搜索信息()来寻找该声明的真实性的证据。 :hot_beverage: 版本 ...
PrestoFActo提案坎蒂涅和德斯科雷耶斯活动的逻辑学免费课程Cette应用程序网络性能组织(Mairie,协会等),由家庭成员,保护性家庭成员和保管场所的维护者组成。 预设和不可修改的参数。组织联合国组织(Mairie,...
The first part is an introduction that will help the readers get acquainted with big data environments, whereas the second part will contain a hardcore discussion on all the concepts in analytics on ...
By proceeding to use this tool, you agree not to hold it's author accountable for any damages that could potentially arise from it's use or misuse. --------------------------------------------------...
Practical Research Planning and design Practical Research: Planning and Design is a broad-spectrum, cross-disciplinary book suitable for a wide variety of courses in basic research methodology. ...
Without a clear and well- defined architecture, most developers and architects will resort to the de facto standard traditional layered architecture pattern (also called the n-tier architecture), ...
virtio
The Second Edition of Joe Celko’s Trees and Hierarchies in SQL for Smarties covers two new sets of extensions over three entirely new chapters and expounds upon the changes that have occurred in SQL...
the de facto standard traditional layered architecture pattern also called the n tier architecture creating implicit layers by separating source code modules into packages Unfortunately what often ...
If you are interested in learning and mastering Swift in Apple's ecosystem, namely mobile development, then this book is for you. Table of Contents Swift Basics – Variables and Functions Getting ...
If you are interested in learning and mastering Swift in Apple's ecosystem, namely mobile development, then this book is for you. Table of Contents Swift Basics – Variables and Functions Getting ...
With Pro iOS Web Design and Development, you’ll design websites and develop web applications for iPhone and iPad using web standards deployed with Apple's Safari browser. Utilizing the very latest ...