在java中,我会创建这样的东西:
private static MyClass instance;
public static MyClass getInstance() {
if(instance != null) {
return instance;
}
instance = new MyClass();
return instance;
}
Run Code Online (Sandbox Code Playgroud)
在 ruby 中获得相同功能的合适方法是什么?
更新:我已经阅读了关于“包含单例”的内容,但是当我尝试在 Ruby 1.9 上的 irb 中执行此操作时,我得到了:
[vertis@raven:~/workspace/test]$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin9.4.0]
[vertis@raven:~/workspace/test]$ irb
irb(main):001:0> class TestTest
irb(main):002:1> include Singleton
irb(main):003:1> end
NameError: uninitialized constant TestTest::Singleton
from (irb):2:in `<class:TestTest>'
from (irb):1
from /usr/local/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud) 可以使用Java中的枚举来保证对象的唯一实例,如下所示:
public enum EmmaTest {
;
public static int someStaticMethod() {
return 33;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在这些对象上实现100%的Emma测试覆盖率?可能吗?或者是否可以告诉Emma忽略某些方法?
我能得到的最好的是:

问题说了一切.有没有特别的事件发生?我有一个需要实现Serializable接口的类.我正在使用Wicket开发一个应用程序.我已经实现了一个Factory类,它将提供一些链接(以删除一遍又一遍地写入的相同代码块的冗余):
public class NavigationLinkFactory implements Serializable {
private static final long serialVersionUID = 7836960726965363785L;
private NavigationLinkFactory() {
}
private static class SingletonHolder {
public static final NavigationLinkFactory instance = new NavigationLinkFactory();
}
public static NavigationLinkFactory getFactory() {
return SingletonHolder.instance;
}
private Object readResolve() throws ObjectStreamException {
return SingletonHolder.instance;
}
public Link<Serializable> getUserHomeLink() {
return new Link<Serializable>("home", new Model<Serializable>()) {
private static final long serialVersionUID = -8282608926361995521L;
@Override
public void onClick() {
EMSSession session = (EMSSession) getSession();
session.setActiveHorizonalMenu(1);
setResponsePage(HomePage.class);
}
}; …Run Code Online (Sandbox Code Playgroud) 我在Mongodb中遇到了一些奇怪的错误,在Mongodb中,你应该使用Mongo单身人士.我只是想确保这是非常有效的.
public class DBManager {
public static Mongo mongoSingleton = null;
public static synchronized void getMongo(){
if(mongoSingleton == null){
mongoSingleton = new Mongo();
}
return mongoSingleton;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有一个方法,在这个方法里面我有一个块:
public void method()
{
[block instructions]
}
Run Code Online (Sandbox Code Playgroud)
但是这个方法在我的程序中被调用了两次.我希望这个块只执行一次,并且只对第一次出现的方法执行.什么是最好和最优雅的方式呢?
我增强/测试了我在使用Singleton类初始化/访问的ArrayList上找到的编码
import java.util.ArrayList;
public class SingletonArrayList {
private static SingletonArrayList mInstance;
private static ArrayList<String> list = null;
public static SingletonArrayList getInstance() {
if (mInstance == null)
mInstance = new SingletonArrayList();
SingletonArrayList.list.add("a");
SingletonArrayList.list.add("b");
SingletonArrayList.list.add("c");
return mInstance;
}
private SingletonArrayList() {
list = new ArrayList<String>();
}
// retrieve array from anywhere
public ArrayList<String> getArray() {
return SingletonArrayList.list;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我做了一个测试类,我将上述单例调用两次:
import java.util.ArrayList;
public class TestSingletonArrayList {
public static void main(String[] args) {
ArrayList<String> array = SingletonArrayList.getInstance().getArray();
for (int i = …Run Code Online (Sandbox Code Playgroud) 实现单身时:
class MyConnection {
private static MyConnection connection = new MyConnection();
private MyConnection() {
}
public static MyConnection getConnection() {
return connection;
}
}
Run Code Online (Sandbox Code Playgroud)
1)为什么我们给予connection静态?
这只是因为它getConnection()是静态的,我们不能在静态上下文中引用非静态或者还有其他原因吗?
2)是否有必要宣布connection为最终?
通常我使用第一个实现.几天前我发现了另一个.任何人都可以解释这两个实现之间的区别吗?第二个实现是线程安全的吗?在第二个例子中使用内部类有什么好处?
//--1st Impl
public class Singleton{
private static Singleton _INSTANCE;
private Singleton() {}
public static Singleton getInstance(){
if(_INSTANCE == null){
synchronized(Singleton.class){
if(_INSTANCE == null){
_INSTANCE = new Singleton();
}
}
}
return _INSTANCE;
}
}
//--2nd Impl
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
private static final Singleton _INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder._INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud) 我在stackexchange中遇到了另一篇关于实现java单例的各种方法的文章.显示的方法之一是以下示例.它的投票率非常低.想知道为什么. 在Java中实现单例模式的有效方法是什么?
public class Singleton {
private static Singleton instance = null;
static {
instance = new Singleton();
// do some of your instantiation stuff here
}
private Singleton() {
if(instance!=null) {
throw new ErrorYouWant("Singleton double-instantiation, should never happen!");
}
}
public static getSingleton() {
return instance;
}
}
Run Code Online (Sandbox Code Playgroud) 我的目标是使用try-with-resource构造和一个类的单个实例,即一个处理连接池的单例.我需要这个以确保连接池在一切结束时关闭,然后我想使用try-with-resource.例如
public class MyHandler implements java.io.Closeable{
public static ConnectionPool pool;
//Init the connection pool
public MyHandler(){
pool = ...;
}
@Override
public void close() {
pool().close();
}
}
Run Code Online (Sandbox Code Playgroud)
可能的地方main是:
public static void main(String [] args){
try(MyHandler h = new MyHandler){
//execute my code
// somewhere I do MyHandler.pool.something();
}
}
Run Code Online (Sandbox Code Playgroud)
如何确保将MyHandler用作单例?
java ×9
singleton ×6
coding-style ×1
emma ×1
enums ×1
object ×1
ruby ×1
serializable ×1
try-catch ×1
unit-testing ×1