The introduction
Design Patterns are code Design lessons that are used over and over again in software Design. The purpose of using design patterns is to make code reusable, extensible and maintainable.
A computer is composed of hardware devices and software systems. In order to improve the performance of the computer, users often replace the hardware or reinstall the system rather than choose to replace a new computer (create a new class). So this requires the computer and hardware devices, operating system separation, can be independent change, this replacement conforms to the characteristics of the bridge mode.
define
Structural mode
Used to decouple abstraction from implementation so that the two can vary independently
The bridge pattern is an extensibility design for classes that replaces inheritance with composition relationships, thus reducing the coupling of abstraction and implementation.
The model structure
1. Abstraction
Define an abstract class that contains a reference to an implementable object indicating what interface (functionality) is expected
2. Refined Abstraction
Abstract implementation that implements business methods in roles through composite relational calls
3. Implementor
Define an implementation class that contains the business methods needed for abstraction
4. Concrete Implementor
The realization of realization, the specific implementation of the business method
In front-end development, there is no habit of creating interfaces. So instead of creating abstract and implementable roles, you create only extended abstract and implementable roles.
Here is:
Description: Through the bridge mode, you can match different functions of the module.
The characteristics of
advantages
- The bridge mode conforms to the open and close principle
- The bridge mode conforms to the principle of composite multiplexing
- The bridge pattern separates abstraction from implementation and is more extensible
disadvantages
- Bridge mode is a high-level design that is difficult and depends on the skill level of the developer.
implementation
Extend abstract roles
class Abstraction { implementor; constructor(implementor) { this.implementor = implementor; } do() { this.implementor.doSomething(); }}Copy the code
Extend the implementation role
Class Implementor1 {doSomething() {console.log(' This is Implementor1'); }} class Implementor2 {doSomething() {console.log(' This is Implementor2'); }}Copy the code
use
const implementor1 = new Implementor1();
const implementor2 = new Implementor2();
const abstraction1 = new Abstraction(implementor1);
const abstraction2 = new Abstraction(implementor2);
abstraction1.do();
abstraction2.do();
Copy the code
Free collocation, can form modules with different functions. If you want to extend the functionality, you can choose to develop either a new abstract character or a new implementation character. For example: computer and operating system, system unchanged, redesign computer hardware, there will be different brands; Computer unchanged, change the system, will bring different experience.
For example,
Bridge mode simulates the computer and operating system
Analysis: Computer is an abstract role, the operating System is an implementation role. We can bridge the computer from the operating system,
The computer
class Computer { system; constructor(system) { this.system = system; } launch() { this.system.launch(); }}Copy the code
system
Class WindowsSystem {launch() {console.log(' Windows launch '); }}Copy the code
Class LinuxSystem {launch() {console.log(' Linux start '); }}Copy the code
use
const windows = new WindowsSystem(); const linux = new LinuxSystem(); const windowComputer = new Computer(windows); const linuxComputer = new Computer(linux); windowComputer.launch(); // Launch linuxcomputer.launch (); // The Linux computer startsCopy the code
Application scenarios
Bridge mode is widely used in all systems, large and small, for the sake of scalability, you can see the shadow of bridge mode.
Here is not a list, any system multidimensional changes, can use the bridge pattern to design and development.