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

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

问题描述

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

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.

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

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 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 ...

他们说的是有一个对象 A,他想创建一个 Foo 对象.与创建 Foo 对象本身(例如,使用工厂方法)不同,它将获得一个不同对象(抽象工厂)来创建 Foo 对象.

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.

为了向您展示不同之处,这里使用了一个工厂方法:

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天全站免登陆