C#桥接模式完整实例

本文实例讲述了C#桥接模式实现方法。分享给大家供大家参考。具体如下:

C#代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandAddressList:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机通讯录");
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandBrandM:HandSetBrand
  {
    public override void Run()
    {
      soft.Run();
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandBrandN:HandSetBrand
  {
    public override void Run()
    {
      soft.Run();
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
 public abstract class HandSetBrand
  {
    protected HandSetSoft soft;
    public void SetHandSetSoft(HandSetSoft soft)
    {
      this.soft = soft;
    }
    public abstract void Run();
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandSetGame:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机游戏");
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandSetMP3:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机MP3");
    }
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
 public abstract class HandSetSoft
  {
    public abstract void Run();
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      HandSetBrand ab;
      ab = new HandBrandN();
      ab.SetHandSetSoft(new HandSetGame());
      ab.Run();
      ab.SetHandSetSoft(new HandAddressList());
      ab.Run();
      ab = new HandBrandM();
      ab.SetHandSetSoft(new HandSetGame());
      ab.Run();
      ab.SetHandSetSoft(new HandAddressList());
      ab.Run();
      ab.SetHandSetSoft(new HandSetMP3());
      ab.Run();
      Console.ReadKey();
    }
  }
}

HandAddressList.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandAddressList:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机通讯录");
    }
  }
}

HandBrandM.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandBrandM:HandSetBrand
  {
    public override void Run()
    {
      soft.Run();
    }
  }
}

HandBrandN.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandBrandN:HandSetBrand
  {
    public override void Run()
    {
      soft.Run();
    }
  }
}

HandSetBrand.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
 public abstract class HandSetBrand
  {
    protected HandSetSoft soft;
    public void SetHandSetSoft(HandSetSoft soft)
    {
      this.soft = soft;
    }
    public abstract void Run();
  }
}

HandSetGame.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandSetGame:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机游戏");
    }
  }
}

HandSetMP3.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  public class HandSetMP3:HandSetSoft
  {
    public override void Run()
    {
      Console.WriteLine("运行手机MP3");
    }
  }
}

HandSetSoft.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
 public abstract class HandSetSoft
  {
    public abstract void Run();
  }
}

Program.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      HandSetBrand ab;
      ab = new HandBrandN();
      ab.SetHandSetSoft(new HandSetGame());
      ab.Run();
      ab.SetHandSetSoft(new HandAddressList());
      ab.Run();
      ab = new HandBrandM();
      ab.SetHandSetSoft(new HandSetGame());
      ab.Run();
      ab.SetHandSetSoft(new HandAddressList());
      ab.Run();
      ab.SetHandSetSoft(new HandSetMP3());
      ab.Run();
      Console.ReadKey();
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • 用代码和UML图化解设计模式之桥接模式的深入分析

    桥接模式就是 独立和实现相分离.不同的厂家生产不同的产品....产品和厂家有这组合的关系. 上代码 复制代码 代码如下: // Bridge.cpp : 定义控制台应用程序的入口点./************************************************************************/   #include "stdafx.h"#include <iostream>using namespace std;class Product

  • 简单了解C#设计模式编程中的桥接模式

    桥接模式的概念 定义:将抽象部分与实现部分分离,使它们都可以独立的变化. 理解:为啦解决一个对象变化而影响多个对象跟着变化,需要把具体实现对象抽象化,使降低对象和变化因素的耦合度,提高系统的可维护性和扩展性. 举例: 手机系统的生态圈问题: 啰嗦点:众所周知wp的生态圈相对与有些系统较差,各位需努力,诺基亚走下神坛,wp要走上神坛,顶一下哈. wp/ios系统类:运行软件,可承载本运行环境下的任何软件,如果新增一个系统,软件就要多做一个系统的版本 weixin/kuwo软件类:开始运行软件,如果

  • C++设计模式之桥接模式

    问题描述 现在要去画一个图形,图形有长方形.圆形和扇形等等:而图形又可以加上不同的颜色,然后,我们就可以画出红色的长方形,绿色的长方形:红色的圆形,绿色的圆形等等.而这种图形的形状在变化,图形的颜色也在变化,当使用代码去实现时,如何面对这种多方面的变化呢?这就要说到今天的桥接模式了. 什么是桥接模式? 对于上述的图形与颜色的问题时,很多时候,我们让各个图形类继承颜色类,比如: 复制代码 代码如下: class CShape { }; class CRectangle : public CShap

  • 深入解析C#设计模式中对桥接模式的具体运用

    这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是--把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视品牌的遥控器都继承这个抽象类,具体设计类图如下: 这样的实现使得每部不同型号的电视都有自己遥控器实现,这样的设计对于电视机的改变可以很好地应对,只需要添加一个派生类就搞定了,但随着时间的推移,用户需要改变遥控器的功能,如:用户可能后面需要对遥控器添加返回上一个台等功能时,此时上面的设计就需要修改抽象类Remo

  • 深入解析iOS应用开发中对设计模式中的桥接模式的使用

    引言 在项目开发中,我们会遇到这样的一种场景:某些类型由于自身的逻辑,往往具有两个或多个维度的变化,比如说大话设计模式书中所说的手机,它有两个变化的维度:一是手机的品牌,可能有三星.苹果等:二是手机上的软件,可能有QQ.微信等.如何应对这种"多维度的变化"?怎样利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就是本章桥接模式所要解决的问题. 何为桥接模式? 桥接模式的目的是把抽象层次结构从其实现中分离出来,使其能够独立变更.抽象层定义了供客户端使

  • 举例讲解Python中的Null模式与桥接模式编程

    Null模式 我想每个人都有一种经历,为了获取某属性,但是有时候属性是None,那么需要你做异常处理, 而假如你想节省这样的条件过滤的代码,可以使用Null模式以减少对象是否为None的判断 python的例子 我举个不是很通用的例子,只是为了让大家理解这个模式:我有很多类, 但是不是每个类都有类方法test,所以我调用类方法就要做个异常处理,类似这样 class A(object): pass class B(object): b = 1 @classmethod def test(cls):

  • php设计模式 Bridge (桥接模式)

    复制代码 代码如下: <?php /** * 桥接模式 * * 将抽象部份与它实现部分分离,使用它们都可以有独立的变化 */ abstract class Implementor { abstract public function operation(); } class ConcreteImplementorA extends Implementor { public function operation() { echo "ConcreteImplementorA Operation

  • c#桥接模式(bridge结构模式)用法实例

    本文实例讲述了c#桥接模式(bridge结构模式)用法.分享给大家供大家参考.具体分析如下: 一般来说,Bridge模式使用"对象间的组合关系"解耦了抽象和实现之间固有的绑定关系,使得抽象(Tank的型号)和实现(不同的平台)可以沿着格子的维度来变化.所谓抽象和实现沿着各自维度的变化,即"子类化"它们(比如不同的Tank型号子类,和不同的平台子类),得到各个子类之后,便可以任意组合它们,从而获得不同平台上的不同型号. 在前面的玩家中每增加一个行为,就必须在每个玩家中

  • 轻松掌握Java桥接模式

    定义:将抽象部分与实现部分分离,使它们都可以独立的变化. 特点:桥接模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任.它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展. 企业级开发和常用框架中的应用:多继承的结构 只是从定义理解桥接模式,很难理清它的作用,下面举个例子来说明: 电脑城卖电脑,电脑种类分了平板电脑,笔记本电脑,台式电脑:电脑品牌又分了联想,戴尔,华硕

  • 深入理解JavaScript系列(44):设计模式之桥接模式详解

    介绍 桥接模式(Bridge)将抽象部分与它的实现部分分离,使它们都可以独立地变化. 正文 桥接模式最常用在事件监控上,先看一段代码: 复制代码 代码如下: addEvent(element, 'click', getBeerById); function getBeerById(e) { var id = this.id; asyncRequest('GET', 'beer.uri?id=' + id, function(resp) { // Callback response. consol

随机推荐