一个Bridge模式的例子

仿照<java与模式>中bridge模式的例子自己改写的一个例子:
有N个飞机制造商(AirBus.....),每个飞机制造商有客机(PassenagerPlane)和货机(CargoPlane).....实现代码如下:
1.飞机制造商抽象接口:
None.gif      interface  IAirPlaneMaker
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string name dot.gifget;} //制造商名称
InBlock.gif
        IAirPlane producePass(); //生产一量客机
InBlock.gif
         dot.gif//生产一两货机
ExpandedBlockEnd.gif
    }
2.飞机抽象接口
None.gif interface  IAirPlane
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
void Fly(); //飞行方法
ExpandedSubBlockStart.gifContractedSubBlock.gif
        IAirPlaneMaker maker dot.gifget;set;} //制造商
ExpandedBlockEnd.gif
    }

None.gif
3.飞机制造商的一个实现
None.gif class  AirBus : IAirPlaneMaker
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string _name = "AirBus company";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
IAirPlaneMaker Members#region IAirPlaneMaker Members
InBlock.gif
InBlock.gif        
public string name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._name;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IAirPlane producePass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IAirPlane pass 
= new PassengerPlane();
InBlock.gif            pass.maker 
= this;
InBlock.gif
InBlock.gif            
return pass;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }
4 飞机的实现
None.gif class  PassengerPlane : IAirPlane
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private IAirPlaneMaker _maker;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
IAirPlane Members#region IAirPlane Members
InBlock.gif
InBlock.gif        
public void Fly()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Console.WriteLine(
"The " + _maker.name + "'s PassenterPlane is flying~~~!");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IAirPlaneMaker maker
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._maker;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._maker = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif
None.gif    
class  CargoPlane : IAirPlane
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private IAirPlaneMaker _maker;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
IAirPlane Members#region IAirPlane Members
InBlock.gif
InBlock.gif        
public void Fly()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Console.WriteLine(
"The " + _maker.name + "'s CargoPlane is flying~~~!");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public IAirPlaneMaker maker
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._maker;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._maker = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }
5 通过这种bridge模式,客户端代码非常简洁
None.gif  IAirPlaneMaker maker  =   new  AirBus();
None.gif            IAirPlane passPlane 
=  maker.producePass();
None.gif            passPlane.Fly();

考虑:飞机制造商中生产飞机的方式不是很好,藕合度太高,如果要新增加一种飞机生产,则必须改变制造商接口极其所有实现子类。考虑使用工厂模式实现。。。。。

转载于:https://www.cnblogs.com/DotNetFresh/archive/2005/06/23/179618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值