Java中创建对象的设计问题

发布于 2024-10-18 12:45:25 字数 577 浏览 2 评论 0原文

我正在开发一个应用程序,订阅者可以在其中发送短信来获取任何包裹。

在我的程序中,当我收到用户请求时,我必须创建 Subscriber 对象。为了初始化订阅者对象,我必须查询后端数据库和 IN 节点,然后设置订阅者对象的属性。

请建议,我应该使用工厂模式还是构建器?或者有什么好的选择吗?对于在 Factory 类中放置查询数据库和调用 XMLRPC 的代码有什么建议?

我的类看起来像下面

public class Subscriber {

private String subno;
private String subPackageType;
private String subTariff;
private String subRequest;
private boolean isTransferable;
}

现在 subno,subRequest 我从 SMS 获取,subPackageType 从数据库获取,subTariff 通过发送 XMLRPC 命令从 IN 节点获取。

我还有更多属性,但我只是提到一些来提供概念。

问候,
伊姆兰

I am developing an application in which subscriber send SMS to get any package.

In my program, when I received user request, I have to create Subscriber object. To initialize subscriber object, I have to query back-end database and IN node then set attributes of subscriber object.

Please suggest for this, should I use Factory pattern or Builder? or any good alternative? whats suggestions for putting my code for querying database and calling XMLRPC in Factory class?

My class look like following

public class Subscriber {

private String subno;
private String subPackageType;
private String subTariff;
private String subRequest;
private boolean isTransferable;
}

Now subno,subRequest i got from sms, subPackageType from database and subTariff from IN node by sending XMLRPC command.

I have more attributes as well but i just mention some to provide concept.

Regards,
imran

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

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

发布评论

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

评论(2

童话里做英雄 2024-10-25 12:45:25

不要再思考“我应该使用哪种模式”,就好像任何应用程序都存在单一模式,仅此而已,只需应用特定模式就会神秘地修复世界。

开始写作,模式就会出现。最终,随着经验的积累,这将成为第二天性。
不要试图强迫事物形成某种模式,让它成长和成熟。

Stop thinking "which pattern should I use" as if any application exists of a single pattern and that's it, that just applying a specific pattern will mysteriously fix the world.

Start writing, and a pattern will emerge. Eventually, with experience, that will become second nature.
Don't try to force things into a pattern, let it grow and mature.

霞映澄塘 2024-10-25 12:45:25

无论如何,首先使用工厂是一个很好的做法。
其次,在您的情况下,我认为您应该从数据库检索数据,然后创建订阅者对象。在这种情况下,您实际上在创建期间将数据从数据库传递到订阅者对象。如果由于某些其他原因你不能这样做,只需将 Subscriber 设为一个 bean,即提供适当的 setter。在这种情况下,您可以创建对象,然后查询数据库,然后通过调用适当的设置器来设置所有需要的属性。

看来 Builder 模式在这里帮不了你太多。

First using factory is a good practice anyway.
Second in your case I think that you should retrieve the data from DB and then create the Subscriber object. In this case you actually pass the data from DB to the subscriber object during creation. If for some other reason you cannot do it just make Subscriber to be a bean, i.e. provide appropriate setters. In this case you can create object, then query DB and then set all needed properties by calling appropriate setter.

It seems that Builder pattern could not help you too much here.

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