簡體   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