欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#桥接模式完整实例

程序员文章站 2024-02-10 20:01:52
本文实例讲述了c#桥接模式实现方法。分享给大家供大家参考。具体如下: c#代码如下: using system; using system.collectio...

本文实例讲述了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#程序设计有所帮助。