我只需要在某处阅读以下代码:
public class SingletonObjectDemo {
private static SingletonObjectDemo singletonObject;
// Note that the constructor is private
private SingletonObjectDemo() {
// Optional Code
}
public static SingletonObjectDemo getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
return singletonObject;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要知道这部分需要什么:
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
Run Code Online (Sandbox Code Playgroud)
如果我们不使用这部分代码怎么办?仍然会有一个副本SingletonObjectDemo,为什么我们需要这个代码呢?
我有 ac# 网络服务。当我收到新请求时,我创建一个日志记录实例。我有许多其他类的实例来处理请求,我希望它们也记录下来。共享日志记录实例而不将其传递到构造函数或属性中的最佳方法是什么?
我有共同的代码(我称之为控制器的多个类)需要由项目中的多个包共享.我正在考虑创建一个返回这些控制器的工厂.因此,工厂将有一个hashmap,可以返回请求的控制器,如果没有创建,则创建一个新的.控制器有公共代码,因为我不需要创建这些控制器的多个实例,我认为它们应该是单例.
这看起来像是一个好方法吗?
我有这个:
单例.h
#ifndef SINGLETON_H
#define SINGLETON_H
#include <atomic>
#include <mutex>
class Singleton
{
public:
static std::atomic<Singleton*> Singleton::m_instance;
static std::mutex Singleton::m_mutex;
static Singleton* getInstance();
Singleton();
~Singleton();
};
#endif
Run Code Online (Sandbox Code Playgroud)
单例.cpp
#include "Singleton.h"
Singleton::Singleton()
{
}
Singleton* Singleton::getInstance()
{
Singleton* tmp = m_instance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (tmp == nullptr)
{
std::lock_guard<std::mutex> lock(m_mutex);
tmp = m_instance.load(std::memory_order_relaxed);
if (tmp == nullptr)
{
tmp = new Singleton;
std::atomic_thread_fence(std::memory_order_release);
m_instance.store(tmp, std::memory_order_relaxed);
}
}
return tmp;
}
Singleton::~Singleton() {}
Run Code Online (Sandbox Code Playgroud)
主程序
#include "Singleton.h"
#include <iostream>
int main()
{
Singleton* singleton …Run Code Online (Sandbox Code Playgroud) 我正在使用弹性搜索传输客户端连接到弹性搜索.我尝试过两种方法
1)在我的整个应用程序中共享Singleton客户端.响应时间在1-2秒之间
2)每次调用Elastic Search的新客户端实例,大约需要7秒才能响应.具体而言,有5个类需要连接ES群集,此方法为每个类创建一个新的传输客户端.
1)在弹性搜索方面是一个很好的方法,因为通常不建议使用单例数据库连接对象吗?
是否有可用于Elastic Search的连接池机制,就像我们有关系数据库的DBCP一样?
我有一个sinlgeton对象,它持有一个方法,巫婆不同步.单个用户可以一次访问单例 - 如果多个客户端访问该对象会发生什么?是否会以先来先服务的方式提供对象引用...也就是说,一个客户端是否必须等待第一个完成对象,或者它将在内存中被赋予相同的对象引用?
我对单例中未同步的方法感到奇怪.如果2个客户端使用不同的参数调用Singleton.method(param) - 它们不会为彼此创建问题吗?
我14岁,已经学习了大约4/5个月的java.我正在编写一个现在叫做super mario winshine的游戏,我想知道一个主要是静态变量的类是否是一个好习惯.
该类是保存游戏关卡/世界的所有信息的类.由于我只需要这个类的一个版本,并且许多其他类将使用它,我选择将所有变量设置为静态.这是好习惯吗?
我已经考虑过这样一个事实:我可以保持变量"非静态"并且只是克隆我用于该类的主要对象,但我认为在这种情况下我宁愿为内存牺牲"OO".
我很困惑:在AS3中,为什么我们保留Singleton类构造函数public而不是private像Java一样?如果我们保留构造函数public,那么我们可以直接从外部访问它!
请查看此示例中的MODEL部分.
由于您无法在静态函数中使用$ this->,您应该如何访问静态函数中的常规函数?
private function hey()
{
return 'hello';
}
public final static function get()
{
return $this->hey();
}
Run Code Online (Sandbox Code Playgroud)
这会引发错误,因为你不能在静态内使用$ this->.
private function hey()
{
return 'hello';
}
public final static function get()
{
return self::hey();
}
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
Non-static method Vote::get() should not be called statically
Run Code Online (Sandbox Code Playgroud)
如何在静态方法中访问常规方法?在同一个班*
我在一些博客中读到单例妨碍测试,因为它导致高耦合,而模拟不能代替它,所以解决方案是实现一个接口并在参数中传递它.我没有博客链接,一旦找到它就会附上它.但由于static getInstance()方法不可能.
那么在Singleton Pattern中实现接口有什么优势吗?
public interface SingletonInterface{
//some methods
}
public class Singleton1 implements SingletonInterface{
//Usual singleton methods
}
Run Code Online (Sandbox Code Playgroud) java ×5
singleton ×4
oop ×2
static ×2
actionscript ×1
apache-flex ×1
c# ×1
c++ ×1
c++11 ×1
dbconnection ×1
factory ×1
flex3 ×1
instance ×1
interface ×1
java-api ×1
lazy-loading ×1
logging ×1
methods ×1
php ×1
stdatomic ×1