
本文共 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."); } }}
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; } }}
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(); }}
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."); } }}
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."); } }}
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; } }}
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������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
