对设计模式学了有一段的时间了,设计模式是软件工程的基石,他是人们反复使用的,程序员们几十年的经验总结,以“开放—封闭”、“单一职责”、“依赖倒转”、“里氏代换”、“合成聚合复用”、“迪米特法则”、“接口隔离”为基本原则。
本篇博客我对创建型模式进行总结,其中包括:单例模式、工厂方法模式、抽象工厂模式、建造者模式、原型模式。
1. 单例模式
这是最简单的一个模式,保证一个类仅有一个实例,并提供一个访问他的全局访问点。可以严格的控制客户怎样访问它以及何时访问它。简单地说就是对唯一实例的受控访问。它的秘密武器是创建私有的构造函数。(就像中国只能有一个,任何人不可分割)
UML图:
代码:
namespace 单例模式
{
class Class1
{
static void Main(string[] args)
{
Singleto s1 = Singleto.GetInstance();
Singleto s2 = Singleto.GetInstance();
//Singleto s3 = new Singleto();
if (s1 == s2)
{
Console.WriteLine("两个对象是相同的实例");
}
Console.Read();
}
}
class Singleto
{
private static Singleto instance;
private Singleto() //让构造方法私有,不能使用new实例化
{ }
public static Singleto GetInstance()
{
if (instance == null)
{
instance = new Singleto();
}
return instance;
}
}
}
2 . 工厂方法模式
定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。(就像服装生产工厂,有专门生产袜子的工厂,有专门生产内衣的工厂)
UML图:
代码:
namespace 工厂模式
{
class Class1
{
static void Main(string[] args)
{
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation();
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();
Console.WriteLine(result );
}
}
interface IFactory //工厂接口
{
Operation CreateOperation();
}
class AddFactory : IFactory //加法工厂
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
class SubFactory : IFactory //减法工厂
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
class MulFactory : IFactory //乘法工厂
{
public Operation CreateOperation()
{
return new OperationMul();
}
}
class DivFactory : IFactory //除法工厂
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
public class Operation //抽象运算类
{
private double _numberA = 0;
private double _numberB;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
public virtual double GetResult()
{
double result=0;
return result;
}
}
class OperationAdd : Operation //加法类
{
public override double GetResult()
{
double result=0;
result = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation //减法类
{
public override double GetResult()
{
double result=0;
result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation //乘法类
{
public override double GetResult()
{
double result=0;
result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation //除法类
{
public override double GetResult()
{
double result=0;
if (NumberB == 0)
throw new Exception("除数不能为0");
result = NumberA /NumberB;
return result;
}
}
}
3 . 抽象工厂模式
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类,创建不同的产品对象,客户端应使用不同的具体工厂(就像adidas工厂专门生产这个牌子的鞋、衣服、等装备,NIKE专门生产这个牌子的鞋、衣服、等装备)
UML图:
代码:
namespace 抽象工厂模式
{
class Class2
{
static void Main(string[] args)
{
User user = new User();
Department dept = new Department();
//IFactory factory = new SqlServerFactory2();
IFactory2 factory = new AccessFactory2();
IUser iu = factory.CreateUser();
iu.Insert(user);
iu.GetUser(1);
IDepartment id = factory.CreateDepartment();
id.Insert(dept);
id.GetDepartment(1);
Console.Read();
}
}
class Department
{
private int _id;
public int ID
{
get{return _id ;}
set{_id=value ;}
}
private string _deptName;
public string DeptName
{
get{return _deptName ;}
set{_deptName =value ;}
}
}
interface IDepartment
{
void Insert(Department department);
Department GetDepartment(int id);
}
class SqlserverDepartment : IDepartment
{
public void Insert(Department department)
{
Console.WriteLine("在SQL Server中给Department表增加一条记录");
}
public Department GetDepartment(int id)
{
Console.WriteLine("在SQL Server中根据ID得到Department表的一条记录");
return null;
}
}
class AccessDepartment : IDepartment
{
public void Insert(Department department)
{
Console.WriteLine("在Access中给Department表添加一条记录");
}
public Department GetDepartment(int id)
{
Console.WriteLine("在Access中根据ID得到Department表的一条记录");
return null;
}
}
interface IFactory2
{
IUser CreateUser();
IDepartment CreateDepartment();
}
class SqlServerFactory2 : IFactory2
{
public IUser CreateUser()
{
return new SqlserverUser1();
}
public IDepartment CreateDepartment()
{
return new SqlserverDepartment() ;
}
}
class AccessFactory2 : IFactory2
{
public IUser CreateUser()
{
return new AccessUser1();
}
public IDepartment CreateDepartment()
{
return new AccessDepartment();
}
}
interface IUser //用户接口
{
void Insert(User user);
User GetUser(int id);
}
class SqlserverUser1 : IUser
{
public void Insert(User user)
{
Console.WriteLine("在sql server中给User表增加一条记录");
}
public User GetUser(int id)
{
Console.WriteLine("在SQL Server中根据ID得到User表一条记录");
return null;
}
}
class AccessUser1 : IUser
{
public void Insert(User user)
{
Console.WriteLine("在Access表User中增加一条记录");
}
public User GetUser(int id)
{
Console.WriteLine("在Access中根据ID得到User表一条记录");
return null;
}
}
}
4. 建造者模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。(就像盖一个大楼,房地产商要先找挖地基的工人干活,再找盖楼的人干活,再找做外围包装的工人干活)
UML图:
代码:
namespace 建造者模式
{
class Class2
{
static void Main(string[] args)
{
Director2 director = new Director2();
Builder2 b1 = new ConcreateBuilder1();
Builder2 b2 = new ConcreateBuilder2();
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Show();
director.Construct(b2);
Product p2 = b2.GetResult();
p2.Show();
Console.Read();
}
}
class Product //产品类,由多个部件构成
{
IList<string> parts = new List<string>();
public void Add(string part)
{
parts.Add(part);
}
public void Show()
{
Console.WriteLine("\n产¨²品¡¤ 创ä¡ä建¡§—a—a");
foreach (string part in parts) //列出所有产品的部件
{
Console.WriteLine(part );
}
}
}
abstract class Builder2 //抽象建造者,确定建造者的组成部分
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
class ConcreateBuilder1 : Builder2 //具体建造者ConcreateBuilder1——工人
{
private Product product = new Product();
public override void BuildPartA()
{
product.Add("部件A");
}
public override void BuildPartB()
{
product.Add("部件B");
}
public override Product GetResult()
{
return product;
}
}
class ConcreateBuilder2 : Builder2 //具体建造者ConcreateBuilder2——工人
{
private Product product = new Product();
public override void BuildPartA()
{
product.Add("部件X");
}
public override void BuildPartB()
{
product.Add("部件Y");
}
public override Product GetResult()
{
return product;
}
}
class Director2 //指挥者类——设计师
{
public void Construct(Builder2 builder)
{ //用来指挥建造者的过程
builder.BuildPartA();
builder.BuildPartB();
}
}
}
5. 原型模式
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。其实就是从一个对象再创建另一个可定制的对象,而且不需要知道任何创建的细节。秘密武器:克隆自身(不知道大家见过做月饼的模子没有,就像它,用这个模子可以做出一个一样的东西,做出来之后还可以稍微修改一下成为你想要的形状)
UML图:
代码:
//9.5简历的浅复制
namespace 原型模式
{
class Class3
{
static void Main(string[] args)
{
Resume2 a = new Resume2("大鸟");
a.SetPersonalInfo("男","29");
a.SetWorkExpersonalInfo("1998-2000","XX公司");
Resume2 b = (Resume2)a.Clone();
b.SetWorkExpersonalInfo("2000-2006","YY公司");
Resume2 c = (Resume2)a.Clone();
c.SetPersonalInfo("男","33");
c.SetWorkExpersonalInfo("1900-1937","ZZ公司");
a.Display();
b.Display();
c.Display();
Console.Read();
}
}
class WorkExperience //工作经历
{
private string workDate;
public string WorkDate
{
get { return workDate; }
set { workDate = value; }
}
private string company;
public string Company
{
get { return company; }
set { company = value; }
}
}
class Resume2 : ICloneable //简历
{
private string name;
private string sex;
private string age;
private WorkExperience work;
public Resume2(string name)
{
this.name = name;
work = new WorkExperience();
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetWorkExpersonalInfo(string workDate, string company)
{
work.WorkDate = workDate;
work.Company = company;
}
public void Display()
{
Console.WriteLine("{0}{1}{2}",name,sex,age );
Console.WriteLine("工作经历:{0}{1}",work.WorkDate,work.Company );
}
/* MemberwiseClone 方法创建一个浅表副本,具体来说就是创建一个新的对象
* 然后将当前对象的非静态字段复制到该新对象,如果字段是子类型
* 则对该字段进行逐位的复制,如果是引用则复制引用但不复制
* 引用的对象;因此,原始对象及其副本引用同一对象
*/
public Object Clone()
{
return (Object)this.MemberwiseClone(); //浅复制,引用都指向同一个对象
}
}
}