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

NodeJS Verify and Decode Cognito JWT Tokens
admin12/06/2023

NodeJS Verify and Decode Cognito JWT Tokens
In this article, I will show you how to verify and decode the Cognito JWT Tokens token.
Create Cognito User Pool with AWS CDK
admin09/06/2023

Create Cognito User Pool with AWS CDK
In the previous post, I showed you how to create a simple S3 bucket. Next, in this article, I will guide you to create a Cognito User Pool.
TypeScript Design Pattern - Singleton
admin07/08/2023

TypeScript Design Pattern - Singleton
The singleton ensures only a single install is created in a class and provides a method to access this instance everywhere in a codebase.
Mới nhất

JOI - API schema validation
admin12/06/2023

JOI - API schema validation
Data validation is one of topics that I am interesting. I always review my code after developed features or fixed bugs. There are many places where need to validate data, it is really terrible. Some cases, we need to validate data input because ensure the data into API, it will not make any problems to crash system.
NodeJS Verify and Decode Cognito JWT Tokens
admin12/06/2023

NodeJS Verify and Decode Cognito JWT Tokens
In this article, I will show you how to verify and decode the Cognito JWT Tokens token.
TypeScript Design Pattern - Builder
admin07/08/2023

TypeScript Design Pattern - Builder
TypeScript Design Pattern - Builder
Đ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