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#实现图形界面的时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C#实现listview Group收缩扩展的方法

    C#实现listview Group收缩扩展的方法

    这篇文章主要介绍了C#实现listview Group收缩扩展的方法,结合实例形式分析了listview控件的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-03-03
  • Unity实现物体运动轨迹的绘制

    Unity实现物体运动轨迹的绘制

    这篇文章主要为大家详细介绍了Unity实现物体运动轨迹的绘制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • C#网页信息采集方法汇总

    C#网页信息采集方法汇总

    这篇文章主要介绍了C#网页信息采集方法,实例汇总了三种常用的方法,是非常实用的技巧,需要的朋友可以参考下
    2014-10-10
  • C#实现一个简单实用的TXT文本操作及日志框架详解

    C#实现一个简单实用的TXT文本操作及日志框架详解

    这篇文章主要给大家介绍了关于利用C#如何实现一个简单实用的TXT文本操作及日志框架的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们一起来看看吧
    2018-07-07
  • C#使用iTextSharp封装的PDF文件操作类实例

    C#使用iTextSharp封装的PDF文件操作类实例

    这篇文章主要介绍了C#使用iTextSharp封装的PDF文件操作类,实例分析了C#操作pdf文件的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • httpwebreqeust读取httponly的cookie方法

    httpwebreqeust读取httponly的cookie方法

    下面小编就为大家带来一篇httpwebreqeust读取httponly的cookie方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • C#与js实现去除textbox文本框里面重复记录的方法

    C#与js实现去除textbox文本框里面重复记录的方法

    这篇文章主要介绍了C#与js实现去除textbox文本框里面重复记录的方法,很实用的功能,需要的朋友可以参考下
    2014-08-08
  • 浅析JAVA中过滤器、监听器、拦截器的区别

    浅析JAVA中过滤器、监听器、拦截器的区别

    本文通过代码分析和文字说明的方式给大家浅析JAVA中过滤器、监听器、拦截器的区别,感兴趣的朋友一起看下吧
    2015-09-09
  • C#访问PostGreSQL数据库的方法

    C#访问PostGreSQL数据库的方法

    这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析。
    2013-04-04

最新评论