问题描述
这是一个工厂,它存储从字符串到对象创建函数的映射,所以我可以通过字符串标识符从工厂请求不同类型的对象。该工厂生产的所有类都将继承一个抽象类(Connection),为不同协议(HTTPConnection,FTPConnection等等)之间的连接提供一个通用接口。
I已经很好地掌握了如何与上面的工作方法联系起来,并使其工作。
我遇到问题是试图找出一个机制,以防止实例化Connection对象而不使用Factory。为了工厂做它的工作,我需要提供一个对象创建功能来存储在它的地图。我不能提供它的构造函数,因为你不能使函数指针的构造函数。所以,如上面的链接,必须有一个单独的对象创建函数来返回新对象。但是要做到这一点,我需要使这个创建函数是类的一个静态方法,客户端代码将能够访问,或一个单独的函数,需要a)Connection类的构造函数是public,或b)使构造函数私有并使非类成员创建函数成为朋友,这是不继承的,不能由抽象基类强制执行。
同样,如果我只是使用Connection类创建了Factory类,那么它可以访问它们的私有构造函数,但是我不能 t强制通过abstact基类,因为朋友不是继承。每个子类都必须明确地与工厂朋友。
任何人都可以建议一个实现我上面描述的方法?
重申要求:
1 - 生成各种对象的工厂,这些对象都是从同一基类派生的,基于标识符传递给工厂创建方法。
2 - 工厂将需要生成的所有子类将自动注册一个创建函数和标识符与工厂(见上面的链接SO答案) p>
3 - 工厂生成的所有子类不应通过Factory实例化(实例化)。
4 - 使用继承将#3显式地作为抽象基类的一部分。移除了某人从抽象基类继承的可能性,同时也提供了自由实例化对象的机制。
我想要实现的总体目标是允许新的连接类型添加到层次结构,而不必以任何方式更改Factory类,同时还强制所有Connection的子类不能由客户端代码直接实例化。
我有可能这不是实现我想要的最佳方式,欢迎其他选择的建议。
$ b $ EDIT - 当我回家时会添加一些代码片段, p>如果我正确理解你,我想你可以把一些你想要的在我提供的 METADECL
宏我链接到的答案,即定义一个静态创建函数是朋友或声明为静态方法。
下面我尝试指出 METADECL
(和 METAIMPL
)应该是。
头文件
class MySubClass:public FactoryObjectsRoot {
METADECL(MySubClass)//声明必要的工厂构造
:
:
}
源文件
METAIMPL(MySubClass)//实现和引导工厂构造
I'm working on implementing a Factory class along the lines of what is proposed in this response to a previous question:
http://stackoverflow.com/questions/410823/factory-method-implementation-c/534396#534396
It's a Factory that stores a map from strings to object creation functions so I can request different types of objects from the factory by a string identifier. All the classes this factory produces will inherit from an abstract class (Connection) providing a common interface for connections over different protocols (HTTPConnection, FTPConnection, etc...)
I have a good grasp of how the method linked to above works and have got that working.
Where I'm having problems is trying to figure out a mechanism to prevent instantiation of the Connection objects without using the Factory. In order for the Factory to do it's work, I need to provide it an object creation function to store in it's map. I can't provide it the constructor because you can't make function pointers to constructors. So, as in the link above, there has to be a seperate object creation function to return new objects. But to do this, I need to make this creation function either a static method of the class, which the client code would be able to access, or a seperate function which would require either a)that the constructor of the Connection classes be public, or b) make the constructor private and make a non class member creation function be a friend, which isn't inherited and can't be enforced by the abstract base class.
Similarly, if I just made the Factory class friends with the Connection classes it was supposed to produce so it could access their private constructors, that would work, but I couldn't enforce through the abstact base class because friends aren't inherited. Each subclass would have to explicitly be friends with the Factory.
Can anyone suggest a method of implementing what I've described above?
To reiterate the requirements:
1 - Factory that produces a variety of objects all derived from the same base class based on passed in identifier to the Factory's Create method.
2 - All the subclasses that the factory will need to produce will automatically register a creation function and identifier with the factory (see linked SO answer above)
3 - All the subclasses that the factory will produce should not be instantiable (instantiatable?) without going through the Factory
4 - Enforce #3 explicitly as part of the abstract base class using inheritance. Remove the possibility for someone to subclass from the abstract base class while also providing mechanisms to freely instantiate objects.
The overall goal of what I'm trying to achieve is to allow new Connection types to be added to the hierarchy without having to change the Factory class in any way, while also forcing all the subclasses of Connection to not be instantiable directly by client code.
I'm open to the possibility that this is not the best way to achieve what I want, and suggestions of other alternatives are welcome.
EDIT - Will add some code snippets to this when I get home to hopefully make this clearer.
If I understand you correctly I think you can put some of what you want in the METADECL
macro I mention in my answer you link to, ie define a static creator function that is a friend or declare it as a static method. This will make it possible for you to restrict the constructor from public use etc.
Below I try to point out where the METADECL
(and METAIMPL
) should be. I leave it for you to implement what you need there (I believe in you)
Header file
class MySubClass : public FactoryObjectsRoot {
METADECL(MySubClass) // Declare necessary factory construct
:
:
};
Source file
METAIMPL(MySubClass) // Implement and bootstrap factory construct
这篇关于参数化工厂&无法在没有Factory的情况下实例化的产品类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!