首页 > 其他分享 >结构型——桥接模式

结构型——桥接模式

时间:2023-05-27 16:57:15浏览次数:64  
标签:桥接 模式 int Shape radius DrawAPI public 结构型

推荐文档:

https://www.cnblogs.com/zhili/p/DesignPatternSummery.html

https://www.runoob.com/design-pattern/design-pattern-tutorial.html

什么是桥接模式?

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

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

我们通过下面的实例来演示桥接模式(Bridge Pattern)的用法。其中,可以使用相同的抽象类方法但是不同的桥接实现类,来画出不同颜色的圆。

介绍

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

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

何时使用:实现系统可能有多个角度分类,每一种角度都可能变化。

如何解决:把这种多角度分类分离出来,让它们独立变化,减少它们之间耦合。

关键代码:抽象类依赖实现类。

应用实例: 1、猪八戒从天蓬元帅转世投胎到猪,转世投胎的机制将尘世划分为两个等级,即:灵魂和肉体,前者相当于抽象化,后者相当于实现化。生灵通过功能的委派,调用肉体对象的功能,使得生灵可以动态地选择。 2、墙上的开关,可以看到的开关是抽象的,不用管里面具体怎么实现的。

优点: 1、抽象和实现的分离。 2、优秀的扩展能力。 3、实现细节对客户透明。

缺点:桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。

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

注意事项:对于两个独立变化的维度,使用桥接模式再适合不过了。

实现

我们有一个作为桥接实现的 DrawAPI 接口和实现了 DrawAPI 接口的实体类 RedCircleGreenCircleShape 是一个抽象类,将使用 DrawAPI 的对象。BridgePatternDemo 类使用 Shape 类来画出不同颜色的圆。

 

 

 1 namespace Biao.DesignPatterns.StructuralPatterns
 2 {
 3     /// <summary>
 4     /// 创建桥接实现接口
 5     /// </summary>
 6     public interface DrawAPI
 7     {
 8         public void DrawCircle(int radius, int x, int y);
 9     }
10 
11     /// <summary>
12     /// 创建2个实现DrawAPI接口的实体类
13     /// </summary>
14     public class RedCircle : DrawAPI
15     {
16         public void DrawCircle(int radius, int x, int y)
17         {
18             Console.WriteLine("Drawing Circle[ color: red, radius: "
19          + radius + ", x: " + x + ", " + y + "]");
20         }
21     }
22 
23     public class GreenCircle : DrawAPI
24     {
25         public void DrawCircle(int radius, int x, int y)
26         {
27             Console.WriteLine("Drawing Circle[ color: green, radius: "
28          + radius + ", x: " + x + ", " + y + "]");
29         }
30     }
31 
32     /// <summary>
33     /// 使用DrawAPI创建抽象类Shape
34     /// </summary>
35     public abstract class Shape
36     {
37         protected DrawAPI drawAPI;
38         protected Shape(DrawAPI drawAPI)
39         {
40             this.drawAPI = drawAPI;
41         }
42         public abstract void Draw();
43     }
44 
45     /// <summary>
46     /// 创建了Shape抽象类的实体类Circle
47     /// </summary>
48     public class Circle : Shape
49     {
50         public int x, y, radius;
51 
52         public Circle(int x, int y, int radius, DrawAPI drawAPI) : base(drawAPI)
53         {
54             this.x = x;
55             this.y = y;
56             this.radius = radius;
57         }
58 
59         public override void Draw()
60         {
61             drawAPI.DrawCircle(radius, x, y);
62         }
63     }
64 }
65  
66 
67 static void TestBridgePattern()
68 {
69     Console.WriteLine("---------------测试桥接模式---------------");
70     StructuralPatterns.Shape redShape = new StructuralPatterns.Circle(100, 100, 10, new RedCircle());
71     StructuralPatterns.Shape greenShape = new StructuralPatterns.Circle(100, 100, 10, new GreenCircle());
72     redShape.Draw();
73     greenShape.Draw();
74 }


标签:桥接,模式,int,Shape,radius,DrawAPI,public,结构型
From: https://www.cnblogs.com/swbna/p/17436962.html

相关文章

  • 结构型——装饰器模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是装饰器模式?装饰器模式(DecoratorPattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型......
  • 结构型——组合模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是组合模式?组合模式(CompositePattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合......
  • 创建型——工厂模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是工厂模式?工厂模式(FactoryPattern)是最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式......
  • 创建型——抽象工厂模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是抽象工厂模式?抽象工厂模式(AbstractFactoryPattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种......
  • 创建型——建造者模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是建造者模式?建造者模式(BuilderPattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它......
  • 创建型——原型模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是原型模式?原型模式(PrototypePattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创......
  • 结构型——适配器模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是适配器模式?适配器模式(AdapterPattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立......
  • 设计模式概述
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html为什么要使用设计模式?使用设计模式的根本原因是适应变化,为了增加代码复用率,是软件更具有维护性和可扩展性。设计原则单一职责原则......
  • 创建型——单例模式
    推荐文档:https://www.cnblogs.com/zhili/p/DesignPatternSummery.htmlhttps://www.runoob.com/design-pattern/design-pattern-tutorial.html什么是单例模式?单例模式(SingletonPattern)是最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方......
  • 设计模式之备忘录(Memento)
    概述备忘录模式(MementoPattern),是行为型模式设计模式之一,该模式用于保存对象当前状态,并且在之后可以再次恢复到此状态。备忘录模式实现的方式需要保证被保存的对象状态不能被对象从外部访问,目的是为了保护被保存的这些对象状态的完整性以及内部实现不向外暴露,本篇博客,我们就来......