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

推荐订阅源

博客园 - Franky
J
Java Code Geeks
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
L
LangChain Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
Martin Fowler
Martin Fowler
月光博客
月光博客
Y
Y Combinator Blog
U
Unit 42
D
Docker
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(3) Bridge 桥接模式
Tristan(GuoZhijian) · 2008-02-28 · via 博客园 - Tristan(GuoZhijian)

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