接口依赖 [英] Interface Dependencies

查看:60
本文介绍了接口依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您创建一个接口并且知道您将依赖另一个接口时,您是否将构造函数作为接口的一部分?

When you create an interface and you know you will have a dependency on another, do you make the constructor part of the interface?

就我而言,我想创建

一个 IClientReceiveRecorder,我可以提供给客户端并收集所有网络流量以用于一个简短的测试会话.它可能只是包含一组字符串.

An IClientReceiveRecorder that I can give to a client and have all the network traffic collected for a short test session. It is probably just going to contain a collection of strings.

一个 IEvaluator,可以获取所有收到的消息并实现我们想要测试的各种内容.例如,我可能有一个具体的 Evaluator 来测试是否所有字符串都包含字母c".

An IEvaluator that can get all the messages received and implement various things we want to test for. For example, I might have a concrete Evaluator that tests whether or not all the strings contain the letter 'c'.

我知道任何 IEvaluator 都需要一个 IClientReceiveRecorder 来获取要评估的消息.

I know that any IEvaluator is going to need a IClientReceiveRecorder to get the messages it is to evaluate.

所以,我看到了几个选项.我会做类似的事情吗

So, I see a few options. Do I do something like

interface IEvaluator
{
    IEvaluator(IClientReceiveRecorder);

    void Evaluate();
} 

它不会编译,所以我猜不会.

It doesn't compile, so I am guessing not.

也许我会做类似的事情

interface IEvaluator
{
    void Evaluate(IClientReceiveRecorder);
}

或者,我是否只是让具体类在其构造函数中使用 IClientReceiveRecorder ?

Or, do I just leave it up to the concrete class to take a IClientReceiveRecorder in its constuctor?

推荐答案

直接的答案是将实现细节留给具体的类.

The straight forward answer is to leave implementation detail up to the concrete class.

知道 IEvaluator 的实现将依赖于 IClientReceiveRecorder 是一个实现细节,不会包含在 IEvaluator 中界面.

Knowing that the implementation of IEvaluator is going to have a dependency of IClientReceiveRecorder is an implementation detail and would not be included in the IEvaluator interface.

如果您知道接口将由另一个接口组成,您可以执行以下操作:

If you know that the interface is going to be composed of another interface you could do something like the following:

interface IEvaluator
{
    IClientReceiveRecorder { get; set; }

    void Evaluate();
}

该属性的填充方式是实现细节,不应成为接口的一部分.

How that property is populated is an implementation detail and should not be part of the interface.

这篇关于接口依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆