简体   繁体   English

难以理解通过接口的松耦合

[英]Difficulty in understanding Loose coupling via Interfaces

In "Head First Servlets and Jsp" book by Kathy sierra at page 744 在Kathy sierra的“ Head First Servlets and Jsp”一书中,第744页

it is mentioned that, "A very common approach when class A wants to use methods in class B is to create an interface between the two. Once class B implements this interface, class A can use class B via the interface." 提到“当类A要使用类B中的方法时,一种非常常见的方法是在两者之间创建接口。一旦类B实现了该接口,类A便可以通过该接口使用类B。”

My question is how class A can use class B because they might have same method signatures by implementing the same interface but the implementation of those methods would be different? 我的问题是,类A如何可以使用类B,因为通过实现相同的接口它们可能具有相同的方法签名,但是这些方法的实现会有所不同? Can someone please explain this? 有人可以解释一下吗?

I think there is a slight misunderstanding: A and B would not both implement the interface, let's call it C. Only B would implement C. The trick is that now A can eliminate all direct references to B and just use C, provided there's a way A can somehow get hold of an object implementing C, eg via a factory. 我认为有一点误解:A和B不会同时实现该接口,我们将其称为C。只有B会实现C。诀窍是,现在A可以消除对B的所有直接引用,而只需使用C,只要有一个A可以通过某种方式(例如通过工厂)获得实现C的对象。 This way you could replace B with a totally different implementation of C without breaking the code in A. 这样,您可以用完全不同的C实现替换B而不破坏A中的代码。

An interface is a group of related methods with empty bodies. 接口是一组具有空主体的相关方法。

It is more like a contract. 它更像是一份合同。 When you have a Television Set, the Buttons act as an interface to switch it on and switch it off. 当您拥有电视机时,这些按钮充当将其打开和关闭的界面。 That is a contract between you and the television that you would be using those interfaces to get the maximum benefit of the Television. 那是您与电视之间的合同,您将使用这些接口来获得电视的最大利益。

For example a bicycle's behavior, if specified as an interface, might appear as follows: 例如,如果将自行车的行为指定为接口,则可能显示如下:

interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration: 要实现此接口,您的类的名称将更改(以特定品牌的自行车为例,例如ACMEBicycle),并且您可以在类声明中使用Implements关键字:

class ACMEBicycle implements Bicycle {

    // remainder of this class 
    // implemented as before
}

Hope this helps. 希望这可以帮助。

Loose coupling indicates a dependency, but not an explicit reference. 松散耦合表示依赖关系,但不是显式引用。 Interface is a mechanism to enable loose coupling by providing member declarations independently of a) implementation and b) inheritance tree. 接口是一种通过提供独立于a)实现和b)继承树的成员声明来实现松耦合的机制。

Take ClassA for example. ClassA为例。 ClassA implements interface IService . ClassA实现接口IService MyMethod(ClassA input) has a dependency on ClassA . MyMethod(ClassA input)ClassA有依赖性。

MyMethod(IService input) is dependent on IService but not on ClassA . MyMethod(IService input)依赖于IService但不依赖于ClassA MyMethod(IService) will compile without ClassA - it is loosely coupled. MyMethod(IService)将在没有ClassA情况下进行编译-松散耦合。

In conclusion. 结论。 Loose coupling allows two components to participate in a mechanism but it does not make an explicit reference between them. 松耦合允许两个组件参与一种机制,但是它们之间没有明确引用。

Some sample code (C# but should be understandable if you know Java): 一些示例代码(C#,但如果您知道Java,应该可以理解):

// this interface defines what A should be able to do with B
interface SomeInterface
{
    int GetSomeValue();
}


// B needs to implement this interface
class B : SomeInterface
{
    int GetSomeValue()
    {
        return 42;
    }

    void SomeOtherMethod()
    {

    }
}

// A has access to B via the interface. Pass it in the constructor and save it for later use
class A
{
    A(SomeInterface si)
    {
        this.si = si;
    }
    SomeInterface si;

    // or pass it per call
    void SomeMethodInA(SomeInterface passedIF)
    {
        int c = passedIF.GetSomeValue();
    }

    // may even have the same name but doesn't have to!
    int GetSomeValue()
    {
        // access "B" (or some other class implementing this interface) via the interface
        return si.GetSomeValue() + 1;
    }


    // The interface can of course be also a property of A.
    // but this is left as an exercise to the reader

}


int main()
{
    B b = new B();
    A a = new A(b);
    a.GetSomeValue();
    a.SomeMethodInA(b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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