是否有“更多"信息?抽象工厂模式的真实示例? [英] Is there "more" real world example of Abstract Factory pattern?

查看:61
本文介绍了是否有“更多"信息?抽象工厂模式的真实示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究OPP的设计模式.只是为了给您一些背景.这不是我第一次遇到模式.我已经编程了一段时间(大约十年),并且我对许多编程原理非常熟悉,尤其是SOLID.我主要从事Web应用程序开发,所以也许我缺少在不同编程领域可以学到的东西.

I am currently going through the design patterns of OPP. Just to give you some background. This is not my first time encountering the patterns. I've been programming for a while (decade or so) and I am pretty familiar with many programming principles, especially SOLID. I've been mostly doing web applications development, so maybe I am missing something that could've been learnt in different programming areas.

正如标题所示,我正在努力更深入地了解抽象工厂模式.我确实理解了定义以及何时使用模式"部分,但我仍然不明白这一点.尤其是在将SOLID中的Open闭合原理添加到方程中时.

As the title suggests, I am struggling with grasping deeper the abstract factory pattern. I do understand the definition and also the "when to use the pattern" parts, but I am still missing the point. Especially when adding the Open closed principle from SOLID to the equation.

我是什么意思?

上图取自GOF的设计模式"书.

The image above is taken from the Design Patterns book from GOF.

要使这篇文章真正成为一个问题,我自己找不到两件事的答案:

To make this post really a question of sort, there are two things that I myself can not find answer to:

  1. 当您学习开放封闭原理时,课程应该保持封闭以进行修改.结果导致的实现近似于策略模式.当您查看AbstractFactory的方法结构时,它将其创建的所有产品列为单独的方法.这意味着当我们要向该系列中添加新产品时,我们需要修改AbstractFactory并创建新方法.这不是违反开放封闭原则吗?也许没有更好的方法来做到这一点,是吗?

  1. When you study Open closed principle it stays that classes should stay closed for modifications. The implementation it results into is something approximating Strategy pattern. When you look at the method structure of AbstractFactory, it lists all the products it creates as separate methods. This means when we want to add new product to the family, we need to modify the AbstractFactory and create new method. Is this not a violation of Open closed principle? Maybe there is not better way to do this, is it?

第二,除了书中使用的示例(WidgetFactory-GOF,PizzaStore-HeadFirst)之外,还有谁能给我提供一些真实的示例?您是否有任何常见的实现可以说是的,这是我通常用Abstract factory实现的东西"?什么时候抽象工厂真正有用?还是我很好地理解了这种模式,但这在Web开发中并不常见?

Secondly, could anyone provide me with some real world example besides the ones used in the books (WidgetFactory - GOF, PizzaStore - HeadFirst)? Do you have any common implementations where you can say "Yes, this is something I usually implement with Abstract factory"? When is Abstract factory truly useful? Or am I understanding the pattern well, but it's just not that common in web development?

我希望我能以某种方式表达出我对模式的不确定性.无论如何,请随时提出后续问题,我们将乐意提供更多详细信息.

I hope I somehow managed to express my uncertainties regarding the pattern. In any case feel free to ask follow up questions, I'll be happy to provide more details.

提前谢谢!

推荐答案

[OCP]实现的结果近似于Strategy模式.

The implementation [OCP] results into is something approximating Strategy pattern.

可能.有多种方法可以实现OCP.伯特兰·迈耶(Bertrand Meyer)最初在定义原则时就宣称继承.

Possibly. There are multiple ways to fulfill the OCP. Bertrand Meyer originally touted inheritance when he defined the principle.

...当我们要将新产品添加到该系列时,我们需要修改AbstractFactory并创建新方法.这不是违法吗?

...when we want to add new product to the family, we need to modify the AbstractFactory and create new method. Is this not a violation...

可能.这取决于您如何实现该模式,但是GoF书确实在第90页上承认了此问题.

Possibly. It depends on how you implement the pattern, but the GoF book does acknowledge this problem on page 90.

支持新型产品很困难.将抽象工厂扩展到生产新产品并不容易.那是因为AbstractFactory界面修复了可以创建的产品集.支持新的产品需要扩展工厂界面,这涉及更改AbstractFactory类及其所有子类.我们讨论一种解决方案实施部分中的问题.

Supporting new kinds of products is difficult. Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses. We discuss one solution to this problem in the Implementation section.

GoF书中提到的解决方案是对AbstractFactory方法进行参数化,这将导致其自身的一系列问题.

The solution mentioned in the GoF book is to parameterize the AbstractFactory method, which results in its own set of problems.

...这在Web开发中并不常见吗?

...it's just not that common in web development?

不,不是.在编写GoF本书时,依赖注入并不是一种常见的模式,但是今天它几乎取代了对抽象工厂的需求.我几乎愿意走这么远,以至于今天将Abstract Factory称为反模式.但是我仍然持开放态度,认为可能存在比DI更适合AF的问题.我只是还没看过.

No, it's not. Dependency Injection was not a common pattern when the GoF book was written, but today it has all but replaced the need for Abstract Factories. I would almost be willing to go so far as to call Abstract Factory an anti-pattern today; but I remain open-minded that there could be a problem better suited to AF than DI. I just haven't seen one yet.

这篇关于是否有“更多"信息?抽象工厂模式的真实示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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