Adapter Pattern

Definition

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Explanation

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

The adapter pattern is useful in situations where an already existing class provides some or all of the services you need but does not use the interface you need. A good real life example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.

Screencast

TypeScript Code

module ThirdpartyLib {
    export class StringNewsServer {
        public userName: string;
        public passWord: string;

        public getString(): string {
            /* validate userName and passWord then */
            return "StringNewsServer.newsItem1;StringNewsServer.newsItem2;StringNewsServer.newsItem3";
        }
    }

    export class ArrayNewsServer {
        public url: string;
        public getArray(): Array {
            /* use url to fetch data then */
            return ["ArrayNewsServer.newsItem1", "ArrayNewsServer.newsItem2", "ArrayNewsServer.newsItem3"];
        }
    }
}

module News {
    export interface INewsServerInterface {
        getNews(): Array;
    }

    export class NewsLoader {
        public Load(server: INewsServerInterface) {
            var news = server.getNews();
            news.forEach((value: string) => {
                Output.WriteLine(value);
            });
        }
    }

    export class StringNewsServerAdapter implements INewsServerInterface {
        private newsServer: ThirdpartyLib.StringNewsServer;
        constructor() {
            this.newsServer = new ThirdpartyLib.StringNewsServer();
            this.newsServer.userName = "userName";
            this.newsServer.passWord = "passWord";
        }

        public getNews() {
            var items = this.newsServer.getString();
            return items.split(";");
        }
    }

    export class ArrayNewsServerAdapter implements INewsServerInterface {
        private newsServer: ThirdpartyLib.ArrayNewsServer;
        constructor() {
            this.newsServer = new ThirdpartyLib.ArrayNewsServer();
            this.newsServer.url = "http://mynews.com";
        }

        public getNews() {
            return this.newsServer.getArray();
        }
    }

    window.addEventListener("load", function () {
        var newsLoader = new NewsLoader();

        var arrayNewsServer = new ArrayNewsServerAdapter();
        newsLoader.Load(arrayNewsServer);

        var stringNewsServer = new StringNewsServerAdapter();
        newsLoader.Load(stringNewsServer);
    });
}

Output