Aft*_*ock 1 c++ design-patterns
我脑子里有这样的设计....我的目标是重复使用包含一些功能而没有某些功能的程序.文献中称之为什么?更多信息:有事件..事件导致调用function1()或function2()...
功能具有在事件发生时调用的功能.特征可能会影响事件中调用的函数.功能可能会影响对多个事件执行的操作.
所以它看起来可能是观察者模式+ hasa关系......
class feature1
{
void feature1functionx();
void feature1functiony();
}
class feature2
{
void feature2functionw();
void feature2functionz();
}
class program: feature1, feature2
{
vector<string> data;
void function1()
{
feature2functionw();
}
void function2()
{
feature1functiony();
feature2functionz();
}
void execute()
{
function1();
function2();
}
}
Run Code Online (Sandbox Code Playgroud)
继承模拟IS-A关系.
HAS-A如果你想重用函数,那么使用关系似乎很自然:那就是组合.
class program
{
public:
void function1()
{
m2.feature2functionw();
}
void function2()
{
m1.feature1functiony();
m2.feature2functionz();
}
void execute()
{
this->function1();
this->function2();
}
private:
feature1 m1;
feature2 m2;
};
Run Code Online (Sandbox Code Playgroud)
我知道私人继承有时候被认为是一种捷径,但它并没有带来任何东西,所以更喜欢构图,因为它不会让你受到太多束缚.
编辑:添加了方法的定义,因为它显然不是那么清楚.