Singleton
定义: 一个类仅有一个通过私有全局变量去创建的实例。
适用性: 1)客户端必须通过一个公用的端口获得仅存在的一个实例,例如一个局域网打印机系统,
只存在一个统一的控制所有客户端文档打印的端口和实例;
2)当仅存在的实例需要通过子类进行延伸,客户端能够使用拓展的实例,而不需要改动源码。
结构: Singleton
Static Instance() ----------- return uniqueInstance
SingletonOperation()
GetSignletonData()
static uniqueInstance
singletonData
用C#实现的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns
{
//创建的类
class Singleton
{
//私有的全局变量创建和应用仅存在的一个实例
private static Singleton _slnInstance = new Singleton();
public Singleton()
{
GetSingletonData();
}
public static Singleton GetInstance()
{
Console.WriteLine("获得实例/n");
return _slnInstance;
}
public void GetPersistence()
{
Console.WriteLine("获得类/n");
}
public void GetSingletonData()
{
Console.WriteLine("一个singleton");
}
}
}
Factory
定义: factory 按我的理解,就像瑞士军刀,刀把相当于程序的接口,各种不同的工具和同一个接口连
接。
适用性: 适用于系统可以提供和生产多个不同类别的产品。
用C#实现的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns
{
/*
* 创建一个接口,但是由子类来决定需要的类
*/
public interface ProductType
{
void Type();
}
public class Book : ProductType
{
public void Type()
{
Console.WriteLine("The type is book/n");
}
}
public class DVD : ProductType
{
public void Type()
{
Console.WriteLine("The type is DVD/n");
}
}
class Factory
{
public enum Products
{
aBook,
aDvD
}
public static ProductType getType(Products ps)
{
switch (ps)
{
case Products.aBook:
return new Book();
case Products.aDvD:
return new DVD();
default:
throw new NotImplementedException();
}
}
}
}
Facade
定义: 为一个子系统定义一个统一的接口,有利于调用子系统。
适用性: 对于一个子系统,例如一个被访问的自动取款机,用户只需要使用一个接口来进行
所有的操作。
用C#实现的代码
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.Facade
{
public interface CustomerDetail
{
string GetCustomerName();
string GetCustomerAddress();
}
public interface ProductDetail
{
string GetProductName();
string GetProductType();
}
public class Customer: CustomerDetail
{
private string _name;
private string _address;
public Customer(string name, string address)
{
_name = name;
_address = address;
}
public string GetCustomerName()
{ return _name; }
public string GetCustomerAddress()
{ return _address; }
}
public class Product : ProductDetail
{
private string _name;
private string _type;
public Product(string name, string type)
{
_name = name;
_type = type;
}
public string GetProductName()
{ return _name; }
public string GetProductType()
{ return _type; }
}
class facade: CustomerDetail, ProductDetail
{
private Customer _aCustomer;
private Product _aProduct;
public facade(Customer customer, Product product)
{
_aCustomer = customer;
_aProduct = product;
}
public string GetCustomerName()
{ return _aCustomer.GetCustomerName(); }
public string GetCustomerAddress()
{ return _aCustomer.GetCustomerAddress(); }
public string GetProductName()
{ return _aProduct.GetProductName(); }
public string GetProductType()
{ return _aProduct.GetProductType(); }
}
}
//program
/*
* Facade Pattern
*/
Console.WriteLine("Testing the facade pattern");
Facade.Customer customer = new Facade.Customer("Lincoln", "UC");
Facade.Product product = new Facade.Product("Call of duty", "game");
Facade.facade facade = new Facade.facade(customer, product);
Adapter
定义: 把一个类得接口转移成另外一个接口,允许两个类能够在一起运行。
和facade的比较: adapter 引用新的功能。
用C#实现的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns
{
class Adapter
{
private DateTime _date;
public Adapter(DateTime date)
{
_date = date;
}
public string GetDateBySlash()
{
return string.Format("{0}/{1}/{2}", _date.Day, _date.Month, _date.Year);
}
public string GetDateByDash()
{
return string.Format("{0}-{1}-{2}", _date.Day, _date.Month, _date.Year);
}
}
}
//Program.cs
/*
* Adapter
*/
Console.WriteLine("Testing the adapter pattern");
DateTime date = new DateTime();
Adapter adapter = new Adapter(date);
Console.WriteLine(adapter.GetDateByDash());
Console.WriteLine(adapter.GetDateBySlash());
Proxy
定义:提供另外一个代理接口来控制进入。
用C#实现的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns
{
public interface Iservice
{
string ServiceMethod(string request);
}
class Proxy: Iservice
{
private Iservice _remote;
public Proxy()
{
Console.WriteLine("Constructing proxy ... "
+ "(Basically this is where the connection to the remote is happenning, "
+ "and in real world the remote might obviously be on another machine)");
_remote = new Remote();
}
public void setSubject(Iservice service){ }
public string ServiceMethod (string request)
{
return _remote.ServiceMethod(request);
}
}
class Remote : Iservice
{
public string ServiceMethod(string request)
{
return "This is the service response to " + request + "/"";
}
}
}
引用:Design Patterns: Elements of Reusable Object-Oriented Software: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Addison-Wesley, 1994