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

推荐订阅源

K
Kaspersky official blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Security Latest
Security Latest
Spread Privacy
Spread Privacy
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
U
Unit 42
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Scott Helme
Scott Helme
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
爱范儿
爱范儿
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Latest news
Latest news
GbyAI
GbyAI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
Jina AI
Jina AI
AI
AI
NISL@THU
NISL@THU
I
Intezer
G
GRAHAM CLULEY
B
Blog
S
Secure Thoughts
IT之家
IT之家
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - Cameo

解决VSTO项目重定向到.NET Framework 4的编译和运行时错误 Excel 应用程序如何创建数据透视表 值类型与引用类型 Visual studio tools for office体系结构 C#类型转换 VSTO开发团队成立 15. Command 命令(行为型模式) 14. Template Method模板方法(行为型模式) 13. Proxy代理(结构型模式) 12. Flyweight享元(结构型模式) 11. Facade外观[门面模式](结构型模式) 10. Decorator 装饰(结构型模式) 9. Composite 组合(结构型模式) 7. Adapter 适配器(结构型模式) 6. Prototype 原型(创建型模式) 5. Factory Method 工厂方法(创建型模式的基础) 4.Builder 建造者(生成器)(创建型模式) 书籍收藏 3. Abstract Factory 抽象工厂(创建型模式)
8. Bridge 桥接(结构型模式)
Cameo · 2008-06-19 · via 博客园 - Cameo

抽象与实现
    抽象不应该依赖于实现细节,实现细节应该依赖于抽象。

动机(Motivation)
    思考上述问题的症结:事实上由于Tank类型的固有逻辑,使得Tank类型具有了两个变化的维度——一个变化的维度为“平台的变化”,一个变化的维度为“型号的变化”。
    如何应对这种“多维度的变化”?如何利用面向对象技术来使得Tank类型可以轻松地沿着“平台”和“型号”两个方向变化,而不引入额外的复杂度?

桥梁模式的用意

【GOF95】在提出桥梁模式的时候指出,桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。

脱耦

所谓耦合,就是两个实体的行为的某种强关联。而将它们的强关联去掉,就是耦合的解脱,或称脱耦。在这里,脱耦是指将抽象化和实现化之间的耦合解脱开,或者说是将它们之间的强关联改换成弱关联。

将两个角色之间的继承关系改为聚合关系,就是将它们之间的强关联改换成为弱关联。因此,桥梁模式中的所谓脱耦,就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以相对独立地变化。这就是桥梁模式的用意。


意图(Intent)
    将抽象部分与实现部分分离,使它们都可以独立地变化
                                                                    ——《设计模式》GoF

结构(Structure)

namespace Bridge1
{
    
public abstract class Tank
    
{
       
        
//通过对象组合来降低耦合度
        protected TankPlatformImplementation tankImpl;

        
public Tank(TankPlatformImplementation tankImpl)
        
{
            
this.tankImpl = tankImpl;
        }


        
public TankPlatformImplementation TankImpl
        
{
            
get
            
{
                
return this.tankImpl;
            
            }

            
set
            
{
                
this.tankImpl = tankImpl;
            }

        }

        
public abstract void Shot();
        
public abstract void Run();
        
public abstract void Stop();
    }



    
/// <summary>
    
/// 用子类化来表达变化,是面向对象一个常用的做法
    
/// </summary>

    public abstract class TankPlatformImplementation
    
{
        
public abstract void MoveTankTo(Point to)
        

        }

        
public abstract void DrawTank()
        

            
        }


        
public abstract void DoShot()
        

            
        }


    }


    
public class PCTankImplementation : TankPlatformImplementation
    
{
        
public override void MoveTankTo(Point to)
        
{
            
//..
        }

        
public override void DoShot()
        
{
            
//
        }

        
public override void DrawTank()
        
{
            
            
//..
        }

    }


    
public class MobileTankImplementation : TankPlatformImplementation
    
{
        
public override void MoveTankTo(Point to)
        
{
            
//..
        }

        
public override void DoShot()
        
{
            
//
        }

        
public override void DrawTank()
        
{
            
//.
        }


    }


    
public class T50 : Tank
    
{
        
//继承可能成为的变化点
        public T50(TankPlatformImplementation tankImpl)
            : 
base(tankImpl)
        

        
        }


        
public override void Shot()
        
{
            
//.T50
            TankImpl.DoShot();

        }

        
public override void Run()
        
{
            
//T50
        }

        
public override void Stop()
        
{
            
//.T50
        }

    }


    
public class T75 : Tank
    
{
        
public T75(TankPlatformImplementation tankImpl)
            : 
base(tankImpl)
        
{ }
        
public override void Shot()
        
{
            
//.T75
            TankImpl.DoShot();

        }

        
public override void Run()
        
{
            
//T75
        }

        
public override void Stop()
        
{
            
//.T75
        }

    }


    
public class T90 : Tank
    
{
        
public T90(TankPlatformImplementation tankImpl)
            : 
base(tankImpl)
        

        
        }

        
public override void Shot()
        
{
            
//T90
            TankImpl.DoShot();
        }


        
public override void Run()
        
{
            
//T90
        }

        
public override void Stop()
        
{
            
//.T90
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            TankPlatformImplementation tankImpl 
= new MobileTankImplementation();

            T50 tank 
= new T50(tankImpl);

        }

    }

    
}

Bridge模式的几个要点
    • Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象(Tank的型号)和实现(不同的平台)可以沿着各自的维度来变化
    • 所谓抽象和实现沿着各自纬度的变化,即“子类化”它们,比如不同的Tank型号子类,和不同的平台子类)。得到各个子类之后,便可以任意组合它们,从而获得不同平台上的不同型号。
    • Bridge模式有时候类似于多继承方案,但是多继承方案往往违背单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。
    • Bridge模式的应用一般在“两个非常强的变化维度”,有时候即使有两个变化的维度,但是某个方向的变化维度并不剧烈——换言之两个变化不会导致纵横交错的结果,并不一定要使用Bridge模式。