保存对象实例的C ++管理器

时间:2014-09-02 15:05:15

标签: c++ oop

在我目前的项目中,我打算使用我称之为Profile的多个实例。 Profile由一堆方法和信息组成,如此

class Profile
{
public:
    std::string name() const;
    int id() const;

    void setName(const std::string& name);
};

我们的想法是,随着项目的发展,Profile将包含更多信息。

现在,我本能地使用ProfileManager来跟踪,加载,创建和删除Profile个实例。但在这种情况下,Profile必须有一个实例或指向ProfileManager实例的指针,因为它需要检查是否有任何其他配置文件已经在{{1}中提供了名称方法,如:

setName(const std::string& name)

1。将经理留在托管对象中是不是一个坏主意?

我的想法是每个void Profile::setName(const std::string& name) { for(Profile* profile : manager->profiles()) { if(profile->name() == name) { //Ooops, a profile with that name already exists } } 都存在于它自己的目录中,例如Profile profiles/john/profile.xml可以是任意目录名。

然后john将扫描每个目录以搜索ProfileManager文件,并解析该文件以提取有关profile.xml的信息,并从中创建一个新文件。< / p>

这意味着Profile需要ProfileManager功能。但是,为了根据XML文件中的信息设置不同的变量,void load(const std::string& rootDir)ProfileManager需要成为朋友。

另一种解决方案是在Profile中设置static Profile* Profile::parse(const std::string& xmlFile)功能。但话说回来,Profile将完全需要对Profile的引用。所以也许这不是一个很好的解决方案。

2。我应该将功能分成不同的类吗? (ProfileFactory,ProfileManager等)?

我还需要能够在运行时创建ProfileManager

什么是干净的,不那么容易出错的结构/解决方案呢?

0 个答案:

没有答案
相关问题