deb*_*deb 5 c++ oop design-patterns
我用C++编写代码,由于接口关键字不在语言中,我将在这里使用接口作为概念(合同).
假设你有一个接口说IBase,它已被几十个类实现.现在,您需要在该接口IBase中添加另一个方法.
以最小的方式进行更改以解决在所有实现类中重写该方法的问题的方法是什么?
一种方法是在Ibase类中添加带有默认实现的新方法,并让需要它的派生类覆盖它.
在上面的解决方案中,我觉得我在两个地方出错了,通过触摸界面打破了开放的封闭原则,并通过告诉用户你也可以调用另一种方法来打破合同.
在这里让我感到震惊的另一件事是,我在基础上添加了一个方法,只有一个派生类覆盖它,这基本上意味着其他类不需要该方法.
这样我就可以使用另一个解决方案,其中需要此方法的类将继承自另一个提供此功能的类或使用组合来使用另一个类的功能.
通过上面的解决方案我遇到了另一个问题,客户端如何通过Ibase接口访问派生类的新功能.只有函数存在于基类中时,C++中的动态调度才有效.
有人可以帮忙!
UPDATE
我只是根据下面的评论编写了我的理解,但现在客户端代码看起来很乱,它必须知道哪个接口用于额外的功能.我们应该使用工厂/抽象工厂抽象下面的代码吗?
#include <iostream>
using namespace std;
class IBase {
public:
virtual void test() = 0;
};
class INewInterface {
public:
virtual void newMethod() = 0;
};
class Derived : public IBase {
public:
virtual void test () {
cout<< " Derived " << endl;
}
};
class DerivedSecond: public IBase, public INewInterface {
public:
virtual void test() {
cout<< " Derived Second " << endl;
}
void newMethod( ) {
cout<< " newMethod " << endl;
}
};
int main( int argc, char ** argv ) {
// Client code
//Client needs to cast to two different interfaces to get access to the new functionality.
// Probably should use a factory here
INewInterface * pNewInterfacePtr = dynamic_cast< INewInterface * > ( new DerivedSecond );
IBase * pIbasePtr = dynamic_cast< IBase * > ( new DerivedSecond );
pIbasePtr->test();
pNewInterfacePtr->newMethod();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
很难真正知道什么适合您的情况,因为您显然拥有一个工作系统,该系统可能会或可能不会从我们可能建议的任何内容中受益。
不过,我已经制作了一个示例,说明如何管理具有单独和/或重叠职责的多个子类型。这种方法是让一个主容器使用 a 持有所有对象的所有权std::unique_ptr,以确保它们全部被删除并且我们没有内存泄漏。
除了主容器之外,我们还有用于不同视图的单独容器。每个视图都保存对那些具有特定职责且不与其他类型共享的元素的引用(原始指针)。
也许它对您的情况有一定用处,也许没有:
#include <vector>
#include <memory>
#include <iostream>
class Role
{
public:
virtual ~Role() {}
};
class RoleOne
: public virtual Role
{
public:
virtual void do_role_one_stuff() = 0;
};
class RoleTwo
: public virtual Role
{
public:
virtual void do_role_two_stuff() = 0;
};
class ItemA
: public RoleOne
{
public:
void do_role_one_stuff() override { std::cout << "Item A in Role One\n"; }
};
class ItemB
: public RoleOne
, public RoleTwo
{
public:
void do_role_one_stuff() override { std::cout << "Item B in Role One\n"; }
void do_role_two_stuff() override { std::cout << "Item B in Role Two\n"; }
};
class ItemC
: public RoleTwo
{
public:
void do_role_two_stuff() override { std::cout << "Item C in Role Two\n"; }
};
class Resources
{
// unique_ptr ensures deletion (no memory leaks)
std::vector<std::unique_ptr<Role>> all; // owning container
// raw 'access' pointers share access (no need to share ownership)
std::vector<RoleOne*> ones; // alternate 'view' (no ownership)
std::vector<RoleTwo*> twos; // alternate 'view' (no ownership)
public:
void add_item(Role* item)
{
// manage ALL items life-spans here
all.emplace_back(item);
// add one-centric items to the one-centric view
if(auto one = dynamic_cast<RoleOne*>(item))
ones.emplace_back(one);
// add two-centric items to the two-centric view
if(auto two = dynamic_cast<RoleTwo*>(item))
twos.emplace_back(two);
}
void do_business()
{
// ItemA and ItemB types do this kind of business
std::cout << "\nDoing role one business:\n";
for(auto role: ones)
role->do_role_one_stuff();
// ItemB and ItemC types do this kind of business
std::cout << "\nDoing role two business:\n";
for(auto role: twos)
role->do_role_two_stuff();
}
};
int main()
{
Resources res;
res.add_item(new ItemA);
res.add_item(new ItemB);
res.add_item(new ItemC);
res.add_item(new ItemB);
res.add_item(new ItemA);
res.add_item(new ItemC);
res.do_business();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Doing role one business:
Item A in Role One
Item B in Role One
Item B in Role One
Item A in Role One
Doing role two business:
Item B in Role Two
Item C in Role Two
Item B in Role Two
Item C in Role Two
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
275 次 |
| 最近记录: |