惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
博客园 - 司徒正美
博客园 - 聂微东
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
A
About on SuperTechFans
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
V
V2EX
I
InfoQ
WordPress大学
WordPress大学
小众软件
小众软件
The Cloudflare Blog
Recent Announcements
Recent Announcements
U
Unit 42
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
Vercel News
Vercel News
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
M
MIT News - Artificial intelligence
Project Zero
Project Zero
美团技术团队
L
LangChain Blog
S
Security @ Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
W
WeLiveSecurity
S
Securelist
H
Hacker News: Front Page
K
Kaspersky official blog
Martin Fowler
Martin Fowler
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net

博客园 - cjfwu

设计模式学习3-Strategy模式 设计模式学习2-Adapter模式 设计模式学习1-Facade模式 设备控制(反馈处理) 通过System.IO.Packaging实现打包和解包 将目录添加环境变量 设备控制之矩阵状态显示 windows shell 编程3(函数解释) windows shell 编程2(浏览文件夹) windows shell 编程1(概念) 不同命名空间下名称和结构相同的类相互序列化与反序列化 通过SvcUtil.exe生成客户端代码和配置 分组 在“添加引用”对话框中显示需要的Assembly 只运行一个实例 SVN操作 托盘操作 获得树节点的高度 枚举的操作
设计模式学习4-Bridge模式
cjfwu · 2013-04-07 · via 博客园 - cjfwu

Bridge模式(桥接模式):

定义:将抽象与其实现解耦,使它们都可以独立地变化。(注:这里的实现指抽象类及其派生类用来实现自己的对象)

实现:将实现封装在一个抽象类中。在要实现的抽象的基类中包含一个实现的句柄。

结构图:

 其中Abstraction为要实现的对象接口,Implementor为具体的实现类接口。Abstraction的派生类使用Implementor的派生类,却无需知道自己具体使用哪一个ConcreteImplementor。

 代码示例:

考虑这样的软件需求:需要编写一个程序,使用两个绘图程序(DP1和DP2)之一绘制矩形和圆形。

首先先定义实现,Drawing为实现的基类,V1Drawing和V2Drawing为Drawing的子类,V1Drawing使用绘图程序DP1画线和画圆,V2Drawing使用绘图程序DP2画线和画圆。

 1     public abstract class Drawing {
 2         public abstract void DrawLine(double x1, double y1, double x2, double y2);
 3         public abstract void DrawCircle(double x, double y, double r);
 4     }
 5 
 6     public class V1Drawing : Drawing {
 7         public override void DrawLine(double x1, double y1, double x2, double y2) {
 8             dp1.DrawLine(x1, y1, x2, y2);
 9         }
10 
11         public override void DrawCircle(double x, double y, double r) {
12             dp1.DrawCircle(x, y, r);
13         }
14 
15         private DP1 dp1 = new DP1();
16     }
17 
18     public class V2Drawing : Drawing {
19         public override void DrawLine(double x1, double y1, double x2, double y2) {
20             dp2.DrawLine(x1, y1, x2, y2);
21         }
22 
23         public override void DrawCircle(double x, double y, double r) {
24             dp2.DrawCircle(x, y, r);
25         }
26 
27         private DP2 dp2 = new DP2();
28     }

然后定义抽象,也就是使用实现的对象的基类,这里就是矩形和圆形的基类(Sharp),Sharp里有一个Drawing的句柄并由构造函数传进来,而Sharp并不知道Drawing具体的类型(V1Drawing还是V2Drawing),这样就是将抽象和实现隔离开来了(解耦)。

 1     public abstract class Sharp {
 2         public Sharp(Drawing drawing) {
 3             this.drawing = drawing;
 4         }
 5 
 6         public abstract void Draw();
 7 
 8         protected void DrawLine(double x1, double y1, double x2, double y2) {
 9             this.drawing.DrawLine(x1, y1, x2, y2);
10         }
11 
12         protected void DrawCircle(double x, double y, double r) {
13             this.drawing.DrawCircle(x, y, r);
14         }
15 
16         private Drawing drawing;
17     }

后面就是定义抽象的派生类,

 1     public class Rectangle : Sharp {
 2         public Rectangle(Drawing drawing, double x1, double y1, double x2, double y2)
 3             : base(drawing) {
 4                 this.x1 = x1;
 5                 this.y1 = y1;
 6                 this.x2 = x2;
 7                 this.y2 = y2;
 8             }
 9 
10         public override void Draw() {
11             DrawLine(x1, y1, x2, y1);
12             DrawLine(x2, y1, x2, y2);
13             DrawLine(x1, y1, x1, y2);
14             DrawLine(x1, y2, x1, y1);
15         }
16 
17         private double x1, y1, x2, y2;
18     }
19 
20     public class Circle : Sharp {
21         public Circle(Drawing drawing, double x, double y, double r)
22             : base(drawing) {
23                 this.x = x;
24                 this.y = y;
25                 this.r = r;
26         }
27 
28         public override void Draw() {
29             DrawCircle(x, y, r);
30         }
31 
32         private double x, y, r;
33     }

最后就是执行程序

1     public class Client {
2         static void Main(string[] args) {
3             List<Sharp> sharps = new Factory().GetSharps();
4             foreach(var each in sharps) {
5                 each.Draw();
6             }
7         }
8     }

 总结:

通过Bridge模式可以将抽象和实现解耦,可以独立地变化,比如增加一个椭圆形状或者多了一个绘图程序DP3,实现起来就变得容易了。

同时应该遵循应对变化的基本策略:找到变化并封装之;优先使用对象聚集而不是类继承。