实例化一个类并让它完成工作的最佳方法是什么

发布于 2025-01-02 01:19:32 字数 1432 浏览 1 评论 0原文

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

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

发布评论

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

评论(6

不弃不离 2025-01-09 01:19:32

类中的构造函数将完成从处理数据到将其存储在数据库中的整个工作

错误。切勿以这种方式编程。

在初始化了该类的基本必要性后,尽快退出c'tor。

创建 UserRegister 对象的方法。使用该方法调用 ureg 对象上的方法。

例如:

public void processRequest(name, age, sex, country)
{
    UserRegister ureg=new UserRegister(name,age,sex,country);
    ureg.updateSomeFields();
    ureg.doSomeBusinessLogic();
    ureg.prepareDBData();
    ureg.storeInDb();
}

这只是一个改进您当前设计的示例。但是您还应该有一些实用程序类。 UserRegister 类应该具有代表用户的数据和行为,仅代表用户。

设计实用程序类,使其以某种格式获取输入,进行一些计算,然后将输出数据返回给调用者。因此,当evr processRequest 方法需要完成一项工作时,它不会自行完成,而是调用另一个方法(它可能位于同一个类或其他类中)

编辑:

我要问的是,当你有一个类,在初始化后只需要在其中执行一系列固定的步骤时,应遵循什么方法

这取决于这些步骤的性质。找出它们之间的关系。它们与用户相关吗?如果是,将它们作为方法放在 User 类中。如果没有,则将相关步骤组合在一起,创建一个具有全名含义的类并使用它。创建单独的类时,尝试以它们可以用作实用程序的方式进行设计,而不仅仅是用于当前目的。

my constructor in the class will do the entire job from processing the data to storing it in db

Wrong. Never program this way.

Come out of c'tor ASAP after intializing the basic necessity of it's class.

The method in which UserRegister object is created. use that method to call methods on ureg object.

For example:

public void processRequest(name, age, sex, country)
{
    UserRegister ureg=new UserRegister(name,age,sex,country);
    ureg.updateSomeFields();
    ureg.doSomeBusinessLogic();
    ureg.prepareDBData();
    ureg.storeInDb();
}

This is only an example to improve your current design. however you should have some utility classes also. class UserRegister should have data and behaviour which represent a User, only a User.

Design Utility classes such that they take input in a format, do some calculation and then return the output data to it's caller. So, whenevr processRequest method needs a job to be done, it doesn't do it by itself but calls another method (it may be in same class or some other class)

EDIT:

What I am asking is what approach is to be followed when you have a class where after initializing there are just a fixed series of steps to be performed inside it

It depends on the nature of those steps. Find out how are they related. are they related to User? if yes, put them as methods in User class. if not, combine related steps together, create a class with meaning full name and use it. When creating a separate class, try to design in such a way they can be used as utility and not only for the current purpose.

孤千羽 2025-01-09 01:19:32

我会研究众多可用于将参数提取到表单中的 MVC 框架之一,然后使用业务层进行处理,并使用持久层进行数据库访问。

查看 AppFuse 获取众多 MVC 框架的示例以及它们如何与其他 Java 框架集成。即使您最终没有使用它,也有一些精彩的视频介绍了如何有效地使用这些框架组合来允许您评估各种技术来创建 3 层环境。

I'd look at one of the many MVC frameworks available to handle extracting the parameters into a form and then using a business layer to do your processing and a persistence layer for your database access.

Check out AppFuse for an example of the many many MVC frameworks around and how they integrate with other Java frameworks. Even if you don't end up using it, there are some great videos on how effectively these combinations of frameworks can be used to allow you to evaluate various technologies to create a 3 tiered environment.

呆萌少年 2025-01-09 01:19:32

通常最好将功能分解成多个部分。这使您能够单独对每个部分进行单元测试。根据表单的输入创建用户对象。然后调用数据访问层来保存用户。

阅读 MVC。我个人喜欢Spring MVC。您可以从对象中的表单获取信息。然后,您可以使用服务层来执行任何所需的转换。从那里您可以保留用户。

Normally its better to break the functionality up into pieces. This give you the ability to unit test each part individually. Create a user object from the input from the form. Then make a call to a data access layer to save the user.

Read up on MVC. I personally like Spring MVC. You can get the info from the form in an object. Then you use a service layer to do any transformation needed. From there you can persist the user.

趁年轻赶紧闹 2025-01-09 01:19:32

构造函数主要用于初始化任务,在其中创建对象。我会将数据库中存储的所有逻辑移至某些方法。此外,如果在数据库中存储数据的过程需要花费大量时间,那么挂在该用户的“线程”上根本不是一个好主意,您可以以异步方式完成工作,Servlet 3 支持异步

。这里已经说过,Spring MVC 将是一个非常值得关注的地方。

Constructors are mainly for initialization tasks, where the objects gets created. I would move all the logic of storing in the DB to some methods. Also if the process on storing data in the DB takes a lot of time, then hanging to this user's "thread" is not a good idea at all, you could do the work in an async way, async being supported from Servlet 3

As other have said here, Spring MVC would be a very good place to look at.

单身狗的梦 2025-01-09 01:19:32

我不喜欢这种方法。 UserRegister[] ur = new UserRegister[10] 到底会做什么?如果我有UserRegister ur = readfromDatabase(...)。由于此函数必须创建一个 UserRegister 才能返回,因此数据会再次保存到数据库中!?奇怪又令人困惑。如果我想保存到文件而不是数据库怎么办?

最好将数据对象的创建与数据持久性分开。

I don't like this approach. What exactly will UserRegister[] ur = new UserRegister[10] do? What if I have UserRegister ur = readfromDatabase(...). Since this function has to create a UserRegister to return, the data gets saved again to the database!? Weird and confusing. What if I want to save to a file and not a database?

Better to separate the data object creation from data persistence.

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