OOP类设计在C ++ [英] OOP class design in C++
问题描述
我有关于C ++中类设计的简单问题。
I have simple question about class design in C++.
假设我们有以下类:
class DataBase
{
public:
DataBase();
void addEntry(const std::string& key, double value);
double getEntry(const std::string& key);
protected:
std::map<std::string, double> table;
};
还有另一个类,它保存了指向 DataBase
class:
There is another class which holds a pointer to an instance of DataBase
class:
class SomeClass
{
protected:
DataBase *someDataBase;
};
这里我很困惑,我想到两个选项:
Here I get confused, as two options come to my mind:
-
SomeClass
的每个实例都有一个自己的数据库。 -
SomeClass
的每个实例都会出现在这个数据库(专用数据库)将指中央数据库。SomeClass
的任何实例添加的数据将位于一个数据库(全局数据库)中。
- Each instance of
SomeClass
will have a database of its own. In the sense that only the data added by this instance will be present in this database (dedicated databases). - Each instance of
SomeClass
will be referring to a central database. Data added by any of the instances ofSomeClass
will be in one single database (a global database).
问题:
- OOP中上述概念的名称是什么? / li>
- 如何在C ++中实现上述每种方法
推荐答案
您正在寻找的是C ++中的所有权主题。当我说所有权时,我的意思是谁负责管理拥有对象的内存。
What you are looking for is the topic of ownership in C++. When I say ownership, I mean who is responsible for managing the memory that holds the object.
在你的第一个例子中每个 SomeClass
可以拥有自己的 DataBase
。
In your first example each SomeClass
could own its own DataBase
.
class SomeClass
{
private DataBase *db;
public SomeClass();
public SomeClass(DataBase* db);
public ~SomeClass();
}
SomeClass::SomeClass()
{
this.db = new DataBase();
}
SomeClass::SomeClass(DataBase* db)
{
this.db = db;
}
SomeClass::~SomeClass()
{
delete this.db;
}
此 SomeClass
拥有给予它的DataBase或创建自己的(实际上你通常做一个或另一个)。这意味着你可以传递一个 DataBase
对象(使用一个概念称为依赖注入):
This SomeClass
either takes ownership of the DataBase given to it or creates its own (practically you usually do one or the other). This means you can pass in a DataBase
object (using a concept known as dependency injection):
DataBase *db = new DataBase();
SomeClass sc(db);
sc.doSomeStuffWithDB();
或者让类创建 DataBase
对象:
SomeClass sc();
sc.doSomeStuffWithDB();
在上面的例子中,你不必担心处理 DataBase
对象,知道 SomeClass
应该处理它的析构函数。
In the above example you don't have to worry about disposal of the DataBase
objects, knowing that SomeClass
should take care of disposal in its destructor.
在另一种情况下,您可以共享 DataBase
,而不会被您的 SomeClass
(无论是全局还是非是不相关的)。
In the other scenario you could share a DataBase
without having it be disposed of by your SomeClass
(whether it's global or not is irrelevant).
class SomeClass
{
private DataBase *db;
public SomeClass(DataBase* db);
}
SomeClass::SomeClass(DataBase* db)
{
this.db = db;
}
这里我们可以传递多个 SomeClass
对象同样 DataBase
,而不必担心它们被任何对象处理。
Here we could pass multiple SomeClass
objects the same DataBase
and not have to worry about them being disposed of by any of the objects.
DataBase *db = new DataBase();
SomeClass *sc1 = new SomeClass(db);
SomeClass *sc2 = new SomeClass(db);
sc1.doSomeStuffWithDB();
delete sc1;
sc2.doSomeStuffWithDB();
delete sc2;
delete db;
在这种情况下,我们能够重用 DataBase
object放置在我们的 SomeClass
对象之外。实际上,这种处置可以由另一个类管理,例如 DataBaseStore
,允许你有一个可靠的方法来处理和重用 DataBase
对象。
In this scenario we were able to reuse the DataBase
object before disposing of it external to our SomeClass
objects. Practically speaking, this disposal could be managed by another class like DataBaseStore
, allowing you to have a reliable way to handle and reuse DataBase
objects.
这篇关于OOP类设计在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!