桥接模式

 

在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度?这就要使用Bridge模式。

将抽象部分与实现部分分离,使它们都可以独立的变化

using System;
using System.Web;

public partial class DesignPattern_Bridge : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Window w = new Window();
        CS cs = new CS(w);
        cs.GameName = "反恐";
        cs.RunGame();
        Linux l = new Linux();
        War3 war = new War3(l);
        war.GameName = "魔兽";
        war.RunGame();
        war = new War3(w);
        war.RunGame();
    }
}


//设计一个游戏,,他可能在不同的平台上运行
public abstract class Game
{
    public RunPlatForm PlatForm;
    public abstract void RunGame();
    public string GameName;
    public Game(RunPlatForm platForm)
    {
        this.PlatForm = platForm;
    }
}

public class CS : Game
{

    public CS(RunPlatForm platForm): base(platForm)
    {
       
    }
    public override void RunGame()
    {
        HttpContext.Current.Response.Write(GameName+"运行在"+this.PlatForm.PlatForm() + "平台下...");
    }
}

public class War3 : Game
{
    public War3(RunPlatForm platForm)
        : base(platForm)
    {
       
    }
    public override void RunGame()
    {
        HttpContext.Current.Response.Write(GameName + "运行在" + this.PlatForm.PlatForm() + "平台下...");
    }
}

public abstract class RunPlatForm
{
    public abstract string  PlatForm();
}

public  class Window:RunPlatForm
{
    public Window()
    {
    }
    public override string  PlatForm()
    {
        return "Window";
    }
}

public  class Linux : RunPlatForm
{
    public Linux()
    {   
    }
    public override string  PlatForm()
    {
        return "Linux";
    }
}

posted @ 2008-10-24 16:46  游侠_1  阅读(164)  评论(0编辑  收藏  举报