简单工厂和工厂方法模式的动机 [英] Motivation for Simple Factory and Factory Method Pattern

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

问题描述

我知道有很多不同的工厂模式的差异的问题,但答案是如此不同和混乱。我读的书使用不清楚(过)简化的例子。在阅读维基百科解释之后,我还有很多问题,还有很多关于这些问题的在线解释,包括这些网站上的所有内容。我正在阅读的书是头部设计模式。



在简单工厂中,客户端使用单独的类(创建者),工厂方法(可以是静态的)返回产品。



在工厂方法模式中,创建者和客户端是相同的,他们在同一个类中使用抽象方法来创建新的产品,同一班当然,创造者(或客户)是抽象的,所以关于制作混凝土产品的决定推迟到子类。


  1. 我的理解是否正确(例如,客户和创建者在FMP中是一样的,我从来没有看到客户端在FMP图中)?


  2. 在工厂方法模式中,接口创建方法在创建者之外不可重用,因此只能在创建新的创建者时重复使用


  3. 我可以选择其中一种情况是什么?


  4. <

    (PS请不要将此标记为重复,我想在此网站上清除此事)

    解决方案

    Simple Factory是工厂。因为它不能解决优雅的问题,因为对于每个新的产品子类,您将必须在create()方法中编辑switch语句。这违反了开放/关闭原则。使简单工厂有用的一个潜在方法是在这里使用类别注册: http:// www.oodesign.com/factory-pattern.html



    Factory Method是一种工厂,形式为方法(因此名称)。这不违反开放/关闭原则,因为您通过扩展处理变更,而不是修改代码。



    <你的理解是正确的。 FMP中的客户端和创建者/工厂是相同的,因为工厂(方法)是客户端的一部分。



    确实,FMP中的create方法是不可重用的。这是可以的,因为这不是试图创建应用程序范围的产品的工厂,而是客户端创建他的依赖对象而不使用新的的方法。



    我不能回答你第三个问题,因为我相信它是基于偏好。


    I know there are a lot of questions out there about differences of different factory patterns, but the answers are so different and confusing. The books that i read use unclear and (over)simplified examples. I have a number of questions even after reading Wikipedia explanations, and a lot of online explanations about them including all on these site. The book that I'm currently reading is Head First Design Patterns.

    In Simple Factory the client uses separate class (Creator) with factory method (which CAN be static) to return Products.

    In Factory Method Pattern the Creator and the Client are the same thing and they use abstract method in the same class to create new Products, on which they operate in that same class. Of course the Creator (or Client) are abstract, so the decision about making the Concrete Product is deferred to sub-classes.

    1. Is my understanding correct (for ex. are the Client and Creator in FMP the same thing, I never see Client in FMP diagram)?

    2. In Factory Method Pattern it seams that the create method is not reusable outside of the Creator, so it can only be reused when making some new Creator?

    3. What are the situations where I can choose one over the other?

    (P.S. Please don't mark this as duplicate, I want to make this thing clear on this site)

    解决方案

    Simple Factory is a factory in the form of a class. Because of that it doesn't solve the problem with elegance, since for every new subclass of Product you will have to edit the switch statement in the create() method. This is a violation of the Open/Close Principle. A potential way to make a Simple Factory useful would be to use class registration as sawn here: http://www.oodesign.com/factory-pattern.html

    Factory Method is a factory in the form of a method (hence the name). This doesn't violate the Open/Close Principle since you deal with change by extending and not by modifying code.

    Your understanding is correct. Client and Creator/Factory in FMP are the same since the Factory (method) is part of the Client.

    It is true that the create method in FMP is not reusable. That is ok though, because this is not an attempt to create an application-wide Factory of the Product, but a way for the Client to create his depended objects without using new.

    I cannot answer you third question since I believe it is based on preference.

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

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