23种设计模式--创建型模式
1.单例模式:
#region 1.单例模式(Singleton),确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类。所以单例模式应该具备以下几点①私有化的构造函数 ②私有化的实例成员对象 ③对外提供获取当前实例的方法。 public class HungerSingleton { private static HungerSingleton instance = new HungerSingleton(); private HungerSingleton() { } public HungerSingleton GetHungerSingleton() { return instance; } } public class LazySingleton { private LazySingleton instance = null; private Object objLock = new object(); private LazySingleton() { } public LazySingleton GetLazySingleton() { if (instance == null) { lock (objLock) { if (instance == null) { instance = new LazySingleton(); } } } return instance; } } #endregion
2.简单工厂模式:
#region 2.简单工厂模式(Simple Factory)。定义一个工厂类,它可以根据参数的不同返回不同类的实例,被创建的实例通常具有共同的父类。一般简单工厂的创建实例的方法都是一个静态方法。 public class ChartFactory { public static IChartable GetChart(string type) { IChartable chart = null; if (type.Equals("histogram", StringComparison.OrdinalIgnoreCase)) ; { chart = new HistogramChart(); Console.WriteLine("初始化柱状图完成..."); } if (type.Equals("line", StringComparison.OrdinalIgnoreCase)) ; { chart = new LineChart(); Console.WriteLine("初始化折线图完成..."); } if (type.Equals("pie", StringComparison.OrdinalIgnoreCase)) ; { chart = new PieChart(); Console.WriteLine("初始化饼状图完成..."); } return chart; } } #endregion
3.工厂方法:
#region 3.工厂方法(Factory Method)。 定义一个用于创建对象的接口,让子类决定将哪一个类实例化。 /// <summary> /// 抽象产品 /// </summary> public interface ILogger { void WriteLog(); } #region 具体产品 public class FileLogger : ILogger { public void WriteLog() { Console.WriteLine("文件日志记录...."); } } public class DatabaseLogger : ILogger { public void WriteLog() { Console.WriteLine("数据库日志记录..."); } } #endregion /// <summary> /// 抽象工厂 /// </summary> public interface ILoggerFactory { ILogger CreateLogger(); } /// <summary> /// 具体工厂 /// </summary> public class FileLoggerFactory : ILoggerFactory { public ILogger CreateLogger() { ILogger logger = new FileLogger(); return logger; } } /// <summary> /// 具体工厂 /// </summary> public class DatabaseLoggerFactory : ILoggerFactory { public ILogger CreateLogger() { ILogger logger = new DatabaseLogger(); return logger; } } #endregion
4.抽象工厂:
1 #region 4.抽象工厂(Abstract Factory)。其实就是工厂方法的加强版,区别在于工厂方法生产一类产品,抽象工厂生产多种类的产品。 2 #region 抽象产品 3 public interface IButton 4 { 5 void Display(); 6 } 7 8 public interface ITextField 9 { 10 void Display(); 11 } 12 13 public interface IComboBox 14 { 15 void Display(); 16 } 17 18 19 #endregion 20 21 22 #region 具体产品 23 public class SpringButton : IButton 24 { 25 public void Display() 26 { 27 Console.WriteLine("显示浅绿色按钮..."); 28 } 29 } 30 31 public class SpringTextField : ITextField 32 { 33 public void Display() 34 { 35 Console.WriteLine("显示绿色边框文本框..."); 36 } 37 } 38 39 public class SpringComboBox : IComboBox 40 { 41 public void Display() 42 { 43 Console.WriteLine("显示绿色边框下拉框..."); 44 } 45 } 46 47 public class SummerButton : IButton 48 { 49 public void Display() 50 { 51 Console.WriteLine("显示浅蓝色按钮..."); 52 } 53 } 54 55 public class SummerTextField : ITextField 56 { 57 public void Display() 58 { 59 Console.WriteLine("显示蓝色边框文本框..."); 60 } 61 } 62 63 public class SummerComboBox : IComboBox 64 { 65 public void Display() 66 { 67 Console.WriteLine("显示蓝色边框下拉框..."); 68 } 69 } 70 71 #endregion 72 73 #region 抽象工厂 74 public interface ISkinFactory 75 { 76 IButton CreateButton(); 77 ITextField CreateTextField(); 78 IComboBox CreateComboBox(); 79 } 80 #endregion 81 82 #region 具体工厂 83 84 // Spring皮肤工厂 85 public class SpringSkinFactory : ISkinFactory 86 { 87 public IButton CreateButton() 88 { 89 return new SpringButton(); 90 } 91 92 public IComboBox CreateComboBox() 93 { 94 return new SpringComboBox(); 95 } 96 97 public ITextField CreateTextField() 98 { 99 return new SpringTextField(); 100 } 101 } 102 103 104 public class SummerSkinFactory : ISkinFactory 105 { 106 public IButton CreateButton() 107 { 108 return new SummerButton(); 109 } 110 111 public IComboBox CreateComboBox() 112 { 113 return new SummerComboBox(); 114 } 115 116 public ITextField CreateTextField() 117 { 118 return new SummerTextField(); 119 } 120 } 121 122 #endregion 123 124 #endregion
5.原型模式:
1 #region 5.原型模式(Prototype)。使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 2 3 /// <summary> 4 /// 这里面引入一个浅复制和深复制的概念 5 /// 浅复制,复制一个对象的时候,仅仅复制原始对象中所有的非静态类型成员和所有的引用类型成员的引用。 6 /// 深复制,复制一个对象的时候,不仅复制所有非静态类型成员,还要复制所有引用类型成员的实际对象。 7 /// </summary> 8 9 public class WeeklyLog : ICloneable 10 { 11 public string Name { get; set; } 12 public string Date { get; set; } 13 public string Content { get; set; } 14 public object Clone() 15 { 16 WeeklyLog result = new WeeklyLog(); 17 result.Name = this.Name; 18 result.Date = this.Date; 19 result.Content = this.Content; 20 return result; 21 } 22 } 23 24 #endregion
6.建造者模式:
1 #region 6.建造者模式(Builder)。将一个对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。 建造者模式和抽象工厂模式很相似,工厂模式是通过工厂生产一系列的产品,而建造者模式是通过建造者获得一个完整的对象。 2 /// <summary> 3 /// 产品 4 /// </summary> 5 public class Actor 6 { 7 public string Type { get; set; } 8 public string Head { get; set; } 9 public string Body { get; set; } 10 public string Cloth { get; set; } 11 } 12 /// <summary> 13 /// 抽象建造者 14 /// </summary> 15 public abstract class ActorBuilder 16 { 17 protected Actor actor = new Actor(); 18 public abstract void BuildType(); 19 public abstract void BuildHead(); 20 public abstract void BuildBody(); 21 public abstract void BuildCloth(); 22 23 public Actor CreateActor() 24 { 25 return actor; 26 } 27 } 28 29 /// <summary> 30 /// 具体建造者 31 /// </summary> 32 public class DiaosiBuilder : ActorBuilder 33 { 34 public override void BuildBody() 35 { 36 this.actor.Body = "干瘦的身体"; 37 } 38 39 public override void BuildCloth() 40 { 41 this.actor.Cloth = "泛黄的白色T恤"; 42 } 43 44 public override void BuildHead() 45 { 46 this.actor.Head = "三天没洗过的头"; 47 } 48 49 public override void BuildType() 50 { 51 this.actor.Type = "屌丝"; 52 } 53 } 54 55 /// <summary> 56 /// 具体建造者 57 /// </summary> 58 public class GaoFuShuaiBuilder : ActorBuilder 59 { 60 public override void BuildBody() 61 { 62 this.actor.Body = "又高又壮"; 63 } 64 65 public override void BuildCloth() 66 { 67 this.actor.Cloth = "阿玛尼西装"; 68 } 69 70 public override void BuildHead() 71 { 72 this.actor.Head = "888剪出来的发型"; 73 } 74 75 public override void BuildType() 76 { 77 this.actor.Type = "高富帅"; 78 } 79 } 80 81 /// <summary> 82 /// 指挥者 83 /// </summary> 84 public class ActorDirector 85 { 86 public Actor Construct(ActorBuilder builder) 87 { 88 builder.BuildType(); 89 builder.BuildHead(); 90 builder.BuildBody(); 91 builder.BuildCloth(); 92 93 return builder.CreateActor(); 94 } 95 } 96 #endregion