接口實現是否應該獨立 (Should Interface implementations be independent)


問題描述

接口實現是否應該獨立 (Should Interface implementations be independent)

I have come across some legacy code that has raised all my heckles as an  Object Oriented Programmer.

Here's the pattern used often: An interface has two implementations and one implementation calls a method of the other.

Now, I think it should be refactored so that the implementations do not know about each other. It is simple enough HOW to do it. What I cannot figure out clearly - & hoping good people of SO would help me with -  is WHY. 

I can see the theoratical reason - it is a terrible object-oriented design. But I am playing the devil's advocate here and asking - what is the practical disadvantage of two implementation having knowledge of each other. Why should time & money be spent to get rid of this (in my mind) anti-pattern?

Any info or links on this will be appreciated.


參考解法

方法 1:

  

I can see the theoratical reason - it is a terrible object-oriented design.

Why? It sounds entirely reasonable to me.

For example, suppose I want to decorate every call - e.g. to add statistics for how often a call has been made, or add some authorization check etc. It makes sense to keep that decoration separate from the real implementation, and just delegate:

public class DecoratedFoo : IFoo
{
    private readonly IFoo original;

    public DecoratedFoo(IFoo original)
    {
        this.original = original;
    } 

    public string Bar() // Defined in IFoo
    {
        // Update statistics here, or whatever
        return original.Bar();
    }
}

Why do you view that separation of concerns to be "terribly object-oriented design"? Even if the decorated class knows about a specific implementation of IFoo and calls members which aren't part of IFoo itself in order to make things more efficient, it doesn't seem particularly awful to me. It's just one class knowing about another, and they happen to implement the same interface. They're more tightly coupled than the example above which only knows about IFoo, but it's still not "terrible".

方法 2:

There is nothing wrong with an implementation1 of interface1 being aware of or interacting with implementation2 of interface1.

I think you have just spotted an intended or un intended implementation of proxy pattern  http://en.wikipedia.org/wiki/Proxy_pattern

Hope this helps :) 

方法 3:

That method of the "other implementation" that the first implementation calls is what I would call a library function.  Put it in a separate module/file/project/whatever (depends on your language/dev env) and have both implementations include it and use it from there.

There is absolutely nothing wrong with two implementations of some interfacing containing common code, but of course that common code should probably be separated from each implementation so that you can load either into your program without having to load the other.

方法 4:

My thoughts on this are 

  1. Suppose in the due course of time if you are retiring one implementation and you have kept that separately then there is no change in the other and you dont need to test that. If there is no separation you need to spend time in separating and testing the other implementation.

  2. Its always cleaner to have single responsibility.

(by OceanBlueJon SkeetBullMichael Sladeraddykrish)

參考文件

  1. Should Interface implementations be independent (CC BY-SA 3.0/4.0)

#ooad #OOP #design-patterns






相關問題

接口實現是否應該獨立 (Should Interface implementations be independent)

當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)

抽象VS信息隱藏VS封裝 (Abstraction VS Information Hiding VS Encapsulation)

應用程序中jQuery表單綁定代碼的最佳實踐 (Best practices with jQuery form binding code in an application)

耦合與內聚 (Coupling and cohesion)

什麼時候應該在C ++中使用類與結構? (When should you use a class vs a struct in C++?)

What is an anti-pattern? (What is an anti-pattern?)

如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)

協調 MVP 三元組 (Coordinating MVP triads)

裝飾器和虛擬方法 (Decorators and Virtual Methods)

設計和建築有什麼區別? (What is the difference between Design and Architecture?)

特定時間段有效的業務規則——如何有序管理 (Business rules that are valid for specific time span – how to manage in an orderly manner)







留言討論