在 C++ 中创建环境对象的最佳方法
我想创建一个可以从程序中的所有类访问的环境类,但我不想每次想从其他类访问其成员时都初始化环境对象。在 C++ 中执行此操作的最佳方法是什么?
我想这样做是因为我有环境对象存储其他类可能使用的所有配置值。这些值是从多个位置读取的,包括不同的文件。我不想每次在类中创建新的环境对象时都解析文件。
I want to create an environment class that is accessible from all of my classes in my program but I dont want to initialize the environment object everytime I want to access its members from my other classes. What is the best way to go around doing this in C++?
I want to do this because I have the environment object store all my config values that other classes may use. Those values are read from multiple places, including different files. I dont want to parse the files every time I create a new environment object in my classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Singleton 对象并不总是解决方案。虽然有时这似乎是一个简单的解决方案,但它确实有一些缺点(请参阅这个问题 例如)。
有多少类实际上需要访问此环境对象?如果你的字面意思是你所做的每一堂课,那么听起来你的设计是有缺陷的。
通常,单例的更好替代方案是将对象传递给真正需要它的人。
A Singleton object isn't always the solution. While sometimes it seems like an easy solution, it does have some disadvantages (see this question for example).
How many of your classes actually need access to this Environment object? If you literally meant that every class you have do then it sounds like your design is flawed.
Quite often a better alternative to a singleton is just to pass the object around to those who actually need it.
您需要做的是将环境类包装在单例模式中。请参阅此问题以获取更多信息:C++ 单例设计模式
What you need to do is to wrap your environment class in a Singleton Pattern. See this SO question for more info: C++ Singleton design Pattern
正如已经指出的,您正在寻找的是单例模式。然而,单例模式常常是设计不当的结果。每当您发现自己使用单例模式,或者任何需要实际上是全局变量的模式时,您都应该考虑是否有更好的方法来解决该问题。关于您的特定问题,我建议您查看 QSettings 类,它是 Qt Framework 的一部分,这是一个免费的高质量开源库。
QSetttings 类将允许您使用首选的本机机制(Windows 上的注册表、Mac OS X 上的属性列表文件以及 Linux 上的 gconf XML 文件)加载/保存配置设置。另外,您可能想查看我的帖子 环境变量是邪恶,如果您正在考虑使用环境变量进行配置(配置的“环境”名称听起来非常不祥)。
As has been pointed out, what you are looking for is the Singleton pattern. However, the Singleton pattern is frequently the result of poor design. Whenever you find yourself using the Singleton pattern, or, for that matter, any pattern that requires what are, in effect, global variables, you should consider whether there might be a better approach to the problem. With respect to your particular problem, I recommend you take a look at the QSettings class, which is a part of the Qt Framework, a free and high quality open source library.
The QSetttings class will allow you to load/save configuration settings using the preferred native mechanism (the registry on Windows, a property list file on Mac OS X, and a gconf XML file on Linux). Also, you might want to see my post Environment Variables are Evil, in case you were considering using environment variables for the configuration (the name "environment" for the configuration sounds awfully ominous).
听起来你想要一个单例模式。这将允许您创建和使用类的一个对象/实例,但不能再创建和使用更多对象/实例,即使您多次访问它。请参阅:
http://www.infernodevelopment.com/singleton-c
Sounds like you want a singleton pattern. This will let you create and use one object/instance of a class, but no more, even if you access it many times. See:
http://www.infernodevelopment.com/singleton-c
您可以创建一个静态单例服务。该服务包含您的所有对象集合并提供访问这些对象的函数。
You can create a service which is a static singleton. This service contains all your object collection(s) and provide functions to access these objects.