单例模式完整的实现
来源:互联网 发布:java注解不属于 编辑:程序博客网 时间:2024/05/21 17:48
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
public sealed class Singleton1
{
private Singleton1()
{
}
private static Singleton1 instance = null;
public static Singleton1 Instance
{
get
{
if (instance == null)
instance = new Singleton1();
return instance;
}
}
}
public sealed class Singleton2
{
private Singleton2()
{
}
private static readonly object syncObj = new object();
private static Singleton2 instance = null;
public static Singleton2 Instance
{
get
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton2();
}
return instance;
}
}
}
public sealed class Singleton3
{
private Singleton3()
{
}
private static object syncObj = new object();
private static Singleton3 instance = null;
public static Singleton3 Instance
{
get
{
if (instance == null)
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton3();
}
}
return instance;
}
}
}
public sealed class Singleton4
{
private Singleton4()
{
}
public static void Print()
{
Console.WriteLine("Singleton4 Print");
}
//静态构造函数
private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
}
//实现按需创建实例
public sealed class Singleton5
{
Singleton5()
{
}
public static void Print()
{
Console.WriteLine("Singleton5 Print");
}
public static Singleton5 Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
static Nested()
{
}
internal static readonly Singleton5 instance = new Singleton5();
}
}
class Program
{
static void Main(string[] args)
{
Singleton4.Print();
Singleton5.Print();
}
}
}
// 著作权所有者:何海涛
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
public sealed class Singleton1
{
private Singleton1()
{
}
private static Singleton1 instance = null;
public static Singleton1 Instance
{
get
{
if (instance == null)
instance = new Singleton1();
return instance;
}
}
}
public sealed class Singleton2
{
private Singleton2()
{
}
private static readonly object syncObj = new object();
private static Singleton2 instance = null;
public static Singleton2 Instance
{
get
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton2();
}
return instance;
}
}
}
public sealed class Singleton3
{
private Singleton3()
{
}
private static object syncObj = new object();
private static Singleton3 instance = null;
public static Singleton3 Instance
{
get
{
if (instance == null)
{
lock (syncObj)
{
if (instance == null)
instance = new Singleton3();
}
}
return instance;
}
}
}
public sealed class Singleton4
{
private Singleton4()
{
}
public static void Print()
{
Console.WriteLine("Singleton4 Print");
}
//静态构造函数
private static Singleton4 instance = new Singleton4();
public static Singleton4 Instance
{
get
{
return instance;
}
}
}
//实现按需创建实例
public sealed class Singleton5
{
Singleton5()
{
}
public static void Print()
{
Console.WriteLine("Singleton5 Print");
}
public static Singleton5 Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
static Nested()
{
}
internal static readonly Singleton5 instance = new Singleton5();
}
}
class Program
{
static void Main(string[] args)
{
Singleton4.Print();
Singleton5.Print();
}
}
}
0 0
- 单例模式完整的实现
- 单例的完整实现
- 完整单例模式
- 完整单例模式的写法
- 单例模式最完整的讲解
- 一个完整的单例模式代码
- objective-c中的Singleton单例模式的完整实现示例
- objective-c中的Singleton单例模式的完整实现示例
- objective-c中的Singleton单例模式的完整实现示例
- objective-c中的Singleton单例模式的完整实现示例
- objective-c中的Singleton单例模式的完整实现示例
- GCD单例,完整的单例实现
- 更完整的单例模式(java, kotlin)
- 单例模式的实现
- 单例模式的实现
- 单例模式的实现
- 单例模式的实现
- 单例模式的实现
- 909422229________【个人理解】编译时多态与运行时多态
- prototype 是什么东西,原型链的理解,什么时候用 prototype
- git常用操作
- Java线程(八)----锁对象Lock-同步问题更完美的处理方式
- 如何实现浏览器兼容版的element.dataset
- 单例模式完整的实现
- java中的instanceof用法
- Hexo+GitHub创建网站
- Servlet之response
- Android SharedPreferences的使用.
- JS函数里的this的含义,什么情况下怎么用
- android.cts.security.SELinuxNeverallowRulesTest#testNeverallowRules**
- Hbase单机模式安装和使用
- javaweb项目中遇到的一些乱码问题