初始化一个从文件加载数据的类。是建筑商吗?

发布于 2024-12-04 19:13:53 字数 761 浏览 3 评论 0原文

我是设计模式的新手。
我想创建一个类的实例(例如 ClassA),并将其一些字段设置为从配置文件中读取的值。
如果我将类的代码与管理具有值的文件的代码区分开来,那么 ClassA 就成为纯粹的“业务逻辑”类。

 class ClassA {
    boolean config1;
    String originDB;
    String destDB;
    //other fields not initialized at the construction time
  }

  class ClassAFactory {
    boolean config1;
    String originDB;
    String destDB;        

    public ClassAFactory {
      //constructor of the class factory
      //load config values from file
    }

    public ClassA newInstance() {
     //create a new instance of ClassA and config fields config1, originDB, destDB
    }

  }

我想说这是一种构建器模式,因为构建器似乎不仅负责实例化,还负责初始化。
但除此之外,构建器似乎专注于分步打破创建过程,而就我而言,我只有一个(但复合)步骤。

可以被认为是构建器模式吗?或者有什么不同吗?

I am a newbie in design patterns.
I want to create an instance of a class, say ClassA, and set some of its fields to the values read from a config file.
If I keep distinct the code of the class from the code that manages the file with the values then ClassA becomes a pure "businness logic" class.

 class ClassA {
    boolean config1;
    String originDB;
    String destDB;
    //other fields not initialized at the construction time
  }

  class ClassAFactory {
    boolean config1;
    String originDB;
    String destDB;        

    public ClassAFactory {
      //constructor of the class factory
      //load config values from file
    }

    public ClassA newInstance() {
     //create a new instance of ClassA and config fields config1, originDB, destDB
    }

  }

I would say that this is a builder pattern because builder seems to take care of "not only instantiation" but initialization too.
But otherwise, builder seems to be focused on breaking the creation process in steps, while in my case I have only one (yet compound) step.

May be considered a builder pattern? Or is it something different?

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

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

发布评论

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

评论(2

も星光 2024-12-11 19:13:53

没有步骤 - 没有Builder

    ClassA a = ClassABuilder
            .config(config1)
            .originDB(originDB)
            .destDB(destDB)
            .build();
    // above reads a Builder to me and hopefully to any reader
    //   note above allows to swap, add, remove particular steps


    ClassA a = ClassAFactory.newInstance();
    // above reads Factory to me and hopefully to any reader
    //   note for reader, it's a one-step thing

no steps - no Builder

    ClassA a = ClassABuilder
            .config(config1)
            .originDB(originDB)
            .destDB(destDB)
            .build();
    // above reads a Builder to me and hopefully to any reader
    //   note above allows to swap, add, remove particular steps


    ClassA a = ClassAFactory.newInstance();
    // above reads Factory to me and hopefully to any reader
    //   note for reader, it's a one-step thing
日裸衫吸 2024-12-11 19:13:53

看起来更像工厂模式而不是构建器。这是因为只有一个方法可以完成所有工作。通常,构建器会有一组 setter 样式的方法,允许自定义正在构建的对象。

Looks more like a Factory pattern than a Builder. This is because there is only an single method that does all the work. Generally a builder would have a set of setter-style methods that would allow for the customization of the object being built.

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