如何获取/设置并非总是实现的接口的属性 [英] How to get/set a property of an interface that is not always implemented

查看:55
本文介绍了如何获取/设置并非总是实现的接口的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当实现一个接口的类的属性并不总是存在于实现同一接口的所有类中时,设置该对象的属性的一种好方法(面向对象)是什么?

What is a good (object oriented) way of setting a property of a class which implements an interface, when that property doesn't always exist in all classes that implement that same interface?

例如

假设我有一个界面

public interface IDataRepository {

    public DataStructure GetData();   // DataStructure is an arbitrary class, doesn't matter for this example

}

现在我也有两个继承自此的类

Now I also have two classes that inherit from this

public class DatabaseRepository : IDataRepository {

    public DataStructure GetData() 
    {
       // get data from database
    }
}

public class FileRepository : IDataRepository {

    public string WorkingFolder { get; set; }

    public DataStructure GetData() {
       // get data from files
    }
}

现在,我的客户端方法不一定知道存储库是什么,但这是我想做的...

Now my client method doesn't necessarily know what the repository is but here's what I want to do...

private DataStructure ReadData(IDataRepository repository)
{
     repository.WorkingFolder = @"C:\Data"; // What is the best way of doing this?

     return repository.GetData();
}

显然,以上代码无法正常工作,我可以...

obviously the above code won't work and I could do...

     if (repository is FileRepository) {
        ((FileRepository)repository).WorkingFolder = @"C:\Data";
     }

或将WorkingFolder添加为接口的属性(以及实现该接口的所有类),即使在大多数情况下无关紧要.

or add WorkingFolder as a property of the interface (and therefore all the classes that implement it) even though in most cases it's irrelevant.

,但是这两个(尤其是第一个)看起来都不太优雅,也不是非常面向对象的.这种方法的oop方法是什么?

but both of these (esp. the first one) seem very inelegant and not very object oriented. What is the oop way of doing this kind of thing?

修改

一个明显的问题是,如果该方法不知道repository是什么,它如何知道WorkingFolder的正确值...但是以上内容是我正在尝试做的事情的过度简化,所以我们只说它可以找出...

The obvious question is if the method doesn't know what repository is, how can it know the correct value for WorkingFolder... But the above is an over-simplification of what I'm trying to do, so let's just say it can find out...

推荐答案

显然,您的ReadData方法不能实际上接受任何类型的存储库.它只能处理FileRepository.这就是它的期望,这也是它完成工作所需要的.鉴于此,这实际上是它应该接受的参数,而不是实际上没有提供足以完成其工作的合同的接口.

Apparently your ReadData method can't actually accept any type of repository. It is only able to handle a FileRepository. That's what it expects, and that's what it needs to do its job. Given that, that's what it should actually accept as its parameter, rather than an interface that doesn't actually provide a contract that is sufficient for it to do its job.

拥有接口的全部目的是,使使用该接口的任何人都可以使用它,而无需关心实现是什么.因此,如果您想使用该接口,则需要在该接口的定义中包括足够的信息,以便它提供使用该接口的任何人都需要的所有操作,否则最好不要使用它全部(至少针对该特定操作).

The entire point of having an interface is so that anyone using that interface can use it without caring what the implementation is. So if you do want to use the interface you need to include enough information in the interface's definition such that it provides every operation that anyone using the interface needs, otherwise you're better off just not using it at all (at least for that specific operation).

对于给出的 specific 示例,您可能应该只提供一个已经配置的存储库,该存储库具有它需要的任何值才能允许此方法执行其任务.工作,作为参数.从任意存储库读取值的方法根本没有必要配置该存储库.也就是说,如果它确实正在从任意存储库中读取某些内容.

As for the specific example given, you should probably just be providing an already configured repository, that has whatever values it needs in order to allow this method to do its work, as a parameter. It doesn't make sense for a method that's reading a value from an arbitrary repository to be configuring that repository at all. That is, if it really is reading something from an arbitrary repository.

这篇关于如何获取/设置并非总是实现的接口的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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