| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2365 人关注过本帖
标题:JAVA中的让类成为singleton是什么意思?
只看楼主 加入收藏
hqsiswiliam
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2011-11-18
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:5 
JAVA中的让类成为singleton是什么意思?
A singleton is simply a class that is instantiated exactly once. Singletons typically represent a system component that is intrinsically unique, such as the window manager or file system. Making a class a singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type.

表示不理解啊。。
搜索更多相关主题的帖子: substitute difficult interface represent exactly 
2011-11-18 11:45
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:10 
单例模式:单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2011-11-19 09:12
hqsiswiliam
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2011-11-18
收藏
得分:0 
回复 2楼 lampeter123
singleton如何向整个系统提供实例?
2011-11-22 07:11
让我们飞
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:40
专家分:119
注 册:2011-1-9
收藏
得分:0 
class  A {
    private static A a=null;
    private A(){
        ...
}
    private static A getInstance(){
        a=new A();
        
        return a;   
}
}

调用的时候 A a= getInstance();获得实例
2011-11-23 10:47
鱼骨头Main
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:55
专家分:138
注 册:2009-3-27
收藏
得分:0 
class  A {
    private static A a=null;
    private A(){
        ...
}
    private static A getInstance(){
        a=new A();
      

        return a;  

}
}


这代码有问题哦,
最大的问题是,a=new A();

public class Test{
   private static Test test = null;

   private Test(){}
   

   // 如果不是synchorzied 就是synchorized

   public synchorzied static Test getNewInstance(){
      if(null == test)
         test = new Test();
      return test;
   }
}
单列模式最寓意在于,程序运行期间,值允许有该类的一个实例存在
private Test(){}   为了确保只存在一个,所以用private
synchorzied 同步,主要为了避免多线程产生多个实例
2011-11-29 17:42
lianlupeng
Rank: 2
等 级:论坛游民
帖 子:12
专家分:27
注 册:2011-7-1
收藏
得分:0 
以下是引用鱼骨头Main在2011-11-29 17:42:44的发言:

单列模式最寓意在于,程序运行期间,值允许有该类的一个实例存在
private Test(){}   为了确保只存在一个,所以用private
synchorzied 同步,主要为了避免多线程产生多个实例

是synchronized ,嘿嘿,还有应该判断一下,但里是不是已经存在了啊。

祝好
2011-12-02 16:16
快速回复:JAVA中的让类成为singleton是什么意思?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015006 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved