1
我有一个Engine
类,它具有核心逻辑,但有这样的课程将通过类似功能的第三方引擎来代替一个机会,所以我想的其余部分的影响最小应用。C++限制核心类的可视性和提供干净的界面
我有另一个类叫Adapter
,它允许与Engine
类的简单接口,并且还在Engine
类的顶部提供了一些增值功能。
然后我有叫OrderProcessor
,我希望暴露给具有更简单的界面应用程序的其余部分的类。
我想Engine
和Adapter
从应用程序的休息和OrderProcesseor
隐藏作为应用程序的其余部分的唯一接口。
如何去设计这一点,并使用其访问修饰符在哪里?有没有这样的设计模式?
这是我,但我不认为这是正确的。
//This is the core functionality. Might have to replace this class with a third //party implementation
//Want to hide it as much as possible
class Engine
{
private:
char* ip;
char* port;
protected:
Engine();
bool Connect();
bool DisConnect();
bool SendOrder(Message msg);
bool CancelOrder (CancelMessage cxl);
Message ReceiveOrder();
};
//This is an interface to the engine and provides value added functions
//Don't want this known to anyone except the OrderPRocessor
class Adapter : public Engine
{
private:
int TotalAmount;
double DollarAmount;
protected:
bool Start(char*ip, char* port); //this will call Engine's connect() and do other things
bool Stop(); //this will call Engine's Disconnect
int TotalInventory();
double TotalSales();
double TotalCommission();
};
//This class is the main interface to the rest of the application for order
//processing related functionality.
class OrderProcessor
{
public:
OrderProcessor();
~OrderProcessor();
Stats SendStats();
ManageInventory();
private:
Adapter adapter;
};
可能会有用[pImpl习语在实际中是否真的被使用?](http://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practice) – lcs
'ip'至少应该是'const char *'。端口,可能是int。 – Igor