单例模式是一种设计模式,用于将类的实例化限制为一个对象实例.虽然我知道如何用C++和Java编写单例模式,但我想知道是否有人知道如何在Ruby中实现它?
在Scala中,我可以定义一个抽象类并使用对象实现它:
abstrac class Base {
def doSomething(x: Int): Int
}
object MySingletonAndLiteralObject extends Base {
override def doSomething(x: Int) = x*x
}
Run Code Online (Sandbox Code Playgroud)
我在Python中的具体例子:
class Book(Resource):
path = "/book/{id}"
def get(request):
return aBook
Run Code Online (Sandbox Code Playgroud)
继承在这里没有意义,因为没有两个类可以具有相同的功能path.并且只需要一个实例,因此该类不会充当对象的蓝图.换句话说:这里不需要类Resource(Book在我的例子中),但是需要一个基类来提供通用功能.
我想:
object Book(Resource):
path = "/book/{id}"
def get(request):
return aBook
Run Code Online (Sandbox Code Playgroud)
Python 3的方法是什么?
我想知道在WPF中为每个应用程序提供给定Window的单个实例的最佳方法(阅读最优雅)是什么.
我是.NET和WPF的新手,我想出的东西看起来很蹩脚.
private static readonly Object MUTEX = new Object();
private static AboutWindow INSTANCE;
public static AboutWindow GetOrCreate() {
lock (MUTEX) {
if (INSTANCE == null) {
INSTANCE = new AboutWindow();
}
INSTANCE.Show();
return INSTANCE;
}
}
private AboutWindow() {
InitializeComponent();
}
private void AboutWindow_Closed(object sender, EventArgs e) {
// the Closed events are handy for me to update values across
// different windows.
lock (MUTEX) {
INSTANCE = null;
}
}
Run Code Online (Sandbox Code Playgroud)
事情是......这看起来像完全废话.必须有一些方法以更优雅的方式实现同一目标,对吗?
PS:我经常使用该Closed事件来更改其他打开窗口中的值.例如,我有SettingsWindow和"帐户"按钮.当我按下该按钮时,会弹出 …
嗨我有一个实现以前版本的iOS的单身如下:
.h文件
@interface CartSingleton : NSObject
{
}
+(CartSingleton *) getSingleton;
Run Code Online (Sandbox Code Playgroud)
.m文件
@implementation CartSingleton
static CartSingleton *sharedSingleton = nil;
+(CartSingleton *) getSingleton
{
if (sharedSingleton !=nil)
{
NSLog(@"Cart has already been created.....");
return sharedSingleton;
}
@synchronized(self)
{
if (sharedSingleton == nil)
{
sharedSingleton = [[self alloc]init];
NSLog(@"Created a new Cart");
}
}
return sharedSingleton;
}
//==============================================================================
+(id)alloc
{
@synchronized([CartSingleton class])
{
NSLog(@"inside alloc");
NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
sharedSingleton = [super alloc];
return …Run Code Online (Sandbox Code Playgroud) 他们说早期绑定解决了同步问题.我无法理解"如何".这对Java来说是特殊的还是同样适用于C++?
那么,使用这种方法我们实际上不需要互斥锁吗?

我有一个看起来像这样的课程:
public class Configurator {
private static Configurator INSTANCE = null;
private int maxRange = 1;
// many other properties; each property has a default value
private static synchronized Configurator getInstance() {
if(INSTANCE == null)
return new Configurator();
return INSTANCE;
}
public static int getMaxRange() {
getInstance().maxRange;
}
public static void setMaxRange(int range) {
getInstance().maxRange = range;
}
// Getters and setters for all properties follow this pattern
}
Run Code Online (Sandbox Code Playgroud)
它可以作为一个全局配置对象,可以在应用程序启动时设置,然后在整个项目中由几十个类使用:
// Called at app startup to configure everything
public class …Run Code Online (Sandbox Code Playgroud) 在DLL中AI有一个模板单例:
template <class T>
class Singleton
{
public:
static T &instance()
{
static T _instance;
return _instance;
}
private:
//All constructors are here
};
Run Code Online (Sandbox Code Playgroud)
在Dll BI中定义一个类Logger.Dlls C,D和E使用Logger,它的访问方式如下:
Singleton<Logger>::instance();
Run Code Online (Sandbox Code Playgroud)
问题是每个dll都实例化它自己的副本
Singleton<Logger>.
Run Code Online (Sandbox Code Playgroud)
而不是使用相同的单例实例.我知道这个问题的解决方案是使用extern模板.这就是d,C,D和E必须包含的内容
extern template class Singleton<Logger>;
Run Code Online (Sandbox Code Playgroud)
和dll B必须包括:
template class Singleton<Logger>;
Run Code Online (Sandbox Code Playgroud)
这仍然会导致创建多个模板实例.我尝试将extern放在所有的dll中但它仍然无法工作我尝试从所有dll中移除extern并且它仍然无法工作.这不是实现模板单例的标准方法吗?这样做的正确方法是什么?
我们可以在Java中打破单例模式的不同方法有哪些.我知道一种方式,即如果我们不在单例中同步方法,那么我们可以创建多个类的实例.因此应用同步.有没有办法打破单例java类.
public class Singleton {
private static Singleton singleInstance;
private Singleton() {
}
public static Singleton getSingleInstance() {
if (singleInstance == null) {
synchronized (Singleton.class) {
if (singleInstance == null) {
singleInstance = new Singleton();
}
}
}
return singleInstance;
}
}
Run Code Online (Sandbox Code Playgroud) Singleton无法自动装配SessionBean,但ScopedProxy可以.
假设100个用户在同一个应用程序中同时拥有一个有效的Session,ScopedProxy如何决定会话的含义?
我不认为ScopedProxy正在选择任何随机会话,这在我看来是无稽之谈.
NullPointerException发生吗?在类层次结构中的几个类之间共享对象实例的好方法是什么?我有以下情况:
class texture_manager;
class world {
...
std::vector<object> objects_;
skybox skybox_;
}
Run Code Online (Sandbox Code Playgroud)
我目前将texture_manager实现为单例,客户端从代码中的任何位置调用其实例化方法.texture_manager需要object在objects_向量中使用s skybox_,并且可能由其他类使用,这些类可能是也可能不是world类的一部分.由于我试图在我的代码中限制单例的使用,您是否建议使用此方法的替代方法?我想到的一个解决方案是将texture_manager引用作为参数传递给需要访问它的所有类的构造函数.谢谢.