Structural Design Pattern

The Adapter design pattern is a structural design pattern that allows objects with incompatible interfaces to collaborate

struct legacyType {
    int a;
    int b;
};

class LegacyCode {
    public:
        void processData(struct legacyType * lt) {
            cout <<" a : " << lt->a <<" b: " << lt->b << endl;
        }
};

class ModernType {
    private:
        int first;
        int second;
    public :
        ModernType (int a, int b) : first(a) , second(b) {

        }

        int getFirst() {return first;}
        int getSecond() {return second;}
};


class LegacyToModernAdapter {
    LegacyCode lc;
    public:
        void processData(ModernType &mt) {
            // convert mt's data into legacyType..
            legacyType lt = {.a = mt.getFirst(), .b = mt.getSecond()};
            lc.processData(&lt);
        }
};

int main() {

    ModernType mt(2,10);
    LegacyToModernAdapter adapter;
    adapter.processData(mt);
}

Follow more posts @ https://jdecodes.wordpress.com

My all design pattern articles :

It’s Jdecoder

I am trying to decode the concepts into simple words and documenting items i know or currently learning.

Let’s connect

Design a site like this with WordPress.com
Get started