抽象工厂模式和工厂方法之间的区别[英] Differences between Abstract Factory Pattern and Factory Method

本文是小编为大家收集整理的关于抽象工厂模式和工厂方法之间的区别的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

从我所阅读的内容中,我看到工厂方法模式允许您定义如何创建单个具体产品,但对客户隐藏实现,因为他们将看到通用产品.我的第一个问题是关于抽象工厂的.它的作用是允许您在其中创建具体对象系列(这可能取决于您使用的特定工厂),而不仅仅是单个具体对象?抽象工厂是否只返回一个非常大的对象或许多对象,具体取决于您调用的方法?

我的最后两个问题是关于我在很多地方看到的我无法完全理解的单引号:

<块引用>

两者之间的一个区别是抽象工厂模式类委派的责任对象实例化到另一个对象通过组合,而工厂方法模式使用继承和依赖一个子类来处理所需的对象实例化.

我的理解是工厂方法模式有一个 Creator 接口,它会让 ConcreteCreator 负责知道要实例化哪个 ConcreteProduct.这就是使用继承来处理对象实例化的意思吗?

现在关于那句话,抽象工厂模式究竟是如何通过组合将对象实例化的责任委托给另一个对象的?这是什么意思?看起来抽象工厂模式在我看来也使用继承来完成构建过程,但我还是在学习这些模式.

任何帮助,尤其是最后一个问题,将不胜感激.

推荐答案

两者的区别

"工厂方法"和"抽象工厂"的主要区别在于工厂方法是单一方法,而抽象工厂是对象.我想很多人会混淆这两个术语,并开始互换使用它们.我记得当我学习它们时,我很难找到确切的区别.

因为工厂方法只是一个方法,它可以在子类中被覆盖,因此你引用的后半部分:

<块引用>

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

引用假设一个对象在这里调用它自己的工厂方法.因此,唯一可以改变返回值的就是子类.

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

<块引用>

... 使用抽象工厂模式,一个类委托对象的责任通过实例化到另一个对象作文...

他们说的是有一个对象 A,他想创建一个 Foo 对象.它不是自己创建 Foo 对象(例如,使用工厂方法),而是获取一个 不同的 对象(抽象工厂)来创建 Foo 对象.

代码示例

为了向您展示区别,这里使用了一个工厂方法:

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();
    }
}

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

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

本文地址:https://www.itbaoku.cn/post/627457.html

问题描述

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