什么是OOP中的工厂 [英] What is a Factory in OOP

查看:135
本文介绍了什么是OOP中的工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对与工厂相关的"设计模式及其OOP实现的理解一直很简单.

My understanding of "factory-related" design patterns and their OOP-implementations has always been pretty simple.

  • 工厂方法" 是类中的一种方法,该类具有一个接口(或抽象类)作为返回类型,并根据一些内部逻辑构造实现此接口的对象.
  • 工厂" 是仅包含工厂方法的类
  • 抽象工厂" 是仅包含工厂方法的接口(或抽象类)
  • A "Factory method" is a method inside a class that has an interface (or an abstract class) as a return type and constructs objects implementing this interface based on some internal logic.
  • A "Factory" is a class that only contains factory methods
  • An "Abstract factory" is an interface (or an abstract class) that only contains factory methods

但是我最近偶然发现了有关该主题的Wikipeda文章(工厂抽象工厂)使我有些困惑,尤其是关于工厂" 处于面向对象状态.

But I recently stumbled upon Wikipeda articles on the subject (Factory, Abstract factory) that made me somewhat confused, especially about what a "Factory" is in OOP.

以下是一些引号:

  1. 在工厂方法或工厂函数中,返回新"对象的子例程可以称为工厂".
  2. 工厂用于各种设计模式
  3. 抽象工厂模式"是一种建立工厂集合的方法.
  4. 工厂是构造对象的代码中具体类的位置
  1. A subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function.
  2. Factories are used in various design patterns
  3. The "Abstract factory pattern" is a method to build collections of factories.
  4. A factory is the location of a concrete class in the code at which objects are constructed

这引起了一些问题:

(1)&(2)这是否意味着工厂不是类或对象,而是一条逻辑?

(1)&(2) Does this mean that a factory is not a class or an object, but a piece of logic?

(2)工厂"本身不是一种模式吗?

(2) Is "Factory" not a pattern itself?

(3)集合"在这里是什么意思?仅仅是说您可以有多个实现相同接口的工厂(这是一个抽象工厂)"吗?

(3) What does "collection" mean here? Is it just a way of saying "you can have several factories that implement the same interface (which is an abstract factory)"?

(4)什么?

任何人都可以澄清这是什么意思吗?我对工厂的最初理解不正确吗?

Can anyone clarify what this means? Is my initial understanding of factories incorrect?

推荐答案

查看此 Wiki 其中表示:

在面向对象编程(OOP)中,工厂是用于 创建其他对象–正式而言,工厂是一种功能或方法 从某些对象返回不同原型或类的对象 方法调用,它假定为新的".[a]更广泛地讲,a 返回新"对象的子例程可以称为 工厂",如工厂方法或工厂功能中的.这是基本的 OOP中的概念,并构成许多相关软件的基础 设计模式.

In object-oriented programming (OOP), a factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new".[a] More broadly, a subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function. This is a basic concept in OOP, and forms the basis for a number of related software design patterns.

因此,要专门回答您的问题:

So to answer your questions specifically:

(1)&(2)这是否意味着工厂不是类或对象,而是一条逻辑?

(1)&(2) Does this mean that a factory is not a class or an object, but a piece of logic?

否,这意味着您可以使用对象(工厂)创建其他对象.

No, it means that you can create other objects using an object(factory).

(2)工厂"本身不是一种模式吗?

(2) Is "Factory" not a pattern itself?

有多种设计模式,其中工厂模式是其中之一.因此,当您使用工厂创建对象时,创建其他对象的方式就是工厂模式"

There are different design patterns out of which factory pattern is one. So when you are creating objects using a factory then that patter of creating other objects is "Factory pattern"

这篇关于什么是OOP中的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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