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

Semantic Versioning NodeJS
admin07/07/2023

Semantic Versioning NodeJS
How to Use Semantic Versioning in NPM
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.
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

Part 2: The hooks are used popularly in React
admin18/06/2023

Part 2: The hooks are used popularly in React
As a newbie React developer, does not understand when is use stateless (functional) components or stateful components. React hook is a new feature from v16.8, the developer does not worry about react lifecycle, and it is difficult to learn for newbies.
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.
Part 3: React Fragments
admin18/06/2023

Part 3: React Fragments
In this part, I will show you about good benefits when using fragments in React.
Đ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