桥梁模式 Bridge Patter
来源:互联网 发布:农产品直销网络 编辑:程序博客网 时间:2024/05/17 06:22
IElectricalEquipment.cs
// Interface // Implementor 实现类接口// 电器共性,抽象处理public interface IElectricalEquipment{ void PowerOn(); // Each electrical equipment can be turned on void PowerOff(); // Each electrical equipment can be turned off}// 接口只包含方法、属性、事件或索引器的签名。// 实现接口的类或结构必须实现接口定义中指定的接口成员。
Fan.cs
// ConcreteImplementor 具体实现类// 电扇类public class Fan : IElectricalEquipment{ public void PowerOn() { Console.WriteLine("Fan is on"); } public void PowerOff() { Console.WriteLine("Fan is off"); }}
Light.cs
// ConcreteImplementor 具体实现类// 电灯类public class Light : IElectricalEquipment{ public void PowerOn() { Console.WriteLine("Light is on"); } public void PowerOff() { Console.WriteLine("Light is off"); }}
Switch.cs
// Abstraction// Switch 基类// 电器开关public class Switch{ // Switch拥有IElectricalEquipment被封装的实例 public IElectricalEquipment equipment { get; set; } public void On() { // Switch has an on button Console.WriteLine("Switch on the equipment"); equipment.PowerOn(); } public void Off() { // Switch has an off button Console.WriteLine("Switch off the equipment"); equipment.PowerOff(); }}
NormalSwitch.cs
// RefinedAbstraction 扩充抽象类// 普通类型的开关public class NormalSwitch : Switch{}
FancySwitch.cs
// RefinedAbstraction 扩充抽象类// 特定类型的开关public class FancySwitch : Switch{}
Main.cs
static void Main(string[] args){ // We have some electrical equipments, say Fan, Light etc. // So, lets create them first. IElectricalEquipment fan = new Fan(); IElectricalEquipment light = new Light(); // We also have some switches. Lets create them too. Switch fancySwitch = new FancySwitch(); Switch normalSwitch = new NormalSwitch(); // Lets connect the Fan to the fancy switch fancySwitch.equipment = fan; // As the switch now has an equipment (Fan), // so switching on or off should // turn on or off the electrical equipment fancySwitch.On(); // It should turn on the Fan. // so, inside the On() method of Switch, // we must turn on the electrical equipment. // It should turn off the Fan. So, inside the On() method of fancySwitch.Off(); // Switch, we must turn off the electrical equipment // Now, lets plug the light to the fancy switch fancySwitch.equipment = light; fancySwitch.On(); // It should turn on the Light now fancySwitch.Off(); // It should be turn off the Light now normalSwitch .equipment = light; normalSwitch.On(); // It should turn on the Light now normalSwitch.Off(); // It should be turn off the Light now}
源文:我给媳妇解释设计模式:第一部分
阅读全文
0 0
- 桥梁模式 Bridge Patter
- 桥梁(Bridge)模式
- 桥梁(Bridge)模式
- 桥梁(Bridge)模式
- 桥梁模式【Bridge Pattern】
- 桥梁模式(Bridge)
- 桥梁(bridge)模式
- 桥梁模式 ----- bridge
- 桥梁模式/Bridge
- 桥梁模式 Bridge
- 桥梁(bridge)模式
- 桥梁模式【 Bridge Pattern】
- 桥梁模式Bridge
- 桥梁模式【Bridge Pattern】
- 一、 桥梁(Bridge)模式
- 11、桥梁模式(Bridge)
- design patter bridge
- Java与模式 桥梁模式Bridge Pattern
- Map对象深拷贝
- jvm 内存模型及实践
- C++实现树的基本操作,界面友好,操作方便,运行流畅,运用模板
- mui.openwindow的autoshow和anishow异常
- 在线选坐代码
- 桥梁模式 Bridge Patter
- codeforces 773A Success Rate 二分
- 三星Galaxy S8得到Daydream支持
- C语言实现数组快速排序(含对算法的详细解释)
- 一张图看透JavaScript原型关系:__proto__(对象原型)和prototype(函数原型)
- Windows7下安装Docker、下载镜像和运行OpenTsdb容器
- Hadoop的安装与配置
- POJ 1214 "Accordian" Patience 笔记 未完成
- 关于RHEL支持T10 PI的情况