一、概念

桥接模式即将抽象部分与实现部分脱耦,使它们可以独立变化。

二、模型

三、代码实现

// 客户端调用
// 类似Web应用程序
class Client
{
static void Main(string[] args)
{
BusinessObject customers = new CustomersBusinessObject("ShangHai");
customers.Dataacces = new CustomersDataAccess(); customers.Add("小六");
Console.WriteLine("增加了一位成员的结果:");
customers.ShowAll();
customers.Delete("王五");
Console.WriteLine("删除了一位成员的结果:");
customers.ShowAll();
Console.WriteLine("更新了一位成员的结果:");
customers.Update("Learning_Hard");
customers.ShowAll(); Console.Read();
}
} // BLL 层
public class BusinessObject
{
// 字段
private DataAccess dataacess;
private string city; public BusinessObject(string city)
{
this.city = city;
} // 属性
public DataAccess Dataacces
{
get { return dataacess; }
set { dataacess = value; }
} // 方法
public virtual void Add(string name)
{
Dataacces.AddRecord(name);
} public virtual void Delete(string name)
{
Dataacces.DeleteRecord(name);
} public virtual void Update(string name)
{
Dataacces.UpdateRecord(name);
} public virtual string Get(int index)
{
return Dataacces.GetRecord(index);
}
public virtual void ShowAll()
{
Console.WriteLine();
Console.WriteLine("{0}的顾客有:", city);
Dataacces.ShowAllRecords();
}
} public class CustomersBusinessObject : BusinessObject
{
public CustomersBusinessObject(string city)
: base(city) { } // 重写方法
public override void ShowAll()
{
Console.WriteLine("------------------------");
base.ShowAll();
Console.WriteLine("------------------------");
}
} /// <summary>
/// 相当于三层架构中数据访问层(DAL)
/// </summary>
public abstract class DataAccess
{
// 对记录的增删改查操作
public abstract void AddRecord(string name);
public abstract void DeleteRecord(string name);
public abstract void UpdateRecord(string name);
public abstract string GetRecord(int index);
public abstract void ShowAllRecords();
} public class CustomersDataAccess:DataAccess
{
// 字段
private List<string> customers =new List<string>(); public CustomersDataAccess()
{
// 实际业务中从数据库中读取数据再填充列表
customers.Add("Learning Hard");
customers.Add("张三");
customers.Add("李四");
customers.Add("王五");
}
// 重写方法
public override void AddRecord(string name)
{
customers.Add(name);
} public override void DeleteRecord(string name)
{
customers.Remove(name);
} public override void UpdateRecord(string updatename)
{
customers[] = updatename;
} public override string GetRecord(int index)
{
return customers[index];
} public override void ShowAllRecords()
{
foreach (string name in customers)
{
Console.WriteLine(" " + name);
}
} }

C#设计模式(8)——桥接模式的更多相关文章

  1. 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)

    原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...

  2. java面试题之----jdbc中使用的设计模式(桥接模式)

    1.JDBC(JavaDatabase Connectivity) JDBC是以统一方式访问数据库的API. 它提供了独立于平台的数据库访问,也就是说,有了JDBC API,我们就不必为访问Oracl ...

  3. php设计模式之桥接模式

    php设计模式之桥接模式 一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 其实就是讲不相关的东西通过类(本例中是SendInfo)结合在一起,从而 ...

  4. java设计模式7——桥接模式

    java设计模式7--桥接模式 1.桥接模式介绍 桥接模式是将抽象部分与它的实现部分分离,使他们都可以独立的变化.它是一种对象结构型模式,又称为柄体模式或接口模式. 2.解决问题 2.1.将复杂的组合 ...

  5. C++设计模式-Bridge桥接模式

    作用:将抽象部份与它的实现部份分离,使它们都可以独立地变化. 将抽象(Abstraction)与实现(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式 ...

  6. 设计模式之桥接模式(Bridge)

    注:本文不属于原创,而是根据原文重新整理,原文是:我给媳妇解释设计模式:第一部分 设计模式不是基于理论发明的.相反,总是先有问题场景,再基于需求和情景不断演化设计方案,最后把一些方案标准化成“模式”. ...

  7. 【GOF23设计模式】桥接模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_桥接模式.多层继承结构.银行日志管理.管理系统消息管理.人力资源的奖金计算  未用桥接模式: package com.te ...

  8. js设计模式(3)---桥接模式

    0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...

  9. [设计模式] 7 桥接模式 bridge

    #include<iostream> using namespace std; class AbstractionImp { public: virtual ~AbstractionImp ...

  10. java设计模式之桥接模式

    桥接模式 桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化.这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦.这种模式涉及到一个作为桥接的 ...

随机推荐

  1. Bootstrap学习4--Table样式(转载:https://blog.csdn.net/Fanbin168/article/details/53208869)

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html 将<table>标签添加class=‘tab ...

  2. C#自己写的第一个小程序,庆祝一下

    Packages.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...

  3. 【HTTP】初识代理

    Web代理(proxy)位于客户端和服务器端之间.HTTP的代理服务器既是Web服务器端又是Web客户端. 1. 代理和网关的对比 代理连接的是两个或者多个使用相同协议的应用程序. 网关连接的是两个或 ...

  4. linux sort按照指定列排序

    sort怎样按指定的列排序0000 27189 41925425065f 15 419254250663 7 419254250675 5 419254250691 76 419254250693 2 ...

  5. Mac下XAMPP环境中安装MySQLdb

    环境: Mac OS X. Mac下安装MySQLdb模块着实多了些步骤. 用easy_install或者pip安装时有两大问题,"mysql_config not found"和 ...

  6. Pinpoint扩展插件实践笔记

    为链路(spanEvent)添加tag 背景 我们可能需要想在代码中写入特定的信息到调用链中,并且希望对里面的特定key做检索 实现思路 创建一个特定的类,只需要一个方法,再对这个类的方法进行增强,这 ...

  7. etcd -> Highly-avaliable key value store for shared configuration and service discovery

    The name "etcd" originated from two ideas, the unix "/etc" folder and "d&qu ...

  8. poj 1144 Network 【求一个网络的割点的个数 矩阵建图+模板应用】

    题目地址:http://poj.org/problem?id=1144 题目:输入一个n,代表有n个节点(如果n==0就结束程序运行). 在当下n的这一组数据,可能会有若干行数据,每行先输入一个节点a ...

  9. sdut oj 操作系统实验--SSTF磁盘调度算法【操作系统算法】

    操作系统实验--SSTF磁盘调度算法 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 磁盘调度在多道程序设计的计算机系统中,各个进 ...

  10. Eclipse安装Propedit插件、SVN插件、js插件

    1.在线安装Propedit 打开Eclipse的在线安装界面,点击Add Name: propedit Location:http://propedit.sourceforge.jp/eclipse ...