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()
}