ts Adapter pattern

The solution for “ts Adapter pattern” can be found here. The following code will assist you in solving the problem.

/*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.

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in