新版设计模式手册[C#]
___________________________________________________________________________________
整理制作:Terrylee http://terrylee.cnblogs.com
目 录
一.创建型模式 .....................................................3
1.单件模式 .....................................................3
2.抽象工厂 .....................................................7
3.建造者模式 ..................................................14
4.工厂方法模式 ................................................21
5.原型模式 ....................................................27
二.结构型模式 ....................................................32
6.适配器模式 ..................................................32
7.桥接模式 ....................................................38
8.组合模式 ....................................................45
9.装饰模式 ....................................................51
10.外观模式 ...................................................58
11.享元模式 ...................................................64
12.代理模式 ...................................................71
三.行为型模式 ....................................................75
13.职责链模式 .................................................75
14.命令模式 ...................................................82
15.解释器模式 .................................................89
16.迭代器模式 .................................................95
17.中介者模式 ................................................102
18.备忘录模式 ................................................110
19.观察者模式 ................................................116
20.状态模式 ..................................................122
21.策略模式 ..................................................132
22.模版方法 ..................................................137
23.访问者模式 ................................................143
新版设计模式手册[C#]
一.创建型模式
1.单件模式
结构图
生活例子
意图
保证一个类仅有一个实例,并提供一个访问它的全局访问点。
适用性
z 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
z 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一
个扩展的实例时。
示意性代码
// Singleton pattern -- Structural example
using System;
namespace DoFactory.GangOfFour.Singleton.Structural
{
// MainApp test application
class MainApp
{
static void Main()
___________________________________________________________________________________
整理制作:Terrylee http://terrylee.cnblogs.com
新版设计模式手册[C#]
___________________________________________________________________________________
整理制作:Terrylee http://terrylee.cnblogs.com
{
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.Instance();
Singleton s2 = Singleton.Instance();
if (s1 == s2)
{
Console.WriteLine("Objects are the same instance");
}
// Wait for user
Console.Read();
}
}
// "Singleton"
class Singleton
{
private static Singleton instance;
// Note: Constructor is 'protected'
protected Singleton()
{
}
public static Singleton Instance()
{
// Use 'Lazy initialization'
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
实际应用
// Singleton pattern -- Real World example
using System;
using System.Collections;
using System.Threading;
新版设计模式手册[C#]
___________________________________________________________________________________
整理制作:Terrylee http://terrylee.cnblogs.com
namespace DoFactory.GangOfFour.Singleton.RealWorld
{
// MainApp test application
class MainApp
{
static void Main()
{
LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
LoadBalancer b4 = LoadBalancer.GetLoadBalancer();
// Same instance?
if (b1 == b2 && b2 == b3 && b3 == b4)
{
Console.WriteLine("Same instance\n");
}
// All are the same instance -- use b1 arbitrarily
// Load balance 15 server requests
for (int i = 0; i < 15; i++)
{
Console.WriteLine(b1.Server);
}
// Wait for user
Console.Read();
}
}
// "Singleton"
class LoadBalancer
{
private static LoadBalancer instance;
private ArrayList servers = new ArrayList();
private Random random = new Random();
// Lock synchronization object
private static object syncLock = new object();
评论0