我有一个程序从客户端接收文件并对文件进行一些操作并将它们保存在磁盘上或不保存它们。为了解耦工作,我创建了一个名为IFileEditor
. 每个在文件上做某事的组件都应该实现这个接口:
public interface IFileEditor
{
string Name { get; set; }
byte[] Content { get; set; }
string EditedName { get; set; }
byte[] EditedConent { get; set; }
string ComponentName { get; }
XmlDocument Config { get; set; }
XmlDocument Result { get; set; }
void EditFile(byte[] content);
}
该接口的主要方法是EditFile,它接收文件内容并进行操作,并可能最后将结果保存到磁盘上。我编写的示例类是从实现此接口的图像创建缩略图:
public class ThumbnailCreator : IFileEditor
{
public string Name { get; set; }
public byte[] Content { get; set; }
public sting EditedName { get; set; }
public byte[] EditedConent { get; set; }
public XmlDocument Config { get; set; }
public XmlDocument Result { get; set; }
public void EditFile(byte[] content)
{
//change the file content and save the thumbnail content in disk
}
}
我可能有很多组件,例如 ThumbnailCreator,例如 zip 内容或其他任何对内容进行操作的组件。
在主程序中,我通过反射加载每个组件。加载它们的实现并不重要,只知道在主程序的.exe旁边复制组件的ddl,如果dll实现IFileEditor,我将其添加到列表中。
主要问题是,主应用程序只是接收文件并将它们传递给组件,组件完成工作。如果我想将一个组件的结果传递给另一个组件,我应该怎么做?
请记住,组件彼此不知道,主程序不应干扰传递结果。
我搜索了,我认为责任链设计模式将解决我的问题。不知道这样对吗?如果正确,如何实施?例如,一个组件创建缩略图并将结果传递给压缩缩略图。
我是这样写这部分的,每个开发人员都可以创建一个组件,并且主程序可以是可扩展的。
感谢您阅读这篇大文章。;)