如何使用Ruby中的Singleton设计模式创建一个Class? [英] How do I create a Class using the Singleton Design Pattern in Ruby?
本文介绍了如何使用Ruby中的Singleton设计模式创建一个Class?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单例模式是用于将类的实例化限制为一个对象实例的设计模式。虽然我知道如何用C ++和Java编写单例模式,但我想知道有没有人知道如何在Ruby中实现它?
The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object instance. Although I know how to code the singleton pattern in C++ and Java, I was wondering if anyone know how to implement it in Ruby?
推荐答案
p>其实上面的答案是不完全正确的。
Actually, the above answer was not completely correct.
require 'singleton'
class Example
include Singleton
end
您还需要包含require'singleton'陈述。
You also need to include the require 'singleton' statement.
这篇关于如何使用Ruby中的Singleton设计模式创建一个Class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文