这是装饰器模式的一个很好的用法吗?
我必须为客户编写 2 个文本文件。这些文件包含来自我们数据库的信息。我们的应用程序被许多客户使用,因此不会为每个客户编写这两个文件。有些只获得第一个文件,有些还获得第二个文件。文件结构因客户而异,这就是为什么我使用抽象方法 Write 创建一个抽象类,并为每个客户创建一个依赖 Write 方法的特定类。这是第一个文件,适用于所有客户,只是内容和结构不同。
abstract class CustomerWriter
{
//...
abstract Write();
//...
}
然后我有两个继承自该类的类。我想那很好。现在,对于只需要为某些客户编写的第二个文件......在这里使用装饰器模式并装饰第一个文件的类是一个好主意吗?它不是同一个基类(CustomerWriter)。我不确定这对我的情况来说是否太多,这就是我问的原因。
我将如何装饰第一个文件的特定类?
谢谢 :-)
I have to write 2 text files for a customer. The files contain information from our DB. Our app is being used by many customers and thus not both files will be written for every customer. Some just get the first file and some also the 2nd. The file structure differs from customer to customer, thats why I made an abstract class with an abstract method Write and a specific class for each customer which iverrides the Write method. Thats for the first file which is for all customers, just with different content and structure.
abstract class CustomerWriter
{
//...
abstract Write();
//...
}
Then I have 2 classes which inherit from that. Thats fine I guess. Now for the second file which only needs to be written for some of the customers ... is it a good idea to use the decorator pattern here and decorate the class for the first file? Its not the same base class (CustomerWriter). I am not sure if this is too much for my case, thats why I am asking.
How would I decorate the specific class for the first file?
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何根据客户配置在某处注册所有允许的文件编写器,然后执行所有注册的编写器?
更新:您可以使用 IoC 容器来实现此目的,但一个简单的
容器也可以。
How about you register all allowed file writers based on the customer configuration somewhere and then you execute all the writers which are registered?
Update: You could use an IoC container for that but a simple
might do.
如果您无法重构第二个编写器以共享公共基类或接口,那么您实际上可能想使用适配器模式。您可以编写一个适配器,使第二个编写器看起来像一个 CustomerWriter。这样您的客户就可以使用一个通用的基类或接口(甚至更好)。然后,您可以使用 IoC 容器来配置客户端实例化的实例。
适配器看起来像这样:
If you cannot refactor the second writer to share a common base class or interface, you may actually want to use the adapter pattern. You could write an adapter which makes the second writer appear to be a CustomerWriter. That way your clients can use one common base class or interface (even better). Then you can use an IoC container to be able to configure which instance the clients instantiate.
The adapter would look something like this: