Core Design Patterns(3) Bridge 桥接模式

VS 2008

桥接模式将抽象与实现分离
使得抽象和实现自称体系,可以独立变化

1. 模式UML描述



2. 应用

    想象在一个软件公司里,雇用了两类程序员:高级程序员,初级程序员。不同级别的程序员每年为公司创造的价值不同。又由于一些客观条件的不同,程序员为公司创造价值这个行为也不是一成不变的。为了隔离变化,将创造价值这个行为抽出来。形成如下的类图:




Programmer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
abstract class Programmer {

        
protected string name;
        
protected IProfitCreator profitCreator;

        
public Programmer(string name) {
            
this.name = name;
        }

        
public virtual string GetName() {
            
return this.name;
        }

        
public void SetProfitCreator(IProfitCreator profitCreator) {
            
this.profitCreator = profitCreator;
        }

        
public abstract void CreateProfit();
    }

}


JuniorProgrammer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
class JuniorProgrammer : Programmer {
        
public JuniorProgrammer(string name) 
            : 
base(name) 
        
        }


        
public override void CreateProfit() {
            
this.profitCreator.CreateProfit();
        }

    }

}


SeniorProgrammer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
class SeniorProgrammer : Programmer {

        
public SeniorProgrammer(string name)
            : 
base(name) 
        
        }

        
public override void CreateProfit() {
            
this.profitCreator.CreateProfit();
        }

    }

}


IProfitCreator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
interface IProfitCreator {
        
void CreateProfit();
    }

}


LowProfitCreator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
class LowProfitCreator : IProfitCreator {
        
IProfitCreator Members
    }

}


HighProfitCreator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Bridge.BLL {
    
class HighProfitCreator : IProfitCreator {
        
IProfitCreator Members
    }

}


Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Bridge.BLL;

namespace DesignPattern.Bridge {
    
class Program {
        
static void Main(string[] args) {

            Programmer junior 
= new JuniorProgrammer("bruce Lee");
            junior.SetProfitCreator(
new LowProfitCreator());
            junior.CreateProfit();

            Programmer senior 
= new SeniorProgrammer("guozhijian");
            senior.SetProfitCreator(
new HighProfitCreator());
            senior.CreateProfit();
        }

    }

}


Output
posted on 2008-02-28 19:22  Tristan(GuoZhijian)  阅读(583)  评论(0编辑  收藏  举报