全静态方法和应用单例模式有什么区别?

发布于 2024-10-10 20:59:18 字数 369 浏览 4 评论 0原文

我正在创建一个数据库来存储有关我的网站用户的信息(我正在使用 stuts2,因此使用 Java EE 技术)。对于数据库,我将创建一个 DBManager。我应该在这里应用单例模式还是将其所有方法设为静态?

我将使用这个 DBManager 进行基本操作,例如添加、删除和更新用户配置文件。除此之外,我还将用于所有其他查询目的,例如查明用户名是否已存在以及获取所有用户以用于管理目的等。

我的问题

  • 单例模式有什么好处?
  • 哪件事在这里最合适?所有静态方法还是单例模式?
  • 请比较一下两者。

问候

shahensha

​数据库比这个大。这里我仅讨论将用于存储用户信息的表。

I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton pattern here or rather make all it's methods static?

I will be using this DBManager for basic things like adding, deleting and updating User profiles. Along with it, I'll use for all other querying purposes, for instance to find out whether a username already exists and to get all users for administrative purposes and stuff like that.

My questions

  • What is the benefit of singleton pattern?
  • Which thing is most apt here? All static methods or a singleton pattern?
  • Please compare both of them.

regards

shahensha

P.S. The database is bigger than this. Here I am talking only about the tables which I'll be using for storing User Information.

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

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

发布评论

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

评论(3

送舟行 2024-10-17 20:59:18

我应该在这里应用单例模式还是将其所有方法设为静态?

两者都没有。 只需创建一个

在简单的servlet容器中,您可以使用 ServletContextListener 为此。在webapp启动时,创建一个并通过ServletContext#setAttribute()将其放入应用程序范围内。在 web 应用程序的生命周期内,所有 servlet 都可以使用它。对于基本的启动示例,您可以找到这篇文章 有用。

Should I apply singleton pattern here or rather make all its methods static?

None of both. Just create one.

In a simple servletcontainer, you can use ServletContextListener for this. During webapp's startup, create one and put it in the application scope by ServletContext#setAttribute(). It'll be available to all servlets during webapp's lifetime. For a basic kickoff example, you may find this article useful.

少跟Wǒ拽 2024-10-17 20:59:18

我不知道 EE 的内部原理,但通常静态类和 Singleton 之间的区别在于实例化 - 对于静态类,没有实际的实例化,没有成员数据,没有对对象的引用。所以实际的区别是......不多。

我认为这里真正的好处是概念上的......静态方法和属性是适用于类的抽象概念的方法,而不是适用于特定实例的方法。如果您在这里创建一个单例用户对象,那么该用户是谁?为什么他有特定的背景来创建和更新个人资料?我认为概念图不太好。

对我来说,说 UserProfile.Update(用户名、密码、名字...) 更有意义。您是说“根据 UserProfile 的抽象概念对这一特定数据集执行更新任务”。

请记住,我对“摘要”一词的使用严格来说是比喻性的,而不是在计算机科学意义上使用该词。

I don't know the internals of EE, but typically the difference between a static class and a Singleton is instantiation - with the static class, there is no actual instantiation, no member data, no reference to an object. So the practical difference is...not much.

I think the real benefit here is conceptual...static methods and properties are methods which apply to the abstract concept of the class, and not to a particular instance. If you create a singleton user object here, who is that user? Why does he have the particular context to create and update profiles? I don't think the concept maps very well.

It makes much more sense to me to say UserProfile.Update(username, password, firstName...). You're saying "perform the task Update from the abstract concept of UserProfile on this particular set of data".

Keep in mind here that my use of the word abstract is strictly figurative, and not in the computer science sense of the word.

小瓶盖 2024-10-17 20:59:18

没有太大的实际差异,但有一定的哲学差异。

我会在这里推荐单例方法 - 您实际上描述的是一种数据访问对象(DAO),尽管是在较高的级别。

关于原因的一些想法:

  • 创建 DBManager 本身(即 DBManagerFactory)的工厂风格方法将使单元测试更容易;它适用于依赖注入模型。

  • 具有一堆静态方法的类通常被认为是没有总体主题(即方法之间的独立性)的 switchblade/utility 类型的类。想想像 StringUtils 这样的类——人们假设该类没有一般状态。

  • 在粗略的层面上,使用“所有静态方法”方法,您会为自己创建大量工作,因为您需要为每个方法保持键入静态。同样,您可以只拥有一个 DBManagerFactory,它只具有用于创建 DBManager 的单个静态方法,最终成为一个单例。

Not much practical difference, but somewhat of a philosophical difference.

I would recommend the singleton approach here - you're really describing a type of Data Access Object (DAO), albeit at a high level.

Some thoughts on why:

  • A Factory style approach to creation of the DBManager itself (i.e., DBManagerFactory) would make unit testing easier; it lends itself to the dependency injection model.

  • A class with a bunch of static methods is generally assumed to be a switchblade/utility kind of class with no over-arching theme (i.e., independence between methods). Think of a class like StringUtils -- people assume that there's no general state to the class.

  • At a crass level, with the "all static methods" approach, you're creating a lot of work for yourself in that you need to keep typing static for every method. Again, you can just have a DBManagerFactory that simply has a single static method for creating the DBManager, which ends up being a singleton.

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