7.桥接模式
来源:互联网 发布:费根鲍姆常数 知乎 编辑:程序博客网 时间:2024/05/17 06:24
介绍
在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?
核心要点
“将抽象部分与实现部分分离,使它们都可以独立的变化”
例子
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 桥接模式{ //抽象类 class Abstraction { protected Implementor _imolementor; public Implementor Implementor { set { _imolementor = value; } } public virtual void Operation() { if(_imolementor!=null) _imolementor.Operation(); } } //具体的抽象类 class RefinedAbstraction : Abstraction { public override void Operation() { _imolementor.Operation(); } } abstract class Implementor { public abstract void Operation(); } class ConcreateImplementorA : Implementor { public override void Operation() { Console.WriteLine("ConcreateImplementorA Operation"); } } class ConcreateImplementorB : Implementor { public override void Operation() { Console.WriteLine("ConcreateImplementorB Operation"); } }}//调用方法using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 桥接模式{ class Program { static void Main(string[] args) { Abstraction ab = new RefinedAbstraction(); ab.Implementor = new ConcreateImplementorA(); ab.Operation(); ab.Implementor = new ConcreateImplementorB(); ab.Operation(); Console.ReadKey(); } }}
在来个例子:电视遥控器
// 抽象概念中的遥控器,扮演抽象化角色 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("三星牌电视机换频道"); }}//调用方法using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 桥接模式{ 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(); } }}
阅读全文
0 0
- 7.桥接模式
- 7. 桥接模式
- 7.桥接模式
- 设计模式:适配器模式,桥接模式
- 设计模式----桥接模式
- 设计模式---桥接模式
- 结构性模式-桥接模式
- 设计模式:桥接模式
- 设计模式 桥接模式
- 设计模式 - 桥接模式
- 设计模式--桥接模式
- 设计模式--桥接模式
- 设计模式 - 桥接模式
- 设计模式-桥接模式
- 设计模式 - 桥接模式
- 设计模式 - 桥接模式
- 【设计模式-桥接模式】
- 设计模式:桥接模式
- 多线程练习程序 之 模拟火车票售票系统01版
- 《JAVA与模式》之单例模式
- PHP7新增功能
- asp.net GDI+ 绘制验证码 大小写字母数字混合
- android monkey命令学习与脚本制作
- 7.桥接模式
- 基于TQ2440的电子相册项目实现
- oracle中对数字类型的处理
- windows下 android studio 的NDK开发环境配置(1)(2)
- MySQL优化原理及优化经验
- gradle settings use local gradle(设置gradle版本为本地版本)
- TC Linker Error: Unable to open input file 'xxx.C(或.OBJ)'
- WinForm入门--高级控件(简)
- Android Studio导入aar文件并配置使用