使用哪种设计模式来实现“数据扩展器”?

发布于 2024-10-02 06:18:50 字数 525 浏览 1 评论 0原文

这就是我想要做的:

public class DataBuilder {
  public DataBlock create() {
    DataBlock block = new DataBlock();
    for (Extender extender : this.getExtenders()) {
      extender.extend(block);
    }
  }
}

一段时间后:

DataBlock block = new DataBuilder().create();

每个扩展器都会向添加一些特定信息。 DataBuilder 不想了解这些扩展器的内部结构。目前,我的 getExtenders() 方法查找 Extender 的所有子类(在类路径中),并返回它们的实例。

工作正常,但就设计而言,我不喜欢它的外观。也许我可以/应该使用某种模式,使这个结构更加灵活?

This is what I'm trying to do:

public class DataBuilder {
  public DataBlock create() {
    DataBlock block = new DataBlock();
    for (Extender extender : this.getExtenders()) {
      extender.extend(block);
    }
  }
}

Some time later:

DataBlock block = new DataBuilder().create();

Every extender will add some specific information to the block. DataBuilder doesn't want to know anything about internal structure of these extenders. Currently my getExtenders() method finds all subclasses of Extender (in classpath), and returns instances of them.

Works fine, but I don't like how it looks, in terms of design. Maybe I could/should use some pattern, to make this construct more flexible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笑看君怀她人 2024-10-09 06:18:50

继续 Adrian 的回答,我将简要解释它是如何工作的:

让我们假设 DataBlock 派生自一个名为 IDataBlock 的接口。每个扩展器都派生自 DataBlockDecorator,而 DataBlockDecorator 又派生自 IDataBlock,后者对 DataBlock 执行一些操作(在构造函数中接受 IDataBlock)。这允许你做类似的事情。

IDataBlock block = new DataBlock();
for (DataBlockDecorator extender : this.getExtenders()) {
  extender.extend(block);
  block = extender;
}

然而,它并没有真正比以前增加更多的灵活性。就此而言,您原来的解决方案已经非常灵活了。

Continuing Adrian's answer, I'll explain briefly how it would work:

Lets suppose DataBlock derives from an interface called IDataBlock. Each extender derives from DataBlockDecorator which derives from IDataBlock which does some operation to the DataBlock (accepting IDataBlock in the constructor). This allows you to do something like.

IDataBlock block = new DataBlock();
for (DataBlockDecorator extender : this.getExtenders()) {
  extender.extend(block);
  block = extender;
}

However, it doesn't really add anymore flexibility than before. For that matter, your original solution is already as flexible as it gets.

予囚 2024-10-09 06:18:50

查看 装饰器 模式。

Have a look at the Decorator pattern.

尘曦 2024-10-09 06:18:50

装饰模式

interface BlockDeckorator
{
    extend(DataBlock block);
}

class FooBlockDecoratorImpl implements BlockDecorator
{
    private BlockDeckorator blockDecorator;

    public FooBlockDecoratorImpl(BlockDecorator decorator)
    {
         this.blockDecorator = decorator;
    }

    public void extend(DataBlock block)
    {
         blockDecorator(block);
         // Add my own code to do Foo afterwards (or before)
    }
}

Decorator Pattern

interface BlockDeckorator
{
    extend(DataBlock block);
}

class FooBlockDecoratorImpl implements BlockDecorator
{
    private BlockDeckorator blockDecorator;

    public FooBlockDecoratorImpl(BlockDecorator decorator)
    {
         this.blockDecorator = decorator;
    }

    public void extend(DataBlock block)
    {
         blockDecorator(block);
         // Add my own code to do Foo afterwards (or before)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文