Go and Java implement bridge mode

Go and Java implement bridge mode

Let's illustrate bridging by playing video files of different formats (mpeg, rmvb, avi, and wmv) across platforms (Windows, Mac, and Linux)

pattern usage.

1. Bridge mode

Bridging is used to decouple abstraction from implementation so that the two can change independently. This type of design pattern is a structural pattern, which provides abstraction

The bridging structure between visualization and realization is used to realize the decoupling of the two.

This pattern involves an interface that acts as a bridge, making the functionality of the entity class independent of the interface implementing class. These two types of classes can be structurally changed to interact with each other.

does not affect.

The purpose of the bridge pattern is to separate the abstraction from the implementation so that they can vary independently.

separated so that they can be changed independently. It connects abstraction and implementation by way of composition rather than inheritance.

  • Intent: To separate the abstraction from the implementation so that they can both vary independently.

  • The main solution: In the case of multiple possible changes, using inheritance will cause a class explosion problem, and it is not flexible to expand.

  • When to use: Implementation systems may have multiple perspective classifications, each of which may vary.

  • How to solve: Separate this multi-angle classification, let them change independently, and reduce the coupling between them.

  • Key code: the abstract class depends on the implementation class.

  • Application examples: 1. Zhu Bajie was reincarnated from Marshal Tianpeng to pig. The mechanism of reincarnation divides the world into two levels, namely: soul and body,

    The former is equivalent to abstraction, and the latter is equivalent to realization. The living being calls the function of the physical object through the delegation of functions, so that the living being can dynamically choose

    select. 2. The switch on the wall, the switch that can be seen is abstract, regardless of how it is realized inside.

  • Advantages: 1. Separation of abstraction and implementation. 2. Excellent scalability. 3. The implementation details are transparent to customers.

  • Disadvantages: The introduction of the bridge mode will increase the difficulty of system understanding and design. Since the aggregation relationship is established on the abstract layer, developers are required to

    Design and program.

  • Usage scenario: 1. If a system needs to add more flexibility between the abstract role and the concrete role of the component, avoid two levels

    Establish a static inheritance relationship between them, and through the bridge mode, they can establish an association relationship at the abstract layer. 2. For those who do not want to use the

    The bridge mode is especially suitable for systems where the number of system classes increases sharply due to multi-level inheritance. 3. There are two independent changes in a class

    dimension, and both dimensions need to be expanded.

  • Note: For two dimensions that change independently, using the bridge mode is perfect.

  • The following are a few key roles of the bridge mode:

    Abstraction: Defines an abstract interface, usually containing a reference to an implementing interface.

    Extended Abstraction (Refined Abstraction): The extension to abstraction can be a subclass of an abstract class or a concrete implementation class.

    Implementation (Implementor): Define the implementation interface and provide the interface for basic operations.

    Concrete Implementor: A concrete class that implements an interface.

  • applicability:

    You don't want to have a fixed binding between an abstraction and its implementation, for example, because, at run time, the implementation

    Sections should be selectable or toggleable.

    Both the abstraction of a class and its implementation should be able to be extended by generating subclasses. At this time, the Bridge mode allows you to connect different abstractions.

    port and implementation parts, and extend them separately.

    Modifications to an abstract implementation part should have no impact on the client, that is, the client's code does not need to be recompiled.

    If there are many classes to generate, you have to decompose an object into two parts.

    Imagine sharing the implementation (possibly using reference counting) among multiple objects, but at the same time require that the client is not aware of this.

2. Go implements bridge mode

package bridge

// ==========Video==========
type Video interface {
    
    
	Format()
}
package bridge

import "fmt"

// ==========Avi==========
type Avi struct {
    
    
}

func (avi *Avi) Format() {
    
    
	fmt.Println("avi format!")
}
package bridge

import "fmt"

// ==========Mpeg==========
type Mpeg struct {
    
    
}

func (mpeg *Mpeg) Format() {
    
    
	fmt.Println("mpeg format!")
}
package bridge

import "fmt"

// ==========Rmvb==========
type Rmvb struct {
    
    
}

func (rmvb *Rmvb) Format() {
    
    
	fmt.Println("rmvb format!")
}
package bridge

import "fmt"

// ==========Wmv==========
type Wmv struct {
    
    
}

func (wmv *Wmv) Format() {
    
    
	fmt.Println("wmv format!")
}
package bridge

// ==========Platform==========
type Platform interface {
    
    
	Play()
}
package bridge

import "fmt"

// ==========Linux==========
type Linux struct {
    
    
	Video
}

func (linux *Linux) Play() {
    
    
	fmt.Println("linux platform play:")
	linux.Video.Format()
}
package bridge

import "fmt"

// ==========Windows==========
type Windows struct {
    
    
	Video
}

func (windows *Windows) Play() {
    
    
	fmt.Println("windows platform play:")
	windows.Video.Format()
}
package bridge

import "fmt"

// ==========Mac==========
type Mac struct {
    
    
	Video
}

func (mac *Mac) Play() {
    
    
	fmt.Println("mac platform play:")
	mac.Video.Format()
}
package main

import . "proj/bridge"

func main() {
    
    

	// ==========Windows=======
	// ==========Mpeg==========
	winMpeg := &Windows{
    
    Video: &Mpeg{
    
    }}
	winMpeg.Play()

	// ==========Linux========
	// ==========Wmv==========
	linuxWmv := &Linux{
    
    Video: &Wmv{
    
    }}
	linuxWmv.Play()

	// ==========Mac==========
	// ==========Avi==========
	macAvi := &Mac{
    
    Video: &Avi{
    
    }}
	macAvi.Play()
}
# 输出
windows platform play:
mpeg format!
linux platform play:
wmv format!
mac platform play:
avi format!

3. Java implements bridge mode

package com.bridge;

// ==========Video==========
public interface Video {
    
    
    void format();
}
package com.bridge;

// ==========Avi==========
public class Avi implements Video{
    
    
    @Override
    public void format() {
    
    
        System.out.println("avi format!");
    }
}
package com.bridge;

// ==========Mpeg==========
public class Mpeg implements Video{
    
    
    @Override
    public void format() {
    
    
        System.out.println("mpeg format!");
    }
}
package com.bridge;

// ==========Rmvb==========
public class Rmvb implements Video{
    
    
    @Override
    public void format() {
    
    
        System.out.println("rmvb format!");
    }
}
package com.bridge;

// ==========Wmv==========
public class Wmv implements Video{
    
    
    @Override
    public void format() {
    
    
        System.out.println("wmv format!");
    }
}
package com.bridge;

// ==========Platform==========
public abstract class Platform {
    
    
    Video video;
    abstract void play();
}
package com.bridge;

// ==========Linux==========
public class Linux extends Platform{
    
    

    public Linux(Video video){
    
    
        this.video = video;
    }

    @Override
    public void play() {
    
    
        System.out.println("linux platform play:");
        this.video.format();
    }
}
package com.bridge;

// ==========Mac==========
public class Mac extends Platform {
    
    

    public Mac(Video video){
    
    
        this.video = video;
    }

    @Override
    public void play() {
    
    
        System.out.println("mac platform play:");
        this.video.format();
    }
}
package com.bridge;

// ==========Windows==========
public class Windows extends Platform{
    
    

    public Windows(Video video){
    
    
        this.video = video;
    }

    @Override
    public void play() {
    
    
        System.out.println("windows platform play:");
        this.video.format();
    }
}
package com.bridge;

public class Test {
    
    
    public static void main(String[] args) {
    
    

        // ==========Windows==========
        // ==========Mpeg=============
        Platform winMpeg = new Windows(new Mpeg());
        winMpeg.play();

        // ==========Linux==========
        // ==========Wmv=============
        Platform linuxWmv = new Linux(new Wmv());
        linuxWmv.play();

        // ==========Mac==========
        // ==========Avi==========
        Mac macAvi = new Mac(new Avi());
        macAvi.play();
    }
}
# 输出
windows platform play:
mpeg format!
linux platform play:
wmv format!
mac platform play:
avi format!

Guess you like

Origin blog.csdn.net/qq_30614345/article/details/130390255