Danh mụcThẻBài viết

admin

I'm a Full-stack developer

Thẻ

Linked List
Data Structure
Chat GPT
Design Pattern
Microservices
API
AWS CDK
ReactJS
AWS Lightsail
Flutter Mobile
TypeScript Design Pattern - Bridge
Ngày đăng: 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

Đề xuất

Next.js 14 App Router Localization with next-intl
admin07/07/2024

Next.js 14 App Router Localization with next-intl
In this article, I will illustrate how to implement next-intl localization in a Next.js 14 application
TypeScript Design Pattern - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
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
Mới nhất

TypeScript Design Pattern - Prototype
admin07/08/2023

TypeScript Design Pattern - Prototype
The prototype pattern is one of the Creational pattern groups. The responsibility is to create a new object through clone the existing object instead of using the new key. The new object is the same as the original object, and we can change its property does not impact the original object.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
TypeScript Design Pattern - Bridge
admin08/08/2023

TypeScript Design Pattern - Bridge
Decouple an abstraction from its implementation so that the two can vary independently.
Đ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
Góp ý
Họ & Tên
Số điện thoại
Email
Nội dung
Tải ứng dụng
hotline

copyright © 2023 - AGAPIFA

Privacy
Term
About