[英]Implementation of Interface Segregation Principle Example given in Uncle Bob's Agile Principles Book
Is my implementation of Figure 12-1 correct? 我对图12-1的实现正确吗?
This is an implementation of Interface Pollution example from Uncle Bob's book Agile Principles Patterns and Practices in C#. 这是Bob叔叔的书《 C#中的敏捷原理模式和实践》中接口污染示例的实现。
I have tried implementing it as following:- 我尝试实现它如下:
using System;
namespace AgilePrinciplesCSharp.Chapter12.Listing12_2
{
class Listing12_2
{
static void Main(string[] args)
{
var door = new TimedDoor();
var client = new Client(door);
client.TimeOut();
}
}
public interface ITimerClient
{
void TimeOut();
}
// We force Door, and therefore (indirectly) TimedDoor, to inherit from TimerClient.
public interface IDoor : ITimerClient
{
void Lock();
void Unlock();
bool IsDoorOpen();
}
// Implementation of Door Interface with Timer functionality
public class TimedDoor : IDoor
{
public bool IsDoorOpen()
{
return true;
}
public void Lock()
{
Console.WriteLine("Door Locked");
}
public void TimeOut()
{
var timer = new Timer();
timer.Register(5, this);
Console.WriteLine("Timeout! Door left open for so long");
}
public void Unlock()
{
Console.WriteLine("Door Unlocked");
}
}
// Timer class can use an object of TimerClient
public class Timer
{
public void Register(int timeout, ITimerClient client)
{
/* CODE */
}
}
// Client uses Door Interface without depending upon any particular implementation of Door
public class Client
{
IDoor door;
public Client(IDoor door)
{
this.door = door;
}
public void TimeOut()
{
door.TimeOut();
}
}
}
My doubt is in the way the implementation is described in the book, where Door is sometimes being called as a class or sometimes as an interface and i am getting confused whether i need to have a separate implementation of Door class apart from IDoor interface? 我的疑问是书中描述实现的方式,其中Door有时被称为类或有时被称为接口,而我是否需要除IDoor接口之外还需要单独的Door类实现而感到困惑? Also the author does not use I notation while naming an interface which makes it even more confusing. 同样,作者在命名接口时不使用I表示法,这使它更加混乱。
If anyone has read the book, i hope can understand my concern and help me out with this. 如果有人读过这本书,我希望可以理解我的关注并为此提供帮助。
NOTE: This Book can also be read online. 注意:这本书也可以在线阅读。 This discussion is at page 215. https://druss.co/wp-content/uploads/2013/10/Agile-Principles-Patterns-and-Practices-in-C.pdf 讨论位于第215页 。https://druss.co/wp-content/uploads/2013/10/Agile-Principles-Patterns-and-Practices-in-C.pdf
I believe he is using UML class diagram notation, so this diagram would seem to apply: 我相信他正在使用UML类图表示法,因此此图似乎适用:
If I read this correctly, TimedDoor
should inherit from Door
. 如果我没看错, TimedDoor
应该继承 Door
。 But in your example, TimedDoor
implements IDoor
. 但是在您的示例中, TimedDoor
实现了 IDoor
。 That is not consistent with the diagram. 这与图表不一致。
The declaration should be: 声明应为:
class TimedDoor : Door
I don't think you need an IDoor
. 我认为您不需要IDoor
。 What is germaine to the example is that Door
must implement ITimerClient
in order for a Timer
to perform operations on it. 该示例的观点是, Door
必须实现ITimerClient
才能使Timer
对其执行操作。 ITimerClient
should expose a single public member, Timeout()
. ITimerClient
应该公开一个公共成员Timeout()
。 Presumably when the timer calls this method, the door should unlock itself, if it is a timed door. 大概是当计时器调用此方法时,如果门是定时门,则门应自行解锁。 The default behavior (eg for a non-timed door) is probably no-operation. 默认行为(例如,对于非定时门)可能是无操作。
interface ITimerClient
{
void Timeout();
}
class Door : ITimerClient
{
public virtual void Timeout()
{
//No operation; this isn't a timed door
}
//etc.
}
class TimedDoor : Door
{
protected bool locked = true;
public override void Timeout()
{
this.Unlock(); //Override default behavior because this type of door is timed
base.Timeout(); //Optional, sometimes recommended
}
public virtual void Unlock()
{
this.locked = false;
}
//etc.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.