mercredi 8 mars 2017

What C++ design pattern should I use for access control of child objects?

I have a class hierarchy where many subclasses derive from base. The base class is designed to be extensible by library users in the future, so the number and names of subclasses are unbounded. One of the services provided by base are input/output port objects that represent conduits by which data gets into and out of the object. Something like this pseudocode:

class input {};

class output {};

class base 
{
public:
    input &get_input() { return input_; }

    output &get_output() { return output_; }

private:
    input input_;
    output output_;
};

I would like the input and output classes to have two levels of access control:

  1. Public functions that are accessible by users of the base class. These would be used to, for instance, inject data into an input or pull data out of an output.

  2. Protected functions that are accessible only by base and its subclasses. These would be used to configure parameters of the input/output ports as appropriate for a specific subclass. I do not want these functions to be accessible to external users of base and its subclasses.

What is an appropriate design pattern to use for this case? My first idea was to make the base class a friend of input and output, but friendship is not inherited by subclasses. I don't think it would be relevant to this question, but you can assume that I can use any facilities up to C++14.

Aucun commentaire:

Enregistrer un commentaire