抽象工厂和工厂设计模式之间有什么区别? [英] What are the differences between Abstract Factory and Factory design patterns?

查看:130
本文介绍了抽象工厂和工厂设计模式之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于这两种模式之间差异的文章,但是有些事情我找不到。



从我的经历阅读中,我看到工厂方法模式允许您定义如何创建单个具体产品,但对客户端隐藏了实现,因为他们将看到通用产品。我的第一个问题是关于抽象工厂。它的作用是允许您在其中创建具体对象系列(取决于您使用的特定工厂),而不仅仅是一个具体对象?



我的最后两个问题是关于一个单引号,我无法完全理解该抽象工厂是否只返回一个非常大的对象或根据调用的方法而返回许多对象?我在很多地方都看到过:


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


我的理解是,工厂方法模式具有一个Creator接口,它将使ConcreteCreator负责知道要向哪个ConcreteProduct进行操作。实例化。



现在关于那个引用,抽象工厂模式到底如何将对象实例化的职责委托给另一个对象呢?通过合成对象?这是什么意思?在我看来,抽象工厂模式也使用继承来完成构建过程,但是我仍然在学习这些模式。



特别是任何帮助最后一个问题,将不胜感激。

解决方案

两者之间的区别



a之间的主要区别工厂方法和抽象工厂是工厂方法是单一方法,抽象工厂是对象。我认为很多人将这两个术语弄混了,并开始互换使用。我记得我在学习它们时很难找到确切的区别。



由于factory方法只是一个方法,因此可以在子类中覆盖它,因此是报价的后半部分:


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


引号假定对象正在调用自己的 工厂方法。因此,唯一可以改变返回值的是子类。



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


...具有Abstract Factory模式,
类委派了对象
通过
组合实例化到另一个对象...


他们在说的是想要创建Foo对象的对象A。代替创建Foo对象本身(例如,使用工厂方法),将获得一个不同对象(抽象工厂)来创建Foo对象。



代码示例



为显示差异,以下是使用的工厂方法:



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

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

B类扩展了A {
受保护的Foo makeFoo(){
//子类覆盖了工厂方法
/ /返回其他内容
返回new SpecialFoo();
}
}

这是一个正在使用的抽象工厂:

  class A {
private Factory factory;

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

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

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

//需要在此处构建实现工厂接口的具体工厂


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 its own factory method here. Therefore 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 to 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天全站免登陆