结构型设计模式之桥接模式(Bridge)
结构 |
![]() |
意图 | 将抽象部分与它的实现部分分离,使它们都可以独立地变化。 |
适用性 |
|
using System; class Abstraction
{
protected Implementation impToUse; public void SetImplementation(Implementation i)
{
impToUse = i;
} virtual public void DumpString(string str)
{
impToUse.DoStringOp(str);
}
} class DerivedAbstraction_One : Abstraction
{
override public void DumpString(string str)
{
str += ".com";
impToUse.DoStringOp(str);
}
} class Implementation
{
public virtual void DoStringOp(string str)
{
Console.WriteLine("Standard implementation - print string as is");
Console.WriteLine("string = {0}", str);
}
} class DerivedImplementation_One : Implementation
{
override public void DoStringOp(string str)
{
Console.WriteLine("DerivedImplementation_One - don't print string");
}
} class DerivedImplementation_Two : Implementation
{
override public void DoStringOp(string str)
{
Console.WriteLine("DerivedImplementation_Two - print string twice");
Console.WriteLine("string = {0}", str);
Console.WriteLine("string = {0}", str);
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Abstraction SetupMyParticularAbstraction()
{
// we localize to this method the decision which abstraction and
// which implementation to use. These need to be decided
// somewhere and we do it here. All teh rest of the client
// code can work against the abstraction object.
Abstraction a = new DerivedAbstraction_One();
a.SetImplementation(new DerivedImplementation_Two());
return a;
} public static int Main(string[] args)
{
Client c = new Client();
Abstraction a = c.SetupMyParticularAbstraction(); // From here on client code thinks it is talking to the
// abstraction, and will not need to be changed as
// derived abstractions are changed. // more client code using the abstraction goes here
// . . .
a.DumpString("Clipcode"); return ;
}
}
桥接模式
结构型设计模式之桥接模式(Bridge)的更多相关文章
- 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)
原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- python 设计模式之桥接模式 Bridge Pattern
#写在前面 前面写了那么设计模式了,有没有觉得有些模式之间很类似,甚至感觉作用重叠了,模式并不是完全隔离和独立的,有的模式内部其实用到了其他模式的技术,但是又有自己的创新点,如果一味地认为每个模式都是 ...
- 二十四种设计模式:桥接模式(Bridge Pattern)
桥接模式(Bridge Pattern) 介绍将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象 ...
- 设计模式 笔记 桥接模式 Bridge
//---------------------------15/04/15---------------------------- //Bridge 桥接模式----对象结构型模式 /* 1:意图:将 ...
- [设计模式] 7 桥接模式 bridge
#include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...
- 设计模式之桥接模式(Bridge)--结构模型
1.意图 将抽象部分与它的实现部分分离,使它们可以独立地变化. 2.适用性 你不希望在抽象和它的实现部分之间有一个固定的绑定关系. 类的抽象与它的实现都应该可以通过子类的方式加以扩展. 抽象部分与实现 ...
- 设计模式之桥接模式(Bridge)
桥接模式与原理:将抽象部分与实现部分分离,使它们都可以独立的变化.最终的结果表现在实现类中.两者之间属于等价关系,即实现部分和抽象部分可以相互交换. 代码如下 #include <iostrea ...
- 【设计模式】—— 桥接模式Bridge
前言:[模式总览]——————————by xingoo 模式意图 这个模式使用的并不多,但是思想确实很普遍.就是要分离抽象部分与实现部分. 实现弱关联,即在运行时才产生依赖关系. 降低代码之间的耦合 ...
随机推荐
- Flask初学者:视图函数/方法返回值(HTML模板/Response对象)
返回HTML模板:使用“from flask import render_template”,在函数中传入相对于文件夹“templates”HTML模板路径名称字符串即可(默认模板路径),flask会 ...
- R-描述性统计
RT...老实说这一章我是抖的...但是,加油- # 从1:100中均匀抽取size个数据,replace=TRUE指有放回抽样,数据可以重复 x = sample(1:100, size = 100 ...
- 大话目标检测经典模型(RCNN、Fast RCNN、Faster RCNN)
目标检测是深度学习的一个重要应用,就是在图片中要将里面的物体识别出来,并标出物体的位置,一般需要经过两个步骤:1.分类,识别物体是什么 2.定位,找出物体在哪里 除了对单个物体进行检测,还要能支持 ...
- 学习网络请求返回json对应的model
原来泛型可以这样用: 网络返回基类,返回一个code,msg,body,其中body不确定,所以,我们把它写成泛型 import org.json.JSONObject; /** * 网络请求的基类 ...
- laravel - ReflectionException in Container.php, Class not found?
SIGN UPSIGN IN CATALOG SERIES PODCAST DISCUSSIONS ReflectionException in Container.php, Class not fo ...
- 查询数据库里当前用户下的所有表的总共数据sql
select t.table_name,t.num_rows from user_tables t select sum(num_rows) from user_tables t
- angular 模块化之directive
通过使用directive使页面模块化.需要哪部分直接调用即可.原本这些操作需要后端配合,现在前端即可.将原本的html代码拆为不同的模块,然后通过directive衔接加载到主页面中.首先页面通过d ...
- codility
// you can also use imports, for example: // import java.util.*; // you can write to stdout for debu ...
- php静态文件返回304
有时一些静态文件(如图片)会由php输出,会发现请求都是200,静态文件每次都去服务器上请求太浪费资源了,这时如何让浏览器缓存图片呢?就需要我们在php中输出304了. 我们可以利用php中的 HTT ...
- 抓取网站访问者的QQ号码
开源,是一种精神.但不开源,并不是没有精神,而可能是代码写得惨不忍睹,我属于后者.(首先申明:对代码提出意见可接受,虚心接受,但不能人身攻击啊!) 最近闲的蛋疼,喜欢到处看看做得好的站点, 莫 ...