本文介绍了在项目中有很多单例类是好的编程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是最好的方法是什么?


  1. 可以创建为静态对象。

  2. 可以创建为单身

  3. 可以创建为全局。

实现此功能的最佳设计模式是什么?



我想把所有的类都创建为singleton,但是这会创建很多单例。



使用单例的优点和缺点是什么?

解决方案

查看Steve Yegge的博客文章 -


I have a few classes in a project that should be created only once.

What is the best way to do that?,

  1. They can be created as static object.
  2. Can be created as singleton
  3. Can be created as global.

What is the best design pattern to implement this?

I am thinking of creating all classes as singleton, but that would create lot of singletons. Is it good programming practice to have lot of singletons?

What are the pros and cons for using singletons?

解决方案

Take a look at Steve Yegge's blog post about this - Singleton Considered Stupid

这篇关于在项目中有很多单例类是好的编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:19