我正在尝试追踪我们系统中的问题,以下代码让我担心.主servlet中的doPost()方法发生以下情况(名称已更改以保护有罪):
...
if(Single.getInstance().firstTime()){
doPreperations();
}
normalResponse();
...
Run Code Online (Sandbox Code Playgroud)
单身'单身'看起来像这样:
private static Single theInstance = new Single();
private Single() {
...load properties...
}
public static Single getInstance() {
return theInstance;
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式设置为使用静态初始化程序而不是在getInstance()方法中检查null theInstance,是否可以一次又一次地重建?
PS - 我们正在使用Java 1.4上的App运行WebSphere 6
我知道Java中的双重锁定已被破坏,那么在Java中使单例线程安全的最佳方法是什么?我想到的第一件事是:
class Singleton{
private static Singleton instance;
private Singleton(){}
public static synchronized Singleton getInstance(){
if(instance == null) instance = new Singleton();
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
这有用吗?如果是这样,它是最好的方式(我想这取决于具体情况,所以说明特定技术最好的时候会有用)
所以,我总是像这样实现一个单例:
class Singleton {
private static $_instance = null;
public static function getInstance() {
if (self::$_instance === null) self::$_instance = new Singleton();
return self::$_instance;
}
private function __construct() { }
}
Run Code Online (Sandbox Code Playgroud)
但是,它最近让我感到震惊,我也可以使用成员方式的静态变量来实现它:
class Singleton {
public static function getInstance() {
//oops - can't assign expression here!
static $instance = null; // = new Singleton();
if ($instance === null) $instance = new Singleton();
return $instance;
}
private function __construct() { }
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这是更清洁,因为它不会使类混乱,我不需要做任何明确的存在检查,但因为我从来没有在其他任何地方见过这个实现,我想知道:
使用第二个实现比第一个实现有什么问题吗?
因此,经典简单的Singleton实现如下:
class Singleton
{
private:
static Singleton* singleton;
Singleton() {}
public:
static Singleton* getInstance();
};
Run Code Online (Sandbox Code Playgroud)
cpp文件:
Singleton* Singleton::singleton = 0;
Singleton* Singleton::getInstance()
{
if (!singleton)
{
singleton = new Singleton;
}
return singleton;
}
Run Code Online (Sandbox Code Playgroud)
我在这里看到的内存泄漏- "因为没有删除的新.但是在C++中没有静态析构函数,所以我们只是不关心这个内存泄漏?
可能重复:
为什么单例对象更面向对象?
为什么Scala对Singleton反模式有语言支持?如果Scala static从Java 继承了该关键字,那么该关键字的合法用例object是什么?
在Guice中,有什么区别:
// Inside your AbstractModule subclass:
@Override
public void configure() {
bind(Service.class).to(ServiceImpl.class).in(Singleton.class);
}
Run Code Online (Sandbox Code Playgroud)
和:
@Override
public void configure() {
bind(Service.class).to(ServiceImpl.class);
}
@Provides @Singleton
public ServiceImpl providesService() {
return new ServiceImpl();
}
Run Code Online (Sandbox Code Playgroud)
他们俩都一样吗?你什么时候使用一个?提前致谢.
我正在编写一个Android应用程序,它具有封装在内部库中的一些功能.但是,要使此功能起作用,库需要应用程序上下文的实例.给这个上下文库提供库的最佳方法是什么?我看到一些选项,其中没有一个吸引人:
Application并调用getApplicationContext()
Context每次获得对单例的引用时都会传递.
对于大多数人来说,如果他们想要使用Google Plus登录他们的应用程序,这似乎是一件简单的事情:s.
在活动1中:
我签了用户.
登录后,我想让该用户对象全局可访问,因此我将其添加到Application对象:
public class GlobalUserAccess extends Application {
private GoogleApiClient mGoogleApiClient;
public GlobalUserAccess(){
mGoogleApiClient = null;
}
public void setClient(GoogleApiClient client){
mGoogleApiClient = client;
}
public GoogleApiClient getClient(){
return mGoogleApiClient;
}
}
Run Code Online (Sandbox Code Playgroud)
通过这样绑定它:
GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
client.setClient(mGoogleApiClient);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在活动2中访问它时:
GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
String currentUser = Plus.AccountApi.getAccountName(client.getClient());
Run Code Online (Sandbox Code Playgroud)
我收到错误:
E/GMPM: getGoogleAppId failed with status: 10
Run Code Online (Sandbox Code Playgroud)
有人可以请你填写正确的方法来实现这个目标吗?我想让所有类都可以使用该用户对象,而且我花了太多时间在这上面:|.
我在某个地方搞砸了吗?啊...
编辑:活动1中的客户端创建代码
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.addScope(new Scope(Scopes.EMAIL))
.build();
Run Code Online (Sandbox Code Playgroud)
我直接从他们的Git存储库使用Googles代码.它成功登录并获取活动1中的帐户信息.
在查看Comparators类的源代码时,我遇到了这些代码行.
class Comparators {
//...
enum NaturalOrderComparator implements Comparator<Comparable<Object>> {
INSTANCE;
@Override
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c1.compareTo(c2);
}
@Override
public Comparator<Comparable<Object>> reversed() {
return Comparator.reverseOrder();
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
我想我明白这是做什么的.它是一个实现Comparator接口的Singleton实例.它使用实现Comparable接口的类的"compareTo"进行自然排序(如果我错了,请纠正我).
然而,我不明白为什么使用枚举完成它.我真的很喜欢Singletons的枚举,不要误会我的意思但在这种情况下我个人认为这会更简单:
public static final Comparator<Comparable<Object>> NATURAL_ORDER_COMPARATOR =
new Comparator<Comparable<Object>>() {
@Override
public int compare(Comparable<Object> c1, Comparable<Object> c2) {
return c1.compareTo(c2);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
除了个人偏好之外,还有任何理由使用枚举来实现吗?
我有一个主程序(main.cpp)和一个共享库(test.h和test.cpp):
test.h:
#include <stdio.h>
struct A {
A() { printf("A ctor\n"); }
~A() { printf("A dtor\n"); }
};
A& getA();
Run Code Online (Sandbox Code Playgroud)
TEST.CPP:
#include "test.h"
A& getA() {
static A a;
return a;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include "test.h"
struct B {
B() { printf("B ctor\n"); }
~B() { printf("B dtor\n"); }
};
B& getB() {
static B b;
return b;
}
int main() {
B& b = getB();
A& a = getA();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我在Linux上编译这些源代码的方法:
g++ …Run Code Online (Sandbox Code Playgroud)