Originally published on Hackdoor

Structural design patterns ease the design of large structure of relationships between objects while keeping these structures flexible and efficient. Let's find them out, using some references from Game Of Thrones.

In software engineering, a Design Pattern describes an established solution to the most commonly encountered problems in software design. It represents the best practices evolved over a long period through trial and error by experienced software developers.

In this article we will talk about the Structural Design Patterns. Structural Patterns are design patterns that ease the design by identifying a simple way to realize relationships among entities.

The most common Structural Patterns are:

Adapter Pattern

The Adapter Pattern is a structural pattern where the interface of one class is translated into another. This pattern lets classes work together that could not otherwise because of incompatible interfaces.

Let's pretend that the Targaryen house wants to go to war with all its strength, both the Unsullied and the Dragons, but Daenerys wants to find a unique way to ask both of them to kill someone.

According to the "Adapter" pattern, we will need:

class Unsullied {
    constructor(name) {
        this.name = name;
        this.kill = this.kill.bind(this);
    }

    kill() {
        console.log(`Unsellied ${this.name} kill`)
    }
}

class Dragon {
    constructor(name) {
        this.name = name;
        this.burn = this.burn.bind(this);
    }

    burn() {
        console.log(`Dragon ${this.name} burn`)
    }
}

class DragonAdapter extends Dragon {
    kill() {
        this.burn()
    }
}

(() => {
    const Army = [
        new DragonAdapter('Rhaegal'),
        new Unsullied('Grey worm'),
        new DragonAdapter('Drogon')
    ];
    Army.forEach(soldier => soldier.kill());
})();

When to use the Adapter Pattern?