8.桥接模式(Bridge Pattern)
using System; namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
// 创建一个遥控器
RemoteControl remoteControl = new ConcreteRemote(); // 长虹电视机
remoteControl.Implementor = new ChangHong();
remoteControl.On();
remoteControl.SetChannel();
remoteControl.Off();
Console.WriteLine(); // 三星牌电视机
remoteControl.Implementor = new Samsung();
remoteControl.On();
remoteControl.SetChannel();
remoteControl.Off();
Console.Read();
}
} /// <summary>
/// 抽象概念中的遥控器,扮演抽象化角色
/// </summary>
public class RemoteControl
{
// 字段
private TV implementor; // 属性
public TV Implementor
{
get { return implementor; }
set { implementor = value; }
} /// <summary>
/// 开电视机,这里抽象类中不再提供实现了,而是调用实现类中的实现
/// </summary>
public virtual void On()
{
implementor.On();
} /// <summary>
/// 关电视机
/// </summary>
public virtual void Off()
{
implementor.Off();
} /// <summary>
/// 换频道
/// </summary>
public virtual void SetChannel()
{
implementor.tuneChannel();
}
} /// <summary>
/// 具体遥控器
/// </summary>
public class ConcreteRemote : RemoteControl
{
public override void SetChannel()
{
Console.WriteLine("---------------------");
base.SetChannel();
Console.WriteLine("---------------------");
}
} /// <summary>
/// 电视机,提供抽象方法
/// </summary>
public abstract class TV
{
public abstract void On();
public abstract void Off();
public abstract void tuneChannel();
} /// <summary>
/// 长虹牌电视机,重写基类的抽象方法
/// 提供具体的实现
/// </summary>
public class ChangHong : TV
{
public override void On()
{
Console.WriteLine("长虹牌电视机已经打开了");
} public override void Off()
{
Console.WriteLine("长虹牌电视机已经关掉了");
} public override void tuneChannel()
{
Console.WriteLine("长虹牌电视机换频道");
}
} /// <summary>
/// 三星牌电视机,重写基类的抽象方法
/// </summary>
public class Samsung : TV
{
public override void On()
{
Console.WriteLine("三星牌电视机已经打开了");
} public override void Off()
{
Console.WriteLine("三星牌电视机已经关掉了");
} public override void tuneChannel()
{
Console.WriteLine("三星牌电视机换频道");
}
}
}
8.桥接模式(Bridge Pattern)的更多相关文章
- 桥接模式(Bridge Pattern)
1,定义 桥接模式(Bridge Pattern),也称为桥梁模式,其用意是将抽象化与实现化脱耦,使得两者可以独立的变化,它可以使软件系统沿着多个方向进行变化,而又不引入额外的复杂 ...
- 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)
原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- 二十四种设计模式:桥接模式(Bridge Pattern)
桥接模式(Bridge Pattern) 介绍将抽象部分与它的实现部分分离,使它们都可以独立地变化. 示例有一个Message实体类,对它的操作有Insert()和Get()方法,现在使这些操作的抽象 ...
- python 设计模式之桥接模式 Bridge Pattern
#写在前面 前面写了那么设计模式了,有没有觉得有些模式之间很类似,甚至感觉作用重叠了,模式并不是完全隔离和独立的,有的模式内部其实用到了其他模式的技术,但是又有自己的创新点,如果一味地认为每个模式都是 ...
- Net设计模式实例之桥接模式( Bridge Pattern)
一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...
- 转:设计模式-----桥接模式(Bridge Pattern)
转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html 记得看原始链接的评论. 学习设计模式也有一段时间了,今天就把我整理 ...
- 七个结构模式之桥接模式(Bridge Pattern)
问题: 当存在多个独立的变化维度时,如果仍采用多层继承结构,会急剧的增加类的个数,因此可以考虑将各个维度分类,使他们不相互影响. 定义: 将抽象部分与它的实现部分进行分离,抽象部分只保留最为本质的部分 ...
- C#设计模式——桥接模式(Bridge Pattern)
一.概述在软件开发中,我们有时候会遇上一个对象具有多个变化维度.比如对汽车对象来说,可能存在不同的汽车类型,如公共汽车.轿车等,也可能存在不同的发动机,如汽油发动机.柴油发动机等.对这类对象,可应用桥 ...
- php桥接模式(bridge pattern)
有点通了 <?php /* The bridge pattern is used when we want to decouple a class or abstraction from its ...
随机推荐
- JavaScript input file上传前获取文件名、文件类型、文件大小等信息
document.getElementById("productImgInput").files[0].type document.getElementById("pro ...
- 自定义NSLog无时间
#define SXLog(FORMAT, ...) fprintf(stderr,"file --\t%s\nline --\t%d\nmethd --\t%s\noutput --\t\ ...
- SQL防注入程序
1.在Global.asax.cs中写入: protected void Application_BeginRequest(Object sender,EventArgs e){ SqlIn ...
- 爬虫的自我解剖(抓取网页HtmlUnit)
网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源`HtmlUnit`包,4行代码就OK啦,例子如下: final WebClient webClient=new ...
- whereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- angularjs中关于当前路由再次点击强制刷新
angularjs中,有一个机制,就是当前路由再次点击的时候,不会再刷新页面,这实在是愁坏了包子,因为业务人员要求一定要刷新,于是我各种百度,然并卵....呜呜呜~~~~~ 于是乎,我就想到写指令了, ...
- Mysql性能监控
show processlist; show global variables like 'max_allowed_packet'; // QPS计算(每秒查询数)show global status ...
- Android 遍历界面控件
//遍历界面上的控件 fubin.pan LinearLayout sLinerLayout = (LinearLayout)findViewById(R.id.layout_scr); for (i ...
- UIView 注意问题
1. UIView.userInteractionEnabled UIView.userInteractionEnabled默认值是YES http://blog.csdn.net/studyreco ...
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form