这是装饰器模式的一个很好的用法吗?

发布于 2024-10-24 06:13:20 字数 473 浏览 3 评论 0原文

我必须为客户编写 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 技术交流群。

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

发布评论

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

评论(2

野心澎湃 2024-10-31 06:13:21

您如何根据客户配置在某处注册所有允许的文件编写器,然后执行所有注册的编写器?

更新:您可以使用 IoC 容器来实现此目的,但一个简单的

public class FileWriterRegistry
{
   public void Register(CustomerWriter writer)
   {
   }

   public void WriteAllFiles()
   {
       ... call Write() for each registered writer
   }
}

容器也可以。

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

public class FileWriterRegistry
{
   public void Register(CustomerWriter writer)
   {
   }

   public void WriteAllFiles()
   {
       ... call Write() for each registered writer
   }
}

might do.

断舍离 2024-10-31 06:13:21

如果您无法重构第二个编写器以共享公共基类或接口,那么您实际上可能想使用适配器模式。您可以编写一个适配器,使第二个编写器看起来像一个 CustomerWriter。这样您的客户就可以使用一个通用的基类或接口(甚至更好)。然后,您可以使用 IoC 容器来配置客户端实例化的实例。

适配器看起来像这样:

public SecondWriterAdapter : CustomerWriter
{
    private SecondWriter writerInstance;
    public SecondWriterAdapter(SecondWriter writerInstance)
    {
        this.writerInstance = writerInstance;
    }

    public override void Write()
    {
        writerInstance.someExistingWriteMethod();
    } 
}

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:

public SecondWriterAdapter : CustomerWriter
{
    private SecondWriter writerInstance;
    public SecondWriterAdapter(SecondWriter writerInstance)
    {
        this.writerInstance = writerInstance;
    }

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