Adapter

Posted by Dustin Boston .


CAUTION

Construction Area
Watch out for:

  • Broken code
  • No comments
  • Partial examples
  • Missing tests

Source Code Listing

code.ts

type Target = {
  request(): void;
};

class ConcreteTarget implements Target {
  request(): void {
    console.log("Target request"); // More descriptive output
  }
}

class Adaptee {
  specificRequest(): void {
    console.log("Specific request");
  }
}

class Adapter implements Target {
  constructor(private readonly adaptee: Adaptee) {}

  request(): void {
    this.adaptee.specificRequest();
  }
}

const client = {
  run(): void {
    const adaptee = new Adaptee();
    const adapter = new Adapter(adaptee);
    adapter.request();

    const target = new ConcreteTarget(); // Demonstrate using the original Target
    target.request();
  },
};

client.run();