抽象化と実装に独立した変更を加えましょう
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"); } }
Call:
Game g = new KofGame("kof97"); g.m_play = new IPadPlay(); g.PlayForFun();
上記は、C# デザイン パターンのブリッジ モードの詳細なコード説明です。関連コンテンツの詳細については、PHP 中国語 Web サイト (www. php.cn)!