2017-02-06 2 views
1

コアロジックを持つEngineクラスがありますが、このクラスは同様の機能を持つサードパーティエンジンに置き換えられる可能性があります。応用。C++コアクラスの可視性を制限してクリーンなインターフェイスを提供する

IはEngineクラスに単純なインタフェースを可能にし、またEngineクラスの上にいくつかの付加価値機能を提供Adapterと呼ばれる別のクラスを持っています。

私はOrderProcessorという別のクラスを持っています。これは、はるかに単純なインターフェイスを持つアプリケーションの残りの部分に公開したいと思っています。

EngineAdapterは残りのアプリケーションから隠され、OrderProcesseorは残りのアプリケーションのための唯一のインターフェイスです。

これを設計するにはどうすればよいですか、どのアクセス修飾子をどこで使用するのですか?それを行うデザインパターンはありますか?

これは私が持っているものですが、私はそれが正しいとは思わないです。あなたはよりきめ細かいアクセス制限をしたい場合は 'キータグ' を作る、

class OrderProcessor 
{ 
public: 
    OrderProcessor(); 
    ~OrderProcessor(); 

    Stats SendStats(); 
    ManageInventory(); 

private: 
    class Engine 
    { 
     // ... 
    }; 

    class Adapter : public Engine 
    { 
     // ... 
    }; 

    Adapter adapter; 
}; 

//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; 
}; 
+0

実際にはpImplイディオムが実際に使用されていますか?(http://stackoverflow.com/questions/8972588/is-the-pimpl-idiom-really-used-in-practice) – lcs

+0

'ip'少なくとも 'const char *'でなければなりません。ポート、おそらく、int。 – Igor

答えて

1

OrderProcessorEngineAdapterプライベートネストされたクラスを作成します

class OrderProcessor 
{ 
public: 
    class OpenSesame 
    { 
     friend ClassThatCanAccessTreasure; 
    private: 
     OpenSezame() {}; // = default is not sufficient 
    }; 

    int AccessTreasure(int arg1, int arg2, OpenSesame key = {}); 
}; 

注意しています上記のスキームでは、デフォルトのため、keyを渡す必要はありません。デフォルト値は呼び出し元のコンテキストでインスタンス化されるため、OpenSesameの友人のみがAccessTreasureと呼ぶことができます。このdefaultingスキームは、varargs/parameter packを持たない限り動作します。そうした場合は、それらの前に渡して手動で渡す必要があります。

関連する問題