C#桥接模式完整实例

 更新时间:2015年07月16日 12:05:57   作者:宋勇野  
这篇文章主要介绍了C#桥接模式,以实例形式较为详细的分析了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#程序设计有所帮助。

相关文章

  • C#实现在线点餐系统

    C#实现在线点餐系统

    这篇文章主要为大家详细介绍了C#实现在线点餐系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C#版Windows服务安装卸载小工具

    C#版Windows服务安装卸载小工具

    这篇文章主要为大家推荐了一款C#版Windows服务安装卸载小工具,小巧灵活的控制台程序,希望大家喜欢,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • c#不使用系统api实现可以指定区域屏幕截屏功能

    c#不使用系统api实现可以指定区域屏幕截屏功能

    这篇文章主要介绍了不使用系统API通过纯c#实现屏幕指定区域截屏功能,截屏后还可以保存图象文件,大家参考使用吧
    2014-01-01
  • C#基于Windows服务的聊天程序(1)

    C#基于Windows服务的聊天程序(1)

    这篇文章主要为大家详细介绍了C#基于Windows服务的聊天程序的第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • C#编写ActiveX网页截图控件

    C#编写ActiveX网页截图控件

    这篇文章主要介绍了C#编写ActiveX网页截图控件,作为学习C#编写ActiveX的一个简单入门教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • C#实现Check Password和锁定输错密码锁定账户功能

    C#实现Check Password和锁定输错密码锁定账户功能

    C#实现的Check Password,并根据输错密码的次数分情况锁定账户:如果输入错误3次,登录账户锁定5分钟并提示X点X分后重试登录,具体实现代码感兴趣的朋友跟随小编一起看看吧
    2020-01-01
  • C#贪吃蛇游戏实现分析

    C#贪吃蛇游戏实现分析

    这篇文章主要为大家分析了C#贪吃蛇游戏的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#实现HTML转WORD及WORD转PDF的方法

    C#实现HTML转WORD及WORD转PDF的方法

    这篇文章主要介绍了C#实现HTML转WORD及WORD转PDF的方法,涉及C#实现HTML、WORD及PDF等文件格式转换的相关技巧,需要的朋友可以参考下
    2015-09-09
  • C#对图片进行马赛克处理可控制模糊程度的实现代码

    C#对图片进行马赛克处理可控制模糊程度的实现代码

    本文通过实例代码给大家介绍了C#对图片进行马赛克处理可控制模糊程度的实现方法,代码超简单,具有一定的参考借鉴价值,感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-05-05
  • C# 面向对象三大特性:封装、继承、多态

    C# 面向对象三大特性:封装、继承、多态

    本文主要介绍了面向对象的三大特性:封装、继承、多态,提供了简单的示例供大家参考和理解。
    2016-03-03

最新评论