Jeremy认为大多数开发者都没有正确的使用Ruby对象,所以他的话题比较高阶,谈论了诸如单例类(singleton classes)和instance_eval。
Jeremy believes that most programmers don't use Ruby's objects right, so his talk sheds some light onto advanced topics like singleton classes and the instance_eval.
这样分离出单例结构的代码,将有利于保持按单例模式使用类或不按单例模式使用类的灵活性。
This has the added benefit of separating singleton code from the class code leaving the flexibility to use several instances of the class or using the class as a singleton.
清单3 内的单例显然更为准确,但更为重要的一点是它使用了PHP 的LSB来引用适当的覆盖静态类。
The singleton in Listing 3 is certainly more concise, but what's important is the use of PHP's LSB to reference the appropriate overridden static attribute.
清单4显示了使用方法而非属性来指定适当类的单例。
Listing 4 shows a version of the singleton that USES a method instead of an attribute to specify the appropriate class.
尽管单例实现使用的是子类的类名,其他的模式(比如稍后介绍的活动记录模式)需要引用其他的静态属性。
Whereas the singleton implementation uses the subclass's class name, other patterns (such as active record, covered later) need to reference other static properties.
这是一个Scala单例(singleton),因为它使用对象声明,而不是类声明。
This is a Scala singleton because it USES the object declaration instead of a class declaration.
同时,如果使用创建单实例的方法实现单例模式,单例类还具有创建多个实例的变通能力。
Likewise, with a singleton implemented as single instance, you leave open the possibility of altering the class to create more instances in the future.
使用这个类,不需要复制代码就可以实现任意类单例化。
So with this class we can makeany class singleton, without duplicating the code.
使用类来实现单例模式可以确保在任何时间只有一个实例对象。
Classes implementing Singleton pattern will ensure that only one instance of the object ever exists at any one time.
什么时候使用带有静态成员的类,什么时候使用单例?
When would you use a class with static members and when would you use a Singleton class?
什么时候使用带有静态成员的类,什么时候使用单例?
When would you use a class with static members and when would you use a Singleton class?
应用推荐