jeudi 18 juillet 2019

Adapter design patern and data transfer

I'm not sure about the design of the classes using the Adapter design pattern.

I'm trying to use the Adapter pattern and the problem is I'm not sure about the design I chose. I'm using an external library (the Adaptee) to extract some Foo Data. I'm using dependency injection to pass a Target pointer to the Foo class in order to extract the data and initialize the Foo object. I'm wondering if the class Foo shouldn't be directly the ExtractFooAdapter class, and then the ExtractFooAdapter would be use in other (client) classes like the original Foo class. So basically, the idea would be that the ExtractFooAdapter class would contain and hold the data. I'm asking you this question because if feels weird to transfer data from a class to another.

However, if you consider the ExtractFooAdapter class as the new Foo class, how would you handle the fact that new functionalities should be implemented in the class in the future? I guess you will tell me that this is not an adapter anymore because the adapter is just supposed to adapt / map interfaces, but where would you put the new functionalities using the same data?

class Target
{
public:
    virtual Data ExtractData() const = 0;
};

class Adaptee
{
    // ... External library I want to interface with my own code to extract stuff...
};

class ExtractFooAdapter : public Target, private Adaptee
{
private:
    // ... Private methods and data members...

public:
    Data ExtractData() const
    {
        Data foo_data;

        // ... Some work using the adaptee private methods and data members...

        return foo_data;
    }
};

// Client class
class Foo
{
private:
    Data data;

public:
    Foos(Target* adapter) : data(adapter->ExtractData()) {}
};

Aucun commentaire:

Enregistrer un commentaire