mercredi 30 juin 2021

Design pattern - filtered view of a data type that's composed inside another class

I'm working with a data_type but my project requires that the data_type conform to certain specifications so I have a class wrapper that includes a data_type within it.

class wrapper {
private:
    wrapper();
    
public:
    ~wrapper();

    static wrapper * create() {...}

    void member_one(...) {
        // Does stuff to m_ to enforce particular specification
    }

    ...

    void member_ten(...) {
        // Does stuff to m_ to enforce particular specification
    }

protected:
    // Do not expose, since we don't want users to add arbitrary stuff that does not conform to spec
    data_type m_;
};

Now, I am working with a third-party library that provides a view adapter to provide a filtered view of data_type. It takes a data_type and provides a filtered_data_type object.

Keeping in the spirit of my design above, I would like to make a filtered_wrapper that inherits from wrapper. It can have a filtered_data_type as its data member, which is constructed using the base class' data_type.

Problem solved, except that the functions member_one()...member_ten() can act equally on filtered_data_type as they do on data_type, and I want users of filtered_wrapper to be able to do this (a) without having to override all of the functions in filtered_wrapper to do the same thing but on a different data member and (b) while templates are a good solution to this, they expose the data_type and its own templated construction which I am trying to hide from the user, so I want to avoid templates unless there's another template pattern that addresses this.

Is there a design pattern to achieve what I am trying to do?

Aucun commentaire:

Enregistrer un commentaire