在DotNet中实现Bridge模式

None.gif
None.gif
using  System;
None.gif
namespace  Bridge.CoffeeBridgeImp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Coffee 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract class Coffee
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private CoffeeImp coffeeImp ;
InBlock.gif
InBlock.gif        
public CoffeeImp getCoffeeImp() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.coffeeImp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void setCoffeeImp() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.coffeeImp = CoffeeImpSingleton.getCoffeeImp();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 加不加奶的行为接口
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract  class CoffeeImp
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public abstract void pourCoffeeImp(string name) ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif    
/// CoffeeImpSingleton 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CoffeeImpSingleton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static  CoffeeImp  coffeeImp;
InBlock.gif
InBlock.gif        
public static CoffeeImp getCoffeeImp () 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return coffeeImp;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public CoffeeImpSingleton(CoffeeImp coffeeImp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             CoffeeImpSingleton.coffeeImp 
= coffeeImp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// FragrantCoffeeImp 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FragrantCoffeeImp:CoffeeImp    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public FragrantCoffeeImp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public override void pourCoffeeImp(string name )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(name 
+"什么也没加,清香");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif    
///中杯
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MediumCoffee :Coffee
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MediumCoffee() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.setCoffeeImp();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void pourCoffee() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CoffeeImp  coffeeImp 
= this.getCoffeeImp(); 
InBlock.gif            
for (int index =0; index <2; index++
InBlock.gif                coffeeImp.pourCoffeeImp(
"小杯");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif    
/// MilkCoffeeImp 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MilkCoffeeImp :CoffeeImp    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MilkCoffeeImp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public override void pourCoffeeImp(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(name 
+"加了美味的牛奶");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SuperSizeCoffee 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SuperSizeCoffee:Coffee
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SuperSizeCoffee()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.setCoffeeImp();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void pourCoffee() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CoffeeImp  coffeeImp 
=base.getCoffeeImp();
InBlock.gif            
for (int index =0; index <5; index++
InBlock.gif                coffeeImp.pourCoffeeImp(
"大杯");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    一下为测试代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif    
/// BridgeTest 测试Bridge模式
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class BridgeTest
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
InBlock.gif            
//拿出牛奶
InBlock.gif
//            CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton(new FragrantCoffeeImp());
InBlock.gif
            CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton(new MilkCoffeeImp());
InBlock.gif
InBlock.gif            
//中杯加奶
InBlock.gif
            MediumCoffee mediumCoffee = new MediumCoffee();
InBlock.gif            mediumCoffee.pourCoffee();
InBlock.gif
InBlock.gif            
//大杯加奶
InBlock.gif
            SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee();
InBlock.gif            superSizeCoffee.pourCoffee();
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/alonestarsz/archive/2004/11/19/65826.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值