这次的话题是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:走向事实标准的虚拟I/O设备 #### 摘要 本文探讨了Linux内核当前支持的多种虚拟化系统(如Xen、KVM、VMware VMI等),以及这些系统各自拥有的独特驱动程序所带来的挑战。为了解决这一问题,作者介绍了...
此外,文档中还包含了许多行业默认标准(de-facto standards),它们在计算机行业中被广泛采用。 #### 三、标准类型与文档结构 VESA标准分为两种类型:标准和指南。标准通常是针对更广泛的应用场景,而指南则更多地...
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 ...
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开始在开发模式下运行应用程序。 打开在浏览器中查看它。 如果进行编辑,页面将重新加载。 您还将在控制台中看到任何棉绒错误。...
《Spring实战:De facto项目与快速启动开源项目》 在当今的软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java企业级应用的事实标准。它为开发者提供了丰富的工具集,帮助构建高效、可维护的系统。本文...
- **Defacto Half-Width Mechanical Standards**:半宽机械标准,通常指的是行业内的非正式标准,但被广泛接受并采用。 - **LXI Unit Half-Width Mechanical Standards**:LXI联盟定义的半宽度机械标准,为LXI设备...
- **示例**:`SELECT LENGTH('Ipso Facto') AS ergo FROM dual;` - 计算字符串“Ipso Facto”的长度。 8. **LPAD() / RPAD()** - **功能**:在字符串的左侧或右侧填充字符,直到达到指定长度。 - **语法**:`...
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 ...
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), ...
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. --------------------------------------------------...
VirtIO是一种I/O虚拟化标准,它为虚拟机提供了一种高效、标准化的方式来访问虚拟化环境中的I/O设备。在本文档中,主要讨论了virtio的实现、其API层、vring传输机制,以及如何将其以PCI设备的形式嵌入到完全虚拟化的...
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 ...