// Singleton
// Intent: "Ensure a class only has one instance, and provide a global
// point of access to it".
// For further information, read "Design Patterns", p127, Gamma et al.,
// Addison-Wesley, ISBN:0-201-63361-2
/* Notes:
* If it makes sense to have only a single instance of a class (a so-called
* singleton), then it makes sense to enforce this (to elimintate potential
* errors, etc).
*
* A class based on the singleton design pattern protects its constructor,
* so that only the class itself (e.g. in a static method) may instantiate itself.
* It exposes an Instance method which allows client code to retrieve the
* current instance, and if it does not exist to instantiate it.
*/
namespace Singleton_DesignPattern
{
using System;
class Singleton
{
private static Singleton _instance;
public static Singleton Instance()
{
if (_instance == null)
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余2页未读,立即下载