抽象工厂与工厂方法(范围) [英] Abstract Factory vs Factory Method (scope)

查看:82
本文介绍了抽象工厂与工厂方法(范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工厂方法是类设计模式。抽象工厂使用许多工厂方法。为什么Abstract Factory是对象设计模式,而不是类设计模式?抽象工厂模式将实例化延迟到哪个对象?

Factory method is class design pattern. Abstract Factory uses many Factory methods. Why Abstract Factory is object design pattern, not class design pattern? Abstract Factory defer instantiation to which object?

推荐答案

抽象工厂模式将产品对象的创建推迟到ConcreteFactory子类。由于客户端希望将Factory类(它是ConcreteFactory子类的父类)用作方法参数;因此,基于传递的ConcreteFactory实例,创建相应的产品实例。因此,抽象工厂通过在运行时传递的ConcreteFactory实例上调用工厂方法来创建产品实例。因此,这是对象设计模式。通常,这些Concrete工厂是Singletons。

Abstract Factory pattern defers creation of product objects to the ConcreteFactory subclass. Since a client is expecting Factory class (which is the parent of ConcreteFactory subclasses) as a method parameter; based on the instance of ConcreteFactory passed, corresponding product instance is created. So Abstract Factory creates product instances by calling factory methods on a ConcreteFactory instance passed at runtime. Hence this is object design pattern. Typically these Concrete factories are Singletons.

Factory方法发生在类级别。必须创建一个新的ConcreteFactory类来实例化ConcreteProduct。此ConcreteFactory扩展了Factory类,该类具有返回Product的create()方法。

Factory method happens at the class level however. A new ConcreteFactory class has to be created to instantiate a ConcreteProduct. This ConcreteFactory extends Factory class which has a create() method returning Product. ConcreteFactory overrides create() method defined in the Factory parent class.

询问注释:

是Factory方法是参数化的,但是在AbstractFactory中,您将能够在传递的ConcreteFactory上调用方法,从而使您能够在运行时切换到其他的ConcreteFactory。但是在Factory中,由于create()是类的一部分,因此,如果要创建具体的产品,则必须创建ConcreteFactory来扩展其父Factory类。

Yes Factory method is parameterized but in AbstractFactory you will be able to call methods on a ConcreteFactory passed thus enabling you to switch to a different ConcreteFactory at runtime. But in Factory since create() is part of the class, if you want to create concrete products you have to create ConcreteFactory which extends the parent Factory class.

这篇关于抽象工厂与工厂方法(范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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