C# 设计模式之桥接模式的代码详解

黄舟
Lepaskan: 2017-03-02 13:09:15
asal
1227 orang telah melayarinya

让抽象和实现  独立的变化

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");
        }
    }
Salin selepas log masuk

调用:

  Game g = new KofGame("kof97");
            g.m_play = new IPadPlay();
            g.PlayForFun();
Salin selepas log masuk

 以上就是C#  设计模式之桥接模式的代码详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan