如何应用接口隔离原理?

发布于 2025-02-02 14:54:22 字数 637 浏览 6 评论 0原文

我有一个公共接口,该界面由客户端创建了许多不同特定对象,唯一的共同点是它们都需要序列化为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2025-02-09 14:54:22

我有一个公共界面,该界面通过客户端创建一堆不同的特定对象,而唯一的共同点是它们都需要被序列化 xmls。

因此可以得出结论,这些对象之间没有共同点。甚至这些方法的参数也不同。

因此,我的建议是将所有方法分开并为每个对象创建单独的接口:

public interface IBuildXmlServiceXmlObject_1 
{
    XmlObject1 BuildClient1Xml(CustomObjectWithClient1Data data);
}

public interface IBuildXmlServiceXmlObject_2
{
    XmlObject2 BuildClient2Xml(CustomObjectWithClient2Data data);
}

public interface IBuildXmlServiceXmlObject_3 
{   
    XmlObject3 BuildClient3Xml(string string1, int int1);
}

public interface IBuildXmlServiceXmlObject_4 
{       
    XmlObject4 BuildClient4Xml(CustomObjectWithClient4Data data);
}

为什么拥有单独的接口更好?因为接口隔离原理是如何消耗接口的。因此,是否有必要消耗其他接口方法,不依赖于您所需的更多。

使用HDD的示例,该示例使用ISP:

public interface IReadable
{
    string Read();
}

public interface IWriteable
{
    void Write();
}

public class HDD : IReadable, IWriteable
{
    public string Read() { }

    public void Write()  { }
}

通过为read() and write()方法创建一个接口,它将义务类实现 otem 课堂中的方法。但是,有些类只想读取数据,其他类则要编写数据,而有些则是同时完成数据。例如,读取器可能想读取数据。因此,在这种情况下,最好创建单独的接口。

因此,让我们看看 cardReader 的另一个示例。 cardReader 刚刚读取数据,它不编写数据。因此,如果我们使用read()write()方法继承一个接口,那么我们将违反ISP原则。违反ISP的一个示例:

public interface IWriteReadable
{
    string Read();
    void Write();
}

public class CardReader : IWriteReadable
{
    // this is a necessary method
    public string Read() { }

    // here we are obligating to implement unnecessary method of interface
    public void Write() { }
}

因此,通过应用ISP,您仅在客户端类所需的接口中使用方法。如果您的类/客户端只想读取数据,则需要使用ireadable接口,而不是Ireadable -able -wripable

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.

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:

public interface IBuildXmlServiceXmlObject_1 
{
    XmlObject1 BuildClient1Xml(CustomObjectWithClient1Data data);
}

public interface IBuildXmlServiceXmlObject_2
{
    XmlObject2 BuildClient2Xml(CustomObjectWithClient2Data data);
}

public interface IBuildXmlServiceXmlObject_3 
{   
    XmlObject3 BuildClient3Xml(string string1, int int1);
}

public interface IBuildXmlServiceXmlObject_4 
{       
    XmlObject4 BuildClient4Xml(CustomObjectWithClient4Data data);
}

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:

public interface IReadable
{
    string Read();
}

public interface IWriteable
{
    void Write();
}

public class HDD : IReadable, IWriteable
{
    public string Read() { }

    public void Write()  { }
}

By creating one interface for Read() and Write() 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() and Write() methods, then we would violate ISP principle. An example of violation of ISP:

public interface IWriteReadable
{
    string Read();
    void Write();
}

public class CardReader : IWriteReadable
{
    // this is a necessary method
    public string Read() { }

    // here we are obligating to implement unnecessary method of interface
    public void Write() { }
}

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, not IReadableWriteable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文