依赖倒置原则
发布日期:2021-05-14 04:38:44 浏览次数:13 分类:博客文章

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

������

������������������������������������������������������������������������������

���������������������������������������������������������������������������������

������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������

������������������������������������������������������������������

using System;namespace DependencyInversionDaemon{    class Light    {        public void TurnOn()        {            Console.WriteLine("The light is on.");        }        public void TurnOff()        {            Console.WriteLine("The light is off.");        }    }}
Light.cs������
namespace DependencyInversionDaemon{    class ButtonEx    {        private Light light = new Light();        private bool pressed = false;        public void Press()        {            if (pressed)                light.TurnOn();            else                light.TurnOff();            pressed = !pressed;        }    }}
ButtonEx.cs������

Light������ButtonEx������������������������������������������������������������������������������ButtonEx������������������������������Light������������������ButtonEx������������������������������Light���������������������������������

���������������������������ButtonEx������������Light������������Light������������������������������ButtonEx������������������������������������������������������������������������������������������������������

������

������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������ButtonEx������Light���������������������������������������������������������������������������������Light������������������������������������

���������������������������������������Switchable������������������������ButtonEx���������������������������������������������������������������������������������������������TurnOn���TurnOff���������ButtonEx������Light������������������������������������������������������Switchable������������������������������

namespace DependencyInversionDaemons{    interface Switchable    {        void TurnOn();        void TurnOff();    }}
Switchable.cs������
using System;namespace DependencyInversionDaemons{    class Light:Switchable    {        public void TurnOn()        {            Console.WriteLine("The light is on.");        }        public void TurnOff()        {            Console.WriteLine("The light is off.");        }    }}
Light.cs������
using System;namespace DependencyInversionDaemons{    class Fridge:Switchable    {        public void TurnOn()        {            Console.WriteLine("The fridge is on.");        }        public void TurnOff()        {            Console.WriteLine("The fridge is off.");        }    }}
Fridge.cs������
namespace DependencyInversionDaemons{    class ButtonEx    {        private Switchable swithableObjects;        private bool pressed = false;        public void SetSwitchable(Switchable switchable)        {            this.swithableObjects = switchable;        }        public void Press()        {            if (pressed)                swithableObjects.TurnOn();            else                swithableObjects.TurnOff();            pressed = !pressed;        }    }}
ButtonEx.cs������
using System;namespace DependencyInversionDaemons{    class Program    {        static void Main(string[] args)        {            ButtonEx button = new ButtonEx();            button.SetSwitchable(new Fridge());            button.Press();            button.Press();            Console.ReadKey();         }    }}
������������

���������������������������������������������������������������������������������������ButtonEx������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������������������

上一篇:接口分离原则
下一篇:访问 IIS 元数据库失败 的解决方法

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月18日 08时18分17秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章