1、目录大纲
- 创建型
- 结构型
- 行为型
2、创建型模式
非直接 new创建对象实例,创建型模式通过将实例化职责委托他方对象的方法,隔离客户程序与具体类型实例化的依赖关系。保证客户端程序获得期望具体类型实例,同时不发生直接的引用
Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
Factory Method(工厂模式):定义一个用于创建对象的接口,让子类决定将哪一个类实例化。Factory Method使一个类的实例化延迟到其子类。
Builder(生成器模式):将一个复杂对象的构件与它的表示分离,使得同样的构建过程可以创建不同的表述。
Prototype(原型模式):用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。
Singleton(单件模式):保证一个类仅有一个实例,并提供一个访问它的全局访问点。
2.1 简单工厂模式
/// <summary> /// 违反开闭原则,对扩展开放,对修改封闭 /// </summary> public class OperationFactory { public static OperationBase CreateOperation(string operate) { switch (operate) { case "+": return new OperationAdd(); case "-": return new OperationSub(); default: { throw new ArgumentNullException(); } } } }
2.2 工厂模式
定义一个用于创建对象的接口,让子类决定将哪一个类实例化。Factory Method使一个类的实例化延迟到其子类
public class FactoryMethod { public static void Main1() { DbHelper helper = new SqlserverDbHelper(); Database database=helper.CreateDatabase(); database.Show(); Console.Read(); } } public abstract class Database { public abstract void Show(); } public class SqlServer : Database { public override void Show() { Console.WriteLine("SqlServer"); } } public class Oracle : Database { public override void Show() { Console.WriteLine("Oracle"); } } public abstract class DbHelper { public abstract Database CreateDatabase(); public void DoResult() { Console.WriteLine("执行数据库操作"); } } public class SqlserverDbHelper : DbHelper { public override Database CreateDatabase() { return new SqlServer(); } }
2.3 抽象工厂模式
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类,与工厂模式的区别,子Factory实例化具体产品子类时,能创建一系列产品
public class AbstractFactory { public static void Main1() { ComputeFactory factory = new Compute1024(); factory.CreateCPU().Show(); factory.CreateDisk().Show(); Console.Read(); } internal abstract class ComputeFactory { public abstract CPU CreateCPU(); public abstract Disk CreateDisk(); } internal class Compute1024 : ComputeFactory { public override CPU CreateCPU() { return new CPUI386(); } public override Disk CreateDisk() { return new DiskXBSM(); } } internal class Computer5000 : ComputeFactory { public override CPU CreateCPU() { return new CPUII5(); } public override Disk CreateDisk() { return new DiskMicro(); } } //产品CPU internal abstract class CPU { public abstract void Show(); } internal sealed class CPUI386:CPU { public override void Show() { Console.WriteLine("CPUI386"); } } internal sealed class CPUII5 : CPU { public override void Show() { Console.WriteLine("CPUII5"); } } //产品硬盘 internal abstract class Disk { public abstract void Show(); } internal class DiskXBSM : Disk { public override void Show() { Console.WriteLine("西部数码"); } } internal class DiskMicro : Disk { public override void Show() { Console.WriteLine("微软"); } } }
2.4 建造者模式
将一个复杂对象的构件与它的表示分离,使得同样的构建过程可以创建不同的表述。指挥者和Builder之间关系是组合模式
internal class Builder { public static void Main1() { LogHelper helper = new LogHelper(); WriteLog(helper, 1, "binfire", "binfire"); Log log=helper.GetLog(); Console.WriteLine(log); Console.Read(); } private static void WriteLog(LogHelperBase logHelper, int id, string name, string reason) { logHelper.CreateId(id); logHelper.CreateName(name); logHelper.CreaetReason(reason); } } internal abstract class LogHelperBase { public abstract void CreateId(int id); public abstract void CreateName(string name); public abstract void CreaetReason(string reason); } internal class LogHelper : LogHelperBase { private Log _log = new Log(); public override void CreateId(int id) { _log.Id = id; } public override void CreateName(string name) { _log.Name = name; } public override void CreaetReason(string reason) { _log.Reason = reason; } public virtual Log GetLog() { return _log; } } internal class Log { public int Id { get; set; } public string Name { get; set; } public string Reason { get; set; } public override string ToString() { return string.Format("{0}:{1} {2}", Id, Name, Reason); } }
2.5 原型模式,减少内存中实例占用空间
public class Prototype { public Prototype GetCopy() { return this.MemberwiseClone() as Prototype; } }
2.6 单例模式
/// <summary> /// 饿汉式单例 /// </summary> sealed class Singleton1 //阻止发生派发,因为派发会产生多个实例 { private static readonly Singleton1 singleton = new Singleton1(); private Singleton1() { } public Singleton1 GetInstance() { return singleton; } }