什么时候不应该使用单身课,尽管这样做可能很诱人?如果我们列出最常见的"单发性炎症",我们应该注意避免,这将是非常好的.
我有一个使用"静态只读T实例=新T();"的单例 图案.但是,我遇到了T是一次性的情况,实际上需要处理以进行单元测试.如何修改此模式以支持一次性单件?
我想要的界面是这样的:
var x = Foo.Instance;
var y = Foo.Instance; // x == y
...
x.Release(); // this causes the next Foo.Instance to return a fresh object
// also, it assumes no further operations on x/y will be performed.
Run Code Online (Sandbox Code Playgroud)
注意 - 模式当然必须是线程安全的.
编辑 - 出于生产代码的目的,这是一个真正的单身人士.问题是它锁定了一些文件,因此在单元测试中我们必须处理它.
如果可能的话,我也更喜欢可以重复使用的模式.
在java中编写单例的经典之处如下:
public class SingletonObject
{
private SingletonObject()
{
}
public static SingletonObject getSingletonObject()
{
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;
}
private static SingletonObject ref;
}
Run Code Online (Sandbox Code Playgroud)
如果我们需要在多线程情况下运行,我们可以添加synchronized关键字.
但我更喜欢把它写成:
public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}
public static SingletonObject getSingletonObject()
{
return ref;
}
private static SingletonObject ref = new SingletonObject();
}
Run Code Online (Sandbox Code Playgroud)
我认为这更简洁,但奇怪的是我没有看到以这种方式编写的任何示例代码,如果我以这种方式编写代码会有什么不良影响吗?
我环顾四周,我需要一个适用于2个或更多C++/CLI文件的Singleton类的示例.
如何在C++/CLI中声明单例,而不是C#?
如何在两个或多个C++/CLI文件中共享该单例?
当我尝试共享该单例时,我不断获得可变重定义.
如果一个类包含指向单例类的指针,它可以aggregation吗?
据我所知,它不能成为一种has-a关系,因为类不会创建单例类的实例,它只是像association关系一样使用它.
我正在使用一个多线程的c#应用程序,它正在使用WCF Web服务.与webservice的连接将具有我们可以定义的特定超时,然后它将关闭.我希望使用singleton类存储与Web服务的连接.我试图得到如下实例:
CLazySingleton ins = CLazySingleton.Instance;
string connection = CLazySingleton.abc;
Run Code Online (Sandbox Code Playgroud)
下面是单例类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LazySingleton
{
public class CLazySingleton
{
private static readonly Lazy<CLazySingleton> _instance
= new Lazy<CLazySingleton>(() => new CLazySingleton());
private static readonly object ThreadLock = new object();
public static string abc;
//I will use the service connection object in place of 'abc' in the application
//assume that 'abc' is storing the connection object
private CLazySingleton()
{ }
public static CLazySingleton Instance
{ …Run Code Online (Sandbox Code Playgroud) 我创建一个父类来处理带有智能指针的单例模式:
.h文件:
template<class singleType>
class Singleton
{
public:
static std::shared_ptr<singleType> GetInstance();
private:
static std::weak_ptr<singleType> m_singleObject;
};
Run Code Online (Sandbox Code Playgroud)
.cpp文件:
template<class singleType>
std::shared_ptr<singleType> Singleton<singleType>::GetInstance()
{
auto shareObject = m_singleObject.Lock();
if (!shareObject)
{
shareObject.reset(new singleType);
m_singleObject = shareObject;
}
return shareObject;
}
Run Code Online (Sandbox Code Playgroud)
不确定这是使用智能指针的正确方法吗?任何的想法?
非常感谢
我一直试图找出在Laravel中设置服务提供商时的区别app->bind和app->singleton区别.我的印象是,如果我注册singleton它,每次调用它时都会返回相同的对象实例,bind这将是一个新实例.
这是一个简单的例子:
正面:
use Illuminate\Support\Facades\Facade;
class DataFacade extends Facade
{
protected static function getFacadeAccessor() {
return 'Data';
}
}
Run Code Online (Sandbox Code Playgroud)
服务提供者:
use Illuminate\Support\ServiceProvider;
class DataServiceProvider extends ServiceProvider
{
public function register() {
$this->app->singleton('Data', function() {
return new Data;
});
}
}
Run Code Online (Sandbox Code Playgroud)
类:
class Data
{
public $data = [];
public function get($key)
{
return isset($this->data[$key]) ? $this->data[$key] : null;
}
public function set($key, $val)
{
$this->data[$key] = $val;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我们做类似的事情:
$instance = …Run Code Online (Sandbox Code Playgroud)