2016-04-13 1 views
1

いくつかのクラスにグループ化された大量の文字列があります。これらはすべて最終的な1つの巨大クラスにグループ化されています。このクラスは、別のクラスによって満たされていなければならず、不変のコンテンツが一部のクライアントに公開されている必要があります。 (もちろん、これらのクラスは、より複雑であり、これは簡略化された略図である。)異なるアクセス権を持つクラス:多くの人に読み取り専用で、特権者に書き込み可能

解決策1:

class B 
{ 
friend class M; 

private: 
    std::string m_camel; 
    std::string m_pink; 
    std::string m_vandergraaf; 

public: 
    const std::string& Camel() const { return m_camel;} 
    const std::string& PinkFloyd() const { return m_pink;} 
    const std::string& VanDerGraafGenerator() const { return m_vandergraaf;} 

private: 
    void SetCamel(const char* prog) { m_camel = prog;} 
    void SetPinkFloyd(const char* prog) { m_pink = prog;} 
    void SetVanDerGraafGenerator(const char* prog) { m_vandergraaf = prog;} 
} 

より良い解決策、:Bのようなものである

class A 
{ 
friend class M; 

private: 
    B m_b; 
    C m_c; 
    D m_d; 

public: 
    const B& GetB() const { return m_b;} 
    const C& GetC() const { return m_C;} 
    const D& GetD() const { return m_D;} 

private: 
    B& GetB() { return m_b;} 
    C& GetC() { return m_C;} 
    D& GetD() { return m_D;} 
} 

避けるprotectedfriendを回避するには、書き込みアクセスクラスをMに、そして基本クラスを読み取り専用にすることです。

解決策2:B用

class A 
{ 
protected: 
    B m_b; 
    C m_c; 
    D m_d; 

public: 
    const B& GetB() const { return m_b;} 
    const C& GetC() const { return m_C;} 
    const D& GetD() const { return m_D;} 
} 

// only visible to M 
class A_Write: public A 
{ 
public: 
    B& GetB() { return m_b;} 
    C& GetC() { return m_C;} 
    D& GetD() { return m_D;} 
} 

同じこと、多分。クライアントが独自のクラスを派生させることができるので、非常に良い解決策ではありません。

より良いが、より多くの制約変異体は、溶液3:

class A 
{ 
private: 
    const B m_b; 
    const C m_c; 
    const D m_d; 

public: 
    const B& GetB() const { return m_b;} 
    const C& GetC() const { return m_C;} 
    const D& GetD() const { return m_D;} 

protected: 
    A(const B& b, const C& c, const D& d): m_b(), m_c(c), m_d(d) {} 
} 

// only visible to M 
class A_Write: public A 
{ 
public: 
    A_Write(const B& b, const C& c, const D& d): A(b, c, d) {} 
} 

マイ好ましい溶液3が、B、C、Dで簡単struct sと代わりのclass ES ...であり、4です。だから、MはB、C、Dで直接必要なことをして、A_Writeを構築することができます。

もっと良いアイデアを教えてください。

+0

。あなたはそれを試みたことがありますか? – skypjack

+0

PIMPLを意味しますか?これはhttp://en.wikipedia.org/wiki/Proxy_patternですか?いくつかのバーチャルを意味しますか? 私の最初の考えは、これが私の場合には重すぎるということです。私は今、寝なければならない、さようなら! – Liviu

+0

答えにあなたの考えを明確に表現してください。いくつかのクラススキーマで? – Liviu

答えて

1

可能なアプローチは、クラスのインターフェイスを減らすプロキシを使用することです。
クラスMは、Sのインスタンスをインスタンス化/受信します(インターフェイスを使用して変更できるように)。ただし、プロキシPを読者に返します(変更を管理しません)。
それは、最小限の例を次に示します。

プロキシはインターフェイスの可視性を減らすために、ために使用することができるものだ
struct S { 
    void value(int v) noexcept { t = v; } 
    int value() const noexcept { return t; } 
private: 
    int t{0}; 
}; 

struct P { 
    P(S& s): b{s} { } 
    int value() const noexcept { return b.value(); } 
private: 
    S& b; 
}; 

int main() { 
    // the class: full interface 
    S s; 
    // the proxy: reduced interface 
    P p{s}; 
} 
+0

私はあなたのソリューションが気に入っていますが、Pはすべてのメンバーの "プロキシ"関数(値)を書くことを避けるためにS(および他のもの)にconst参照を返すことができると思います。 – Liviu

+0

2番目の解決策は私の会社が優先しましたが、クラスを分けるのであなたの方が良いです。 – Liviu

関連する問題