首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bob叔叔敏捷原理书中接口隔离原理实例的实现

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

Stack Overflow用户
提问于 2019-06-20 06:22:36
回答 1查看 175关注 0票数 0

图12-1的实现正确吗?

这是Bob叔叔的书“C#中的敏捷原则、模式和实践”中接口污染示例的实现。

我曾尝试实施以下措施:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2019-06-20 07:14:13

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

链接

如果我正确地阅读了这篇文章,TimedDoor应该继承Door。但是在您的示例中,TimedDoor实现了IDoor。这与图表不一致。

宣言应包括:

代码语言:javascript
复制
class TimedDoor : Door

我不认为你需要一个IDoor。示例中的germaine是Door必须实现ITimerClient才能让Timer在其上执行操作。ITimerClient应该公开一个公共成员,Timeout()。推测当计时器调用此方法时,如果门是定时的门,则门应该自己解锁。默认行为(例如,对于非定时的门)可能是不操作的。

代码语言:javascript
复制
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.
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56680104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档