CategoryTagArticle

admin

I'm a Full-stack developer

Tag

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Bridge
Published date: 08/08/2023

What is an adapter pattern?

The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern. 


When should I use the adapter pattern?

The adapter pattern should be used when:

  • separate the abstraction from the implementation so that it can be easily extended independently of each other.
  • Both Abstraction and Implementation should be extended with subclasses.
  • Use where changes made in the implementation do not affect the client side.


How to implement

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 and Cons

Pros:

  • Easily to maintain.
  • Easy to expand.
  • Hidding implements from clientside.
  • Reduce the number of classes that are not necessary.
  • Reduce dependency between abstraction vs implementation


Cons:

  • You might make the code more complicated by applying the pattern to a highly cohesive class.



Wrapping Up

Thank you for reading, and happy coding!

I hope this article will help make the concepts of the Bridge Pattern

Recommend

Data structure: Doubly Linked List
admin07/04/2024

Data structure: Doubly Linked List
In this article, I would like to show you about Data structure - Doubly Linked List
What are the SOLID principles?
admin17/06/2023

What are the SOLID principles?
If we want to have good software, the infrastructure should be good first. We should learn more techniques that help to have better quality.
🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
admin13/04/2024

🚀 Using Bitwise Oprators to build a RBAC in Node.js 🚀
In this article, I will illustrate to you how to build an RBAC in Node.js using Bitwise Operators.
Newest

ĐINH THÀNH CÔNG - Software Developer
admin12/01/2024

ĐINH THÀNH CÔNG - Software Developer
Cong Dinh - Software Developer - My personal website, where I write blogs on a variety of topics and where I have some experiments with new technologies.
Create Project with Express + TypeScript + ESLint + Auto Reload
admin12/06/2023

Create Project with Express + TypeScript + ESLint + Auto Reload
In this article, I introduce to you how to initialize an Express + TypeScript project.
Writing a Data Transformation Pipeline Using Go
admin20/03/2024

Writing a Data Transformation Pipeline Using Go
In this article, I will show how to implement data processing using the Go programing language with a simple tutorial.
Đinh Thành Công Blog

My website, where I write blogs on a variety of topics and where I have some experiments with new technologies.

hotlinelinkedinskypezalofacebook
DMCA.com Protection Status
Feedback
Name
Phone number
Email
Content
Download app
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About