繁体   English   中英

Bob叔叔的敏捷原理书中给出的接口隔离原理示例的实现

[英]Implementation of Interface Segregation Principle Example given in Uncle Bob's Agile Principles Book

我对图12-1的实现正确吗? 图12-1

这是Bob叔叔的书《 C#中的敏捷原理模式和实践》中接口污染示例的实现。

我尝试实现它如下:

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();
        }
    }
}

我的疑问是书中描述实现的方式,其中Door有时被称为类或有时被称为接口,而我是否需要除IDoor接口之外还需要单独的Door类实现而感到困惑? 同样,作者在命名接口时不使用I表示法,这使它更加混乱。

如果有人读过这本书,我希望可以理解我的关注并为此提供帮助。

注意:这本书也可以在线阅读。 讨论位于第215页 。https://druss.co/wp-content/uploads/2013/10/Agile-Principles-Patterns-and-Practices-in-C.pdf

我相信他正在使用UML类图表示法,因此此图似乎适用:

在此处输入图片说明

链接

如果我没看错, TimedDoor应该继承 Door 但是在您的示例中, TimedDoor 实现了 IDoor 这与图表不一致。

声明应为:

class TimedDoor : Door

我认为您不需要IDoor 该示例的观点是, Door必须实现ITimerClient才能使Timer对其执行操作。 ITimerClient应该公开一个公共成员Timeout() 大概是当计时器调用此方法时,如果门是定时门,则门应自行解锁。 默认行为(例如,对于非定时门)可能是无操作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM