工厂方法(1)vs工厂(2)vs Builder(3)模式 [英] factory method (1) vs factory(2) vs Builder (3) pattern

查看:96
本文介绍了工厂方法(1)vs工厂(2)vs Builder(3)模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用(1),(2),(3)的用例是什么.什么是pro&缺点使用它.它们之间有什么区别?

What is use cases for use (1),(2),(3). What is pro & cons to use it. What is difference between them?

推荐答案

工厂方法模式

此模式与工厂模式非常相似,客户端还从类层次结构中向工厂询问特定类型的对象,但是 factory类的Create方法将特定对象的创建委托给派生对象classes ,并返回客户要求的类型的类的对象.本质上,您只有一个联系点即可创建一个类层次结构的多个对象.

Factory Method pattern

This pattern is very similar to the Factory Pattern, the client also asks the Factory for a specific type of object from a class hierarchy but the Create method of the factory class delegates the creation of the specific object to the derived classes and return the object of the class of the type asked by client. In essence, you have a single point of contact for the creation of several objects of a class hierarchy.

您可以认为这是去飞机票柜台(控制器)并通过提供您对机票类型的偏好(头等舱,商务舱或经济舱)来索要机票.即使在对象表示形式中,头等舱机票和经济舱机票都是从基础机票舱位中派生的,用户也不必担心其票证的生成方式.

You can think of this as going to a airline ticket counter (controller) and asking for a ticket by giving your preference of the ticket type (first class, executive or economy). The user is not concerned with how the ticket is being generated, even though in an object representation the first class and the economy ticket are both derived from the base ticket class.

何时使用

  • 灵活性很重要(低耦合)
  • 对象可以在子类中扩展
  • 有一个特定的原因会导致选择一个子类而不是另一个子类-这种逻辑构成了Factory方法的一部分.
  • 客户将职责委派给并行层次结构中的子类.


此模式与Factory方法模式非常相似.但是与工厂方法模式不同,此模式要简单一些.除了将创建委托给子类之外,Factory本身的Create方法创建所需类型的实例并返回它.

This pattern is very similar to the Factory method pattern. But unlike a factory method pattern, this pattern is a bit simpler. Instead of delegating the creation to the subclasses, the Create method of the Factory itself creates the instance of the required type and returns it.


在构建器模式中,创建对象的复杂任务封装在类或方法中.例如,考虑在快餐店点餐的情况.餐点通常包括汉堡,薯条和饮料.餐中的每个项目都有其自己的创建过程.不必由客户来处理每一项的创建过程,而是由一个点餐的柜台来处理此任务.下订单时,柜台人员负责创建由这三个项目组成的餐点,并将这些项目作为一顿餐点返回给客户.

In the builder pattern, the complex task of creation of objects is encapsulated in a class or method. For example, consider the case of ordering a meal at a fast food counter. The meal would typically consists of a burger, fries and a drink. Each item in the meal has its own creation process. Rather than the customer having to deal with the creation process of each item, this task is handled by the counter where one orders the meal. When the order is placed, the person as the counter takes charge of creating the meal that consists of the three items and returns the items as a single instance of a meal to the customer.

当另一位顾客可能要求一顿包含大薯条和减肥可乐的饭菜时.同样,订单柜台的人员负责建立与第一个订单不同的订单.从客户的角度来看,总是下订单和柜台,然后退还餐点.

While another customer may ask for a meal that comes with large fries and diet coke. Again, the person at the order counter is responsible for building the order that was different than the first one. From the point of view of the customer, the order is always placed and the counter, which is followed by the meal being returned.

何时使用

  • 构造对象不是简单的任务
  • 子部分组成每个对象
  • 客户需要一种以上的最终对象(其中有不同的子部分).对不同最终对象的这种需求可能会发生,即使不是同时发生,至少在不同的时间点也会发生.

更多信息

  • Design Patterns: Abstract Factory vs Factory Method
  • Questions about the Prototype Pattern
  • Builder Pattern vs Factory Pattern
  • Creational Patterns
  • Creational Design Patterns

这篇关于工厂方法(1)vs工厂(2)vs Builder(3)模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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