Patterns to expose limited interfaces to friend classes
===

I was looking for patterns that can be used to allow a class to expose a subset of its interface for use only by one (or more) explicitly specified class(es). i.e.,
<pre><code>
class A
{
    void Foo ();  // Only class B should be able to call this
    void Bar ();  // Anyone can call this
};
</code></pre>

I came across the [Attorney-Client][1] pattern and the [key-oriented access protection][2] pattern, but before I came across them, I had concocted my own mechanism which seems a lot simpler:

<pre><code>
class AControlInterface
{
private:
    void ControlMe () { ... }
    friend class Controller;
};

class A : public AControlInterface
{
public:
    void Foo () {...}
};

</code></pre>

This way I can separate out interfaces that are meant to be used only by certain classes. Is there anything wrong with this approach, compared to the other approaches?


  [1]: http://www.drdobbs.com/friendship-and-the-attorney-client-idiom/184402053
  [2]: http://stackoverflow.com/questions/3220009/is-this-key-oriented-access-protection-pattern-a-known-idiom