如何应用接口隔离原理?
我有一个公共接口,该界面由客户端创建了许多不同特定对象,唯一的共同点是它们都需要序列化为XML。
这样的事情:
public interface IBuildXmlService
{
XmlObject1 BuildClient1Xml(CustomObjectWithClient1Data data);
XmlObject2 BuildClient2Xml(CustomObjectWithClient2Data data);
XmlObject3 BuildClient3Xml(string string1, int int1);
XmlObject4 BuildClient4Xml(CustomObjectWithClient4Data data);
}
现在这个界面开始变得很大,我绝对敢肯定这不是一个好兆头。同样,每当我将界面注入客户端特定类时,客户特定类都可以访问其他客户的所有其他对象创建方法。
现在,我想到的第一件事就是将对象创建方法作为客户端特定类中的私人方法移动,但这会导致很多大文件。
然后,我想使用工厂方法模式来创建使用buildxml方法来创建对象创建部分的IXMLFACTORY,但这似乎不起作用,因为我有不同的对象可以返回。
谁能给我一些有关我应该寻找哪种设计模式的建议?
I have a public interface that creates a bunch of different specific objects by client, and the only thing in common is that they all need to be serialized as xmls.
Something like this:
public interface IBuildXmlService
{
XmlObject1 BuildClient1Xml(CustomObjectWithClient1Data data);
XmlObject2 BuildClient2Xml(CustomObjectWithClient2Data data);
XmlObject3 BuildClient3Xml(string string1, int int1);
XmlObject4 BuildClient4Xml(CustomObjectWithClient4Data data);
}
Now this interface started to get big, and I'm definitely sure this is not a good sign. Also every time I inject the interface into a client specific class, that client specific class has access to all the other object creation methods for other clients.
Now the first thing I thought about was to just move the object creation method as a private method in client specific class, but this results in quite some big files.
Then I thought to use the factory method pattern to create a IXmlFactory with a BuildXml method for the object creation part, but this doesn't seem to work since I have different objects to return.
Can anyone give me some advice about what design pattern should I look for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此可以得出结论,这些对象之间没有共同点。甚至这些方法的参数也不同。
因此,我的建议是将所有方法分开并为每个对象创建单独的接口:
为什么拥有单独的接口更好?因为接口隔离原理是如何消耗接口的。因此,是否有必要消耗其他接口方法,不依赖于您所需的更多。
使用HDD的示例,该示例使用ISP:
通过为
read()
andwrite()
方法创建一个接口,它将义务类实现 otem 课堂中的方法。但是,有些类只想读取数据,其他类则要编写数据,而有些则是同时完成数据。例如,读取器可能想读取数据。因此,在这种情况下,最好创建单独的接口。因此,让我们看看 cardReader 的另一个示例。 cardReader 刚刚读取数据,它不编写数据。因此,如果我们使用
read()
和write()
方法继承一个接口,那么我们将违反ISP原则。违反ISP的一个示例:因此,通过应用ISP,您仅在客户端类所需的接口中使用方法。如果您的类/客户端只想读取数据,则需要使用
ireadable
接口,而不是Ireadable -able -wripable
。So it can be concluded that there is no common between these objects. Even parameters of these methods are different.
So my suggestion is to separate all methods and create separate interface for each object:
Why is it better to have separate interfaces? Because Interface Segregation Principle is how interface can be consumed. So, whether it is necessary to consume other methods of interface or don’t depend on more than you need.
An example with HDD that uses ISP:
By creating one interface for
Read()
andWrite()
methods, it would obligate class to implement both methods in class. But some classes only want to read data, others want to write data, and some to do both. E.g. card reader might want to read data. So in this case it is better to create separate interfaces.So let's look another example with CardReader. CardReader just reads data, it does not write data. So, if we inherit one interface with
Read()
andWrite()
methods, then we would violate ISP principle. An example of violation of ISP:So by applying ISP, you only puts methods in interface that are necessary for the client class. If your class/client just wants to read data, then you need to use
IReadable
interface, notIReadableWriteable
.