在我目前的项目中,我打算使用我称之为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
。
什么是干净的,不那么容易出错的结构/解决方案呢?