c# 桥接模式简单例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 桥接模式
{
    public abstract class TV
    {
        public abstract void On();
        public abstract void Off();
        public abstract void SetChannel(string strchannel);
    }

    public class ChangHong:TV
    {
        public override void On()
        {
            Console.WriteLine("长虹电视打开了!");
        }
        public override void Off()
        {
            Console.WriteLine("长虹电视关闭了!");
        }
        public override void SetChannel(string strchannel)
        {
            Console.WriteLine("长虹切换到了" + strchannel+"频道");
        }
    }
    public class Samsung: TV
    {
        public override void On()
        {
            Console.WriteLine("三星电视打开了!");
        }
        public override void Off()
        {
            Console.WriteLine("三星电视关闭了!");
        }
        public override void SetChannel(string strchannel)
        {
            Console.WriteLine("三星切换到了" + strchannel + "频道");
        }
    }

    public class RemoteControl
    {
        private TV tv;
        public TV Tv
        {
            get { return tv; }
            set { tv = value; }
        }
        public virtual void On()
        {
            tv.On();
        }
        public virtual void Off()
        {
            tv.Off();
        }
        public virtual void SetChannel(string strchannel)
        {
            tv.SetChannel(strchannel);
        }
    }
    public class ConcreteRemote:RemoteControl
    {
        public override void SetChannel(string strchannel)
        {
            Console.WriteLine("--------------------------");
            base.SetChannel(strchannel);
            Console.WriteLine("--------------------------");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            RemoteControl remotecontrol = new ConcreteRemote();
            remotecontrol.Tv = new ChangHong();
            remotecontrol.On();
            remotecontrol.Off();
            remotecontrol.SetChannel("中央一套");

            remotecontrol.Tv = new Samsung();
            remotecontrol.On();
            remotecontrol.Off();
            remotecontrol.SetChannel("中央二套");

            Console.ReadKey();
        }
    }
}
c# 桥接模式简单例子_第1张图片

你可能感兴趣的:(程序设计模式)