Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [Structural] Adapter

    Published May 02, 2022 [  DesignPattern  ]

    Real world example

    Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter. Another example would be the famous power adapter; a three legged plug can’t be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet. Yet another example would be a translator translating words spoken by one person to another

    In plain words

    Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.

    Wikipedia says

    In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.

    Programmatic Example

    Consider a game where there is a hunter and he hunts lions.

    First we have an interface Lion that all types of lions have to implement

    interface Lion {
        roar();
    }
    
    class AfricanLion implements Lion {
        roar(){
    
        }
    }
    
    class AsianLion implements Lion {
        roar() {
        }
    }
    

    And hunter expects any implementation of Lion interface to hunt.

    class Hunter {
        hunt(lion: Lion){
            lion.roar();
        }
    }
    

    Now let’s say we have to add a WildDog in our game so that hunter can hunt that also. But we can’t do that directly because dog has a different interface. To make it compatible for our hunter, we will have to create an adapter that is compatible

    class WildDog {
        bark(){
    
        }
    }
    
    class WildDogAdapter implements Lion{
        protected dog: WildDog;
    
        constructor(dog: WildDog) {
            this.dog = dog;
        }
    
        roar(){
            this.dog.bark()
        }
    }
    

    And now the WildDog can be used in our game using WildDogAdapter.

    const wildDog = new WildDog()
    const wildDogAdapter = new WildDogAdapter(wildDog)
    
    const hunter = new Hunter();
    hunter.hunt(wildDogAdapter)
    

    References