什么是类工厂? [英] What exactly is a Class Factory?
问题描述
我经常看到这个词,我可能已经使用它自己在代码和库中的时间,但我从来没有真正得到它。在大多数写作中,我遇到了,他们只是希望你能想出来。
I see the word thrown around often, and I may have used it myself in code and libraries over time, but I never really got it. In most write-ups I came across, they just went on expecting you to figure it out.
什么是类工厂?有人可以解释这个概念吗?
What is a Class Factory? Can someone explain the concept?
推荐答案
类工厂构造其他类的实例。通常,它们创建的类共享一个公共的基类或接口,但返回派生类。
A class factory constructs instances of other classes. Typically, the classes they create share a common base class or interface, but derived classes are returned.
例如,你可以有一个类工厂,并返回一个实现 IDbConnection
的类,例如 SqlConnection
(类和接口.Net)
For example, you could have a class factory that took a database connection string and returned a class implementing IDbConnection
such as SqlConnection
(class and interface from .Net)
这篇关于什么是类工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!