模式分类
从目的来看:
– 创建型(Creational)模式:负责对象创建。
– 结构型(Structural)模式:处理类与对象间的组合。
– 行为型(Behavioral)模式:类与对象交互中的职责分配。
从范围来看:
– 类模式处理类与子类的静态关系。
– 对象模式处理对象间的动态关系。
动机(Motivation)
在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性、
以及良好的效率。
如何绕过常规的构造器,提供一种机制来保证一个类只有
一个实例?
这应该是类设计者的责任,而不是使用者的责任。
意图(Intent)
保证一个类仅有一个实例,并提供一个该实例的全局访问点。——《设计模式》GoF
结构(Structure)
单线程Singleton 模式实现
单线程Singleton模式
/// <summary>
/// 单线程Singleton模式
/// </summary>
public class Class1
{
private static Class1 c1;
private Class1() {}
public static Class1 C1
{
get
{
if (c1 == null)
{
c1 = new Class1();
}
return c1;
}
}
}
/// 单线程Singleton模式
/// </summary>
public class Class1
{
private static Class1 c1;
private Class1() {}
public static Class1 C1
{
get
{
if (c1 == null)
{
c1 = new Class1();
}
return c1;
}
}
}
单线程Singleton模式的几个要点
- Singleton模式中的实例构造器可以设置为protected以允许子类派生。
- Singleton模式一般不要支持ICloneable接口,因为这可能会导致多个对象实例,与Singleton模式的初衷违背。
- Singleton模式一般不要支持序列化,因为这也有可能导致多个对象实例,同样与Singleton模式的初衷违背。
- Singletom模式只考虑到了对象创建的管理,没有考虑对象销毁的管理。就支持垃圾回收的平台和对象的开销来讲,我们一般没有必要对其销毁进行特殊的管理。
- 不能应对多线程环境:在多线程环境下,使用Singleton模式仍然有可能得到Singleton类的多个实例对象。
多线程Singleton 模式实现
多线程Singleton模式
/// <summary>
/// 多线程Singleton模式,方法1
/// </summary>
public class Class2
{
public static readonly Class2 C2 = new Class2();
private Class2(){}
}
/// <summary>
//多线程Singleton模式,方法1,相当于这样
/// </summary>
public class Class3
{
public static Class3 c3;
static Class3()
{
c3 = new Class3();
}
private Class3(){}
}
/// <summary>
/// 多线程Singleton模式,方法2
/// </summary>
public class Class4
{
private static volatile Class4 c4 = null;
private static object lockHelper = new object();
private Class4() { }
public static Class4 C4()
{
if (c4 == null)
{
lock (lockHelper)
{
if (c4 == null)
{
c4 = new Class4();
}
}
}
return c4;
}
}
/// 多线程Singleton模式,方法1
/// </summary>
public class Class2
{
public static readonly Class2 C2 = new Class2();
private Class2(){}
}
/// <summary>
//多线程Singleton模式,方法1,相当于这样
/// </summary>
public class Class3
{
public static Class3 c3;
static Class3()
{
c3 = new Class3();
}
private Class3(){}
}
/// <summary>
/// 多线程Singleton模式,方法2
/// </summary>
public class Class4
{
private static volatile Class4 c4 = null;
private static object lockHelper = new object();
private Class4() { }
public static Class4 C4()
{
if (c4 == null)
{
lock (lockHelper)
{
if (c4 == null)
{
c4 = new Class4();
}
}
}
return c4;
}
}
单,多线程Singleton模式.构造函数带参数
单,多线程Singleton模式.构造函数带参数
/// <summary>
/// 单线程Singleton模式,构造函数带参数
/// </summary>
public class Class5
{
int _x, _y;
private static Class5 c5;
private Class5(int x, int y)
{
this._x = x;
this._y = y;
}
public static Class5 getClass5(int x, int y)
{
if (c5 == null)
{
c5 = new Class5(x, y);
}
else
{
c5._x = x;
c5._y = y;
}
return c5;
}
}
/// <summary>
/// 多线程Singleton模式,方法2,构造函数带参数
/// </summary>
public class Class6
{
private static volatile Class6 c6 = null;
private static object lockHelper = new object();
int _x, _y;
private Class6(int x, int y)
{
this._x = x;
this._y = y;
}
public static Class6 C6(int x, int y)
{
if (c6 == null)
{
lock (lockHelper)
{
if (c6 == null)
{
c6 = new Class6(x, y);
}
}
}
else
{
c6._x = x;
c6._y = y;
}
return c6;
}
}
/// <summary>
/// 多线程Singleton模式,方法1,构造函数带参数.
/// </summary>
public class Class7
{
public static readonly Class7 c7 = new Class7();
int _x, _y;
private Class7()
{
}
public void Init(int x, int y)
{
this._x = x;
this._y = y;
}
//或者
public int x
{
get
{
return this._x;
}
set
{
this._x = value;
}
}
public int y
{
get
{
return this._y;
}
set
{
this._y = value;
}
}
}
/// 单线程Singleton模式,构造函数带参数
/// </summary>
public class Class5
{
int _x, _y;
private static Class5 c5;
private Class5(int x, int y)
{
this._x = x;
this._y = y;
}
public static Class5 getClass5(int x, int y)
{
if (c5 == null)
{
c5 = new Class5(x, y);
}
else
{
c5._x = x;
c5._y = y;
}
return c5;
}
}
/// <summary>
/// 多线程Singleton模式,方法2,构造函数带参数
/// </summary>
public class Class6
{
private static volatile Class6 c6 = null;
private static object lockHelper = new object();
int _x, _y;
private Class6(int x, int y)
{
this._x = x;
this._y = y;
}
public static Class6 C6(int x, int y)
{
if (c6 == null)
{
lock (lockHelper)
{
if (c6 == null)
{
c6 = new Class6(x, y);
}
}
}
else
{
c6._x = x;
c6._y = y;
}
return c6;
}
}
/// <summary>
/// 多线程Singleton模式,方法1,构造函数带参数.
/// </summary>
public class Class7
{
public static readonly Class7 c7 = new Class7();
int _x, _y;
private Class7()
{
}
public void Init(int x, int y)
{
this._x = x;
this._y = y;
}
//或者
public int x
{
get
{
return this._x;
}
set
{
this._x = value;
}
}
public int y
{
get
{
return this._y;
}
set
{
this._y = value;
}
}
}
测试代码:
代码
private void button1_Click(object sender, System.EventArgs e)
{
Class1 c1 = Class1.C1;
Class2 c2 = Class2.C2;
Class3 c3 = Class3.c3;
Class4 c4 = Class4.C4();
Class5 c5 = Class5.getClass5(3, 4);
Class6 c6 = Class6.C6(3, 4);
Class7 c7 = Class7.c7;
c7.Init(3, 4);
c7.x = 3;
c7.y = 4;
Class1 cc1 = Class1.C1;
Class2 cc2 = Class2.C2;
Class3 cc3 = Class3.c3;
Class4 cc4 = Class4.C4();
Class5 cc5 = Class5.getClass5(3, 4);
Class6 cc6 = Class6.C6(3, 4);
Class7 cc7 = Class7.c7;
cc7.Init(3, 4);
cc7.x = 9;
cc7.y = 10;
if (c1 == cc1 && c2 == cc2 && c3 == cc3 && c4 == cc4
&& c5 == cc5 && c6 == cc6 && c7 == cc7)
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
MessageBox.Show(c7.x.ToString());
MessageBox.Show(cc7.x.ToString());
}
{
Class1 c1 = Class1.C1;
Class2 c2 = Class2.C2;
Class3 c3 = Class3.c3;
Class4 c4 = Class4.C4();
Class5 c5 = Class5.getClass5(3, 4);
Class6 c6 = Class6.C6(3, 4);
Class7 c7 = Class7.c7;
c7.Init(3, 4);
c7.x = 3;
c7.y = 4;
Class1 cc1 = Class1.C1;
Class2 cc2 = Class2.C2;
Class3 cc3 = Class3.c3;
Class4 cc4 = Class4.C4();
Class5 cc5 = Class5.getClass5(3, 4);
Class6 cc6 = Class6.C6(3, 4);
Class7 cc7 = Class7.c7;
cc7.Init(3, 4);
cc7.x = 9;
cc7.y = 10;
if (c1 == cc1 && c2 == cc2 && c3 == cc3 && c4 == cc4
&& c5 == cc5 && c6 == cc6 && c7 == cc7)
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
MessageBox.Show(c7.x.ToString());
MessageBox.Show(cc7.x.ToString());
}