C# 桥接模式
发布日期:2021-05-07 21:45:10 浏览次数:19 分类:技术文章

本文共 3229 字,大约阅读时间需要 10 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 桥接模式{    class Program    {        static void Main(string[] args)        {            //创建一个遥控器              RemoteControl remoteControl = new ConcreteRemote();            //长虹电视机              remoteControl.Implementor = new ChangHong();            remoteControl.On();            remoteControl.SetChannel();            remoteControl.Off();            Console.WriteLine();            //三星牌电视机              remoteControl.Implementor = new Samsung();            remoteControl.On();            remoteControl.SetChannel();            remoteControl.Off();            Console.ReadKey();        }        ///           /// 电视机,提供抽象方法          ///           public abstract class TV        {            ///             /// 开电视            ///             public abstract void On();            ///             /// 关电视            ///             public abstract void Off();            ///             /// 换频道            ///             public abstract void tuneChannel();        }        ///           /// 抽象概念中的遥控器,扮演抽象化角色          ///           public class RemoteControl        {            private TV implementor;            public TV Implementor            {                get { return implementor; }                set { implementor = value; }            }            ///               /// 开电视机,这里抽象类中不再提供实现了,而是调用了实现类中的实现              ///               public virtual void On()            {                implementor.On();            }            ///               /// 关电视机              ///               public virtual void Off()            {                implementor.Off();            }            ///               /// 换频道              ///               public virtual void SetChannel()            {                implementor.tuneChannel();            }        }        ///           /// 具体遥控器          ///           public class ConcreteRemote : RemoteControl        {            public override void SetChannel()            {                //Console.WriteLine("-----------");                //base.SetChannel();                //Console.WriteLine("-----------");                Console.WriteLine("具体遥控器 换频道");                base.SetChannel();            }        }        ///           /// 长虹电视机,重写基类的抽象方法          /// 提供具体的实现          ///           public class ChangHong : TV        {            public override void Off()            {                Console.WriteLine("长虹牌电视机已经关掉了");            }            public override void On()            {                Console.WriteLine("长虹牌电视机已经打开了");            }            public override void tuneChannel()            {                Console.WriteLine("长虹牌电视机换频道");            }        }        ///           /// 三星牌电视机,重写基类的抽象方法          ///           public class Samsung : TV        {            public override void On()            {                Console.WriteLine("三星牌电视机已经打开了");            }            public override void Off()            {                Console.WriteLine("三星牌电视机已经关掉了");            }            public override void tuneChannel()            {                Console.WriteLine("三星牌电视机换频道");            }        }    }}

运行:

上一篇:C# 状态模式
下一篇:C# 适配器模式

发表评论

最新留言

不错!
[***.144.177.141]2025年04月16日 11时27分25秒