首頁 >後端開發 >C#.Net教程 >C# 設計模式之橋接模式的程式碼詳解

C# 設計模式之橋接模式的程式碼詳解

黄舟
黄舟原創
2017-03-02 13:09:151241瀏覽

讓抽象與實作 獨立的變化

public abstract class Game
    {
        public Game(string name)
        {
            this.Name = name;
        }


        public Play m_play { get; set; }
        public string Name { get; set; }


        public virtual void PlayForFun()
        {
            this.m_play.PlayIt(this.Name);
        }
    }
    public class War3Game : Game
    {
        public War3Game(string name) : base(name) { 
        
        }
        public override void PlayForFun()
        {
            base.PlayForFun();
        }
    }
    public class KofGame : Game
    {
        public KofGame(string name)
            : base(name)
        { 
        
        }
        public override void PlayForFun()
        {
            base.PlayForFun();
        }
    }
    public class CsGame : Game
    {
        public CsGame(string name)
            : base(name)
        {
        
        }
        public override void PlayForFun()
        {
            base.PlayForFun();
        }
    }


    public abstract class Play
    {
        public virtual void PlayIt(string gameName) { }
    }
    public class CpuPlay : Play
    {
        public override void PlayIt(string gameName)
        {
            Console.WriteLine("play" + gameName + " game on computer");
        }
    }
    public class IPadPlay : Play
    {
        public override void PlayIt(string gameName)
        {
            Console.WriteLine("play" + gameName + " game on ipad");
        }
    }
    public class IPhonePlay : Play
    {
        public override void PlayIt(string gameName)
        {
            Console.WriteLine("play " + gameName + " game on iphone");
        }
    }

呼叫:

  Game g = new KofGame("kof97");
            g.m_play = new IPadPlay();
            g.PlayForFun();

 以上是C#  設計模式之橋接模式的程式碼詳解的內容,更相關內容請關注PHP中文網(www.php.cn)!


#
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn