抽象工厂模式与工厂方法的差异 [英] Differences between Abstract Factory Pattern and Factory Method

查看:121
本文介绍了抽象工厂模式与工厂方法的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于这两种模式之间差异的帖子,但是有一些我找不到的东西。



从我以前阅读,我看到工厂方法模式允许您定义如何创建单个具体产品,但隐藏客户端的实现,因为它们将会看到一个通用产品。我的第一个问题是关于抽象工厂。它的作用是允许您创建具体对象的家族(可以依赖于您使用的特定工厂),而不仅仅是单个具体对象?抽象工厂只能根据你所调用的方法返回一个非常大的对象或者很多对象吗?



我的最后两个问题是关于一个单引号,我不能完全理解我已经在许多地方看过:


两者之间的区别在于
与抽象工厂模式,
类将
对象实例化的责任委托给另一个对象
,而Factory
Method模式使用继承,
依赖于一个子类来处理
所需对象实例化。


我的理解是,工厂方法模式有一个创建者界面,将使得ConcreteCreator负责知道哪个ConcreteProduct实例。这是什么意思通过使用继承来处理对象实例化?



现在关于该引用,抽象工厂模式究竟如何将对象实例化的责任委托给另一个对象通过组合?这是什么意思?抽象工厂模式看起来像使用继承来在我的眼睛里进行构造过程,但是我再次仍然在学习这些模式。



最后一个问题,将不胜感激。

解决方案

两者之间的区别



工厂方法和抽象工厂是工厂方法是一种单一的方法,抽象工厂是一个对象。我觉得很多人把这两个术语搞糊涂了,开始互换使用。我记得我很难找到当我学到他们的时候有什么区别。



因为工厂方法只是一种方法,它可以在一个子类中被覆盖,因此您的报价的下半部分:


... Factory Method模式使用
继承并依赖于子类
处理所需的对象
实例化。


报价假定一个对象正在调用它自己的这里的工厂方法。因此,唯一可以改变返回值的东西将是一个子类。



抽象工厂是一个具有多个工厂方法的对象。看看你的报价的前半部分:


...与抽象工厂模式,一个类
委托对象
通过
组合实例化到另一个对象...


他们说的是有一个对象A,谁想要做一个Foo对象。而不是使Foo对象本身(例如使用工厂方法),它将得到一个不同的对象(抽象工厂)来创建Foo对象。



代码示例



为了显示差异,这里是一种使用的工厂方法:

  class A {
public void doSomething(){
Foo f = makeFoo();
f.whatever();
}

保护Foo makeFoo(){
返回新的RegularFoo();
}
}

class B扩展A {
protected Foo makeFoo(){
//子类覆盖工厂方法
/ /返回不同的东西
返回新的SpecialFoo();
}
}

这里是一个使用的抽象工厂: p>

  class A {
private Factory factory;

public A(工厂厂){
this.factory = factory;
}

public void doSomething(){
//具体的f类取决于工厂传递给构造函数的具体类
// 。如果你提供一个
//不同的工厂,你会得到一个不同的Foo对象。
Foo f = factory.makeFoo();
f.whatever();
}
}

界面工厂{
Foo makeFoo();
Bar makeBar();
Aycufcn makeAmbiguousYetCommonlyUsedFakeClassName();
}

//需要在这里实现Factory界面的具体工厂


I know there are many posts out there about the differences between these two patterns, but there are a few things that I cannot find.

From what I have been reading, I see that the factory method pattern allows you to define how to create a single concrete product but hiding the implementation from the client as they will see a generic product. My first question is about the abstract factory. Is its role to allow you to create families of concrete objects in (that can depend on what specific factory you use) rather than just a single concrete object? Does the abstract factory only return one very large object or many objects depending on what methods you call?

My final two questions are about a single quote that I cannot fully understand that I have seen in numerous places:

One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

My understanding is that the factory method pattern has a Creator interface that will make the ConcreteCreator be in charge of knowing which ConcreteProduct to instantiate. Is this what it means by using inheritance to handle object instantiation?

Now with regards to that quote, how exactly does the Abstract Factory pattern delegate the responsibility of object instantiation to another object via composition? What does this mean? It looks like the Abstract Factory pattern also uses inheritance to do the construction process as well in my eyes, but then again I am still learning about these patterns.

Any help especially with the last question, would be greatly appreciated.

解决方案

The Difference Between The Two

The main difference between a "factory method" and an "abstract factory" is that the factory method is a single method, and an abstract factory is an object. I think a lot of people get these two terms confused, and start using them interchangeably. I remember that I had a hard time finding exactly what the difference was when I learnt them.

Because the factory method is just a method, it can be overridden in a subclass, hence the second half of your quote:

... the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

The quote assumes that an object is calling it's own factory method here. Therefor the only thing that could change the return value would be a subclass.

The abstract factory is an object that has multiple factory methods on it. Looking at the first half of your quote:

... with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition ...

What they're saying is that there is an object A, who wants to make a Foo object. Instead of making the Foo object itself (e.g. with a factory method), it's going get a different object (the abstract factory) to create the Foo object.

Code Examples

To show you the difference, here is a factory method in use:

class A {
    public void doSomething() {
        Foo f = makeFoo();
        f.whatever();   
    }

    protected Foo makeFoo() {
        return new RegularFoo();
    }
}

class B extends A {
    protected Foo makeFoo() {
        //subclass is overriding the factory method 
        //to return something different
        return new SpecialFoo();
    }
}

And here is an abstract factory in use:

class A {
    private Factory factory;

    public A(Factory factory) {
        this.factory = factory;
    }

    public void doSomething() {
        //The concrete class of "f" depends on the concrete class
        //of the factory passed into the constructor. If you provide a
        //different factory, you get a different Foo object.
        Foo f = factory.makeFoo();
        f.whatever();
    }
}

interface Factory {
    Foo makeFoo();
    Bar makeBar();
    Aycufcn makeAmbiguousYetCommonlyUsedFakeClassName();
}

//need to make concrete factories that implement the "Factory" interface here

这篇关于抽象工厂模式与工厂方法的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆