单例模式用来创建独一无二的对象。有些类型比如驱动程序等只能有一个对象,如果创建出多个对象的时候可能会导致许多问题的产生。单例模式还是很简单的,实现方法也很多,不过需要注意并发的情况。下面是一个简单的例子(非并发):

class singleton{
private static singleton single;
private singleton(){
}
public singleton getSigle(){
if(single == null){
single = new singleton();
}
return single;
}
}

这样,singleton就是不是谁想创建就能创建的啦。

相关文章:

  • 2021-07-26
  • 2022-01-20
  • 2021-11-26
  • 2021-12-13
  • 2021-11-06
猜你喜欢
  • 2021-08-23
  • 2022-12-23
  • 2022-02-28
  • 2021-11-08
  • 2022-01-21
  • 2021-12-04
相关资源
相似解决方案