The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern.
The adapter pattern should be used when:
Device interface
interface IDevice { getChannel(); }
Device abstract class:
export abstract class Device implements IDevice { protected _remote: IRemote; constructor(remote) { this._remote = remote; } abstract getChannel(); }
Tv device class
export class Television extends Device { constructor(remote: IRemote) { super(remote); } override getChannel() { return this._remote.changeChannel(); } }
Radio device class:
export class Radio extends Device { constructor(remote: IRemote) { super(remote); } override getChannel() { return this._remote.changeChannel(); } }
Remote interface
interface IRemote { changeChannel(); }
RemoteTv class:
export class RemoteTv implements IRemote { changeChannel() { return 'Remote tv change channel'; } }
RemoteRadio class:
export class RemoteRadio implements IRemote { changeChannel() { return 'Remote radio change channel'; } }
The client code:
function client() { const tv = new Television(new RemoteTv()); const radio = new Radio(new RemoteRadio()); console.log(tv.getChannel()); console.log(radio.getChannel()); } client();
Result:
Remote tv change channel Remote radio change channel
Pros:
Cons:
Thank you for reading, and happy coding!
I hope this article will help make the concepts of the Bridge Pattern