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

Difference Between Stack and Queue
admin07/04/2024

Difference Between Stack and Queue
In the fundamental data structure, besides the linked list, the stack and queue are also used widely in computer science and programming.
How to integrate ChatGPT-3.5 Turbo into Node.js
admin10/01/2024

How to integrate ChatGPT-3.5 Turbo into Node.js
Step-by-Step Guide to Incorporating ChatGPT-3.5 Turbo into Node.js for Basic ReactJS Applications
Mới nhất

Design Patterns
admin07/08/2023

Design Patterns
The design pattern does not be a specific programming language. Almost programming languages might apply design patterns that to resolve a problem repeat.
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 - Adapter
admin08/08/2023

TypeScript Design Pattern - Adapter
This design pattern acts as a bridge between two different interfaces.
Đ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