2017-02-06 6 views
1

코어 로직을 사용하는 Engine 클래스가 있지만이 클래스는 비슷한 기능을 가진 타사 엔진으로 대체 될 가능성이 있으므로 나머지 클래스에 최소한의 영향을주고 싶습니다. 신청.C++ 핵심 클래스 가시성 제한 및 깨끗한 인터페이스 제공

나는 Engine 클래스와의 간단한 인터페이스를 허용하는 Adapter이라는 또 다른 클래스가 있으며 Engine 클래스 위에 몇 가지 부가 기능을 제공합니다.

그런 다음 훨씬 간단한 인터페이스를 가진 나머지 응용 프로그램에 노출하고자하는 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 scheme은 varargs/parameter pack이없는 한 작동합니다; 그렇게한다면, 그것들을 전달하기 전에 수동으로 전달해야합니다.