什么是OOP中的工厂

什么是OOP中的工厂

本文介绍了什么是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 其中表示:

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

So to answer your questions specifically:

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

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

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

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中的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:17