Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ts Adapter pattern

/*Adapter
The Adapter design pattern is a design pattern lets you convert 
the interface of a class into another interface
that it expects.

imagine you want to turn a socket into a plug.
*/
class Socket {
  constructor(private type: string) {}

  public getType() {
    return this.type;
  }
}

// Then you make a plug class:
class Plug {
  constructor(private type: string) {}

  public getType() {
    return this.type;
  }
}

// Then you make an adapter class that will adapt 
// the socket class to the plug class:
class SocketAdapter implements Plug {
  constructor(private socket: Socket) {}

  public getType() {
    return this.socket.getType();
  }
}

//Then you make your plug:
const plug = new SocketAdapter(new Socket('Type-C'));
console.log(plug.getType());
// As you can see the adapter class uses inheritance to adapt 
// the socket class to the plug class.
Comment

PREVIOUS NEXT
Code Example
Typescript :: Implement a function that counts the number of nodes in a circularly linked list 
Typescript :: how to deduct user points when he buy something laravel 
Typescript :: marine traffic embeeded map in basic html 
Typescript :: typescript map interface 
Typescript :: return type depends on input typescript 
Typescript :: reader.readasarraybuffer(file) is undefined 
Typescript :: react-i18next bold text 
Typescript :: flights starting from in india 
Typescript :: c code for insrting elements in set 
Typescript :: check if a user already exists firebase realtime database react native 
Typescript :: github actions typescript 
Typescript :: tkinter widgets overview 
Typescript :: Count pets the types of pets in a columns 
Typescript :: calculate north south east west using magnetic sensor 
Typescript :: splice array based on index typescript 
Typescript :: typescript import variable from another file 
Typescript :: Passing Data between fragments in Android using Interface 
Typescript :: styled components tw 
Typescript :: react react-dom react-scripts cra-template has failed. 
Typescript :: find number of digits that changed after addition of 1 
Cpp :: dart and or 
Cpp :: print set c++ 
Cpp :: min priority queue c++ 
Cpp :: c++ file is empty 
Cpp :: clear file before writing c++ 
Cpp :: check if key exists in map c++ 
Cpp :: c++ get cursor position console 
Cpp :: is there an algorithm to create a truly random password 
Cpp :: file handling 
Cpp :: cout hex value 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =