23种设计模式:桥接模式

桥接模式

1.介绍

概念

桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。

这种模式涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。

主要作用

将抽象部分与实现部分分离,使它们都可以独立的变化。

解决的问题

在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。

使用场景

1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。 2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。 3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展。

2.实现

java实现

1.创建桥接实现接口

public interface ProduceApi {
    public void producePhone(int length, int width, String color);
}

2.创建实现了 ProduceApi 接口的实体桥接实现类。

public class BananaPhone implements ProduceApi{
    @Override
    public void producePhone(int length, int width, String color) {
        System.out.println("Produce the Banana phone:");
        System.out.println("color:" + color);
        System.out.println("length:" + length);
        System.out.println("width:" + width);
    }
}

3.创建实现了 ProduceApi 接口的实体桥接实现类。

public class OrangePhone implements ProduceApi{
    @Override
    public void producePhone(int length, int width, String color) {
        System.out.println("Produce the Orange phone:");
        System.out.println("color:" + color);
        System.out.println("length:" + length);
        System.out.println("width:" + width);
    }
}

4.使用 ProduceApi 接口创建抽象类 Phone。

public abstract class Phone {
    protected ProduceApi produceApi;
    protected Phone(ProduceApi produceApi) {
        this.produceApi = produceApi;
    }
    public abstract void produce();
}

5.创建实现了 Phone 抽象类的实体类。

public class ProducePhone extends Phone{
    private int length, width;
    private String color;

    public ProducePhone(ProduceApi produceApi, int length, int width, String color) {
        super(produceApi);
        this.length = length;
        this.width = width;
        this.color = color;
    }

    @Override
    public void produce() {
        produceApi.producePhone(length, width, color);
    }
}

6.创建测试类

public class BridgePatternDemo {
    public static void main(String[] args) {
        ProducePhone bananaPhone = new ProducePhone(new BananaPhone(), 16, 9, "yellow");
        ProducePhone orangePhone = new ProducePhone(new OrangePhone(), 16, 9, "red");

        bananaPhone.produce();
        orangePhone.produce();
    }
}

7.运行结果

Produce the Banana phone:
color:yellow
length:16
width:9
Produce the Orange phone:
color:red
length:16
width:9

golang实现

package BridgePattern

import "fmt"

// ProduceApi 创建桥接实现接口
type ProduceApi interface {
	producePhone(length int, width int, color string)
}

// BananaPhone 创建实现了 ProduceApi 接口的实体桥接实现类
type BananaPhone struct {
	ProduceApi
}

func (*BananaPhone) producePhone(length int, width int, color string) {
	fmt.Println("Produce the Banana phone:")
	fmt.Println("color:", color)
	fmt.Println("length:", length)
	fmt.Println("width:", width)
}

type OrangePhone struct {
	ProduceApi
}

func (*OrangePhone) producePhone(length int, width int, color string) {
	fmt.Println("Produce the Orange phone:")
	fmt.Println("color:", color)
	fmt.Println("length:", length)
	fmt.Println("width:", width)
}


// Produce 使用 ProduceApi 接口创建抽象类 Phone。
type Produce interface {
	produce()
}
type Phone struct {
	Produce
	produceApi ProduceApi
}


// ProducePhone 创建实现了 Phone 抽象类的实体类。
type ProducePhone struct {
	Phone
	length, width int
	color string
}

func (p *ProducePhone) ProducePhone(api ProduceApi, length int, width int, color string) {
	p.produceApi = api
	p.length = length
	p.width = width
	p.color = color
}
func (p *ProducePhone) produce()  {
	p.produceApi.producePhone(p.length, p.width, p.color)
}






package BridgePattern

import "testing"

func TestPhone_Phone(t *testing.T) {
	bananaPhone := ProducePhone{Phone{produceApi: new(BananaPhone)}, 16, 9, "yellow"}
	bananaPhone.produce()
}


其它设计模式

1.工厂模式

2.抽象工厂模式

3.外观模式

4.建造者模式

5.桥接模式

6.命令模式

7.迭代器模式

8.模板模式

9.访问者模式

10.备忘录模式

11.责任链模式

12.中介模式

13.原型模式

14.状态模式

15.策略模式

16.享元模式

17.组合模式

18.解释器模式

19.单例模式

20.适配器模式

21.代理模式

22.装饰器模式

23.观察者模式

posted @ 2021-09-24 22:22  Dawnlight-_-  阅读(41)  评论(0编辑  收藏  举报