GOF23种设计模式系列之创建型设计模式

2024-04-29 09:58

本文主要是介绍GOF23种设计模式系列之创建型设计模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

GOF23种设计模式系列之创建型设计模式

目录

  • GOF23种设计模式系列之创建型设计模式
    • 什么是创建型设计模式
    • 单例模式/原型模式 (Singleton)
      • 单例方式一:懒汉试双if锁
      • 单例方式二:饿汉式静态构造函数
      • 单例模式三:饿汉式静态字段
      • 调用方式
      • 如果正确使用单例模式呢?
    • 简单工厂 (Simple Factory)
      • 基于依赖倒置原则的简单工厂
      • 基于配置文件的简单工厂
      • 基于反射+配置文件的简单工厂
      • 关于简单工厂
    • 工厂方法(Factory Method )
      • 关于工厂方法
    • 抽象工厂(Abstract Factory)
      • 关于抽象工厂
    • 建造者模式(Builder)
      • 关于建造者模式
    • 原型模式(Prototype)
      • 直接new
      • 序列化反序列化
      • 关于原型模式

什么是创建型设计模式

创建型设计模式的核心套路,就是管理对象创建,没有一定之规,按需取用就好

创建型设计模式有以下几种:

  1. 单例模式/原型模式 (Singleton Factory)

  2. 简单工厂 (Simple Factory)

  3. 工厂方法(Factory Method )

  4. 抽象工厂(Abstract Factory)

  5. 建造者模式(Builder)

  6. 原型模式(Prototype)

     简单工厂并没有纳入GOF23
    

单例模式/原型模式 (Singleton)

单例模式就是保证进程中,类型只有一个实例a) 构造函数私有化,防止他人实例化b) 对外提供一个获取实例的途径,公开的静态方法c) 返回共用一个静态字段

单例方式一:懒汉试双if锁

	/// <summary>/// 单例类:一个构造对象很耗时耗资源类型/// 懒汉式/// </summary>public sealed class Singleton{/// <summary>/// 对象会持有资源/// </summary>private List<string> ConnList = new List<string>(){};/// <summary>/// 构造函数耗时耗资源/// </summary>private Singleton(){long lResult = 0;for (int i = 0; i < 10000000; i++){lResult += i;}Thread.Sleep(1000);Console.WriteLine($"{this.GetType().Name}被构造一次");}private static Singleton _Singleton = null;private static readonly object Singleton_Lock = new object();public static Singleton CreateInstance(){//开始多线程初始化---lock锁定--线程请求锁--开始判断创建//以上多线程都结束后---再来多个线程请求呢?--都需要等待锁//--拿到锁--进去判断--不为空--返回对象--。。。。//有点浪费,不需要等待锁,直接判断就行了if (_Singleton == null){lock (Singleton_Lock)//可以保证任意时刻只有一个线程进入,其他线程等待{if (_Singleton == null)//这个判断不能去掉,保证只初始化一次{_Singleton = new Singleton();}}}return _Singleton;}public static void DoNothing(){ }}

单例方式二:饿汉式静态构造函数

  public sealed class SingletonSecond{/// <summary>/// 对象会持有资源/// </summary>private List<string> ConnList = new List<string>(){};/// <summary>/// 构造函数耗时耗资源/// </summary>private SingletonSecond(){long lResult = 0;for (int i = 0; i < 10000000; i++){lResult += i;}Thread.Sleep(1000);Console.WriteLine($"{this.GetType().Name}被构造一次");}private static SingletonSecond _SingletonSecond = null;/// <summary>/// 静态构造函数:由CLR保证,在第一次使用到这个类型之前,自动被调用且只调用一次/// 很多初始化都可以写在这里/// </summary>static SingletonSecond(){_SingletonSecond = new SingletonSecond();}public static SingletonSecond CreateInstance(){return _SingletonSecond;}}

单例模式三:饿汉式静态字段

/// <summary>
/// 饿汉式
/// </summary>
public sealed class SingletonThird
{/// <summary>/// 对象会持有资源/// </summary>private List<string> ConnList = new List<string>(){"这里是数据库连接1","这里是数据库连接2","这里是数据库连接3","这里是数据库连接4","这里是数据库连接5"//一些其他资源};/// <summary>/// 构造函数耗时耗资源/// </summary>private SingletonThird(){long lResult = 0;for (int i = 0; i < 10000000; i++){lResult += i;}Thread.Sleep(1000);Console.WriteLine($"{this.GetType().Name}被构造一次");}/// <summary>/// 静态字段:由CLR保障,在第一次使用这个类型之前,会自动初始化且只初始化一次/// </summary>private static SingletonThird _SingletonThird = new SingletonThird();public static SingletonThird CreateInstance(){return _SingletonThird;}}

调用方式

Singleton singleton = Singleton.CreateInstance();
SingletonSecond singletonSecond = SingletonSecond.CreateInstance();
SingletonThird singletonThird = SingletonThird.CreateInstance();

如果正确使用单例模式呢?

学完单例,就恨不得把全部类型都搞成单例,这是不对的。因为单例模式会常驻内存中用来重用对象,不会自动销毁。所以不会节省多少资源。而且单例模式并不能保证整个类中的多线程并发问题。因此我们只需要这个对象实例化一次并且只允许实例化的时候使用单例模式就可以了。
例如:
1. 数据库连接池
2. 流水号生成器
3. 配置文件读取
4. IOC容器实例

简单工厂 (Simple Factory)

 简单工厂设计模式:包含一组需要创建的对象,通过一个工厂类来实例化对象

基于依赖倒置原则的简单工厂

	public class Player{public int Id { get; set; }public string Name { get; set; }//一个方法代替多个方法 泛型//即使将来种族有新的扩展,都可以使用public void Play(IRace iRace){Console.WriteLine("******************************");Console.WriteLine("This is {0} Play War3.{1}", this.Name, iRace.GetType().Name);iRace.ShowKing();}}
	public interface IRace{void ShowKing();}
	public enum RaceType{Human,NE,ORC,Undead}
 	public class ObjectFactory{public static IRace CreateInstance(RaceType raceType){IRace race = null;switch (raceType){case RaceType.Human:race = new Human();break;case RaceType.NE:race = new NE();break;case RaceType.ORC:race = new ORC();break;case RaceType.Undead:race = new Undead();break;default:throw new Exception("wrong raceType");}return race;}}

调用方式

	IRace race = ObjectFactory.CreateInstance(RaceType.Human);player.Play(race);

基于配置文件的简单工厂

public class ObjectFactory{private static string IRaceType = ConfigurationManager.AppSettings["IRaceType"];public static IRace CreateInstanceConfig(){RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), IRaceType);return CreateInstance(raceType);}}
<configuration><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup><appSettings><add key="IRaceType" value="Human"/></appSettings>
</configuration>

调用方式

	IRace race = ObjectFactory.CreateInstanceConfig();player.Play(race);

基于反射+配置文件的简单工厂

 	public class ObjectFactory{private static string IRaceTypeReflection = ConfigurationManager.AppSettings["IRaceTypeReflection"];public static IRace CreateInstanceConfigReflection(){Assembly assembly = Assembly.Load(IRaceTypeReflection.Split(',')[1]);Type type = assembly.GetType(IRaceTypeReflection.Split(',')[0]);return (IRace)Activator.CreateInstance(type);}}

调用方式

	IRace race = ObjectFactory.CreateInstanceConfigReflection();player.Play(race);

关于简单工厂

GOF23种设计模式是不包含简单工厂的。
简单工厂的好处就是去掉上端对细节的依赖,保证上端的稳定。但是它并没有消除矛盾,只是转移矛盾,甚至还集中了矛盾

工厂方法(Factory Method )

包含一组需要创建的对象,通过一个工厂类来实例化对象
 	public interface IFactory{IRace CreateInstance();}
 	/// <summary>/// 工厂类/// </summary>public class HumanFactory : IFactory{/// <summary>/// 使用虚方法还可以为以后扩展做铺垫/// </summary>/// <returns></returns>public virtual IRace CreateInstance(){IRace race = new Human();return race;}}
	public interface IRace{void ShowKing();}

调用方式

	IFactory humanFactory = new HumanFactory();IRace human = humanFactory.CreateInstance();

关于工厂方法

将创建实例的细节交给工厂类,使上端不需要主要这些细节。在拓展时只需添加新的工厂类,减少对源代码的改动,使代码更加文档。
但将工厂方法独立以后,原本需要一句new可以解决的事情变成了两句才可以解决。所以不适合大范围使用。一般对构造复杂的类使用。

抽象工厂(Abstract Factory)

抽象工厂模式提供了一个创建一系列相关或者相互依赖对象的接口,无需指定它们具体的类
	public interface IGroup{void ShowGroup();}public interface IGenerals{void ShowGenerals();}public class FactoryShu{public IGroup CreateGroup() {return new GroupShu();}public IGenerals CreateGenerals(){return new GeneralsShu();}}
	public class GeneralsShu: IGenerals{public void ShowGenerals(){Console.WriteLine("{0} is 关羽、张飞、赵云、马超、黄忠", this.GetType().Name);}}public class GroupShu :IGroup{public void ShowGroup(){Console.WriteLine("{0} is 刘备", this.GetType().Name);}}

调用方式:

 	FactoryShu factoryShu = new FactoryShu();IGroup group = factoryShu.CreateGroup();IGenerals generals = factoryShu.CreateGenerals();group.ShowGroup();generals.ShowGenerals();

关于抽象工厂

抽象工厂属于倾斜试可拓展设计,对于产品簇扩展方便,但对于工厂职责不能扩展。否则改起来很麻烦。
不适合在产品不确定的情况使用,产品不定会导致频繁修改工厂职责,这对于这个模式来说是灾难性的
官方参考类:DbProciderFactory

建造者模式(Builder)

建造者模式就是一步步创建一个对象,它对用户屏蔽了里面构建的细节,但却可以精细地控制对象的构造过程。
	/// <summary>/// 工人抽象/// </summary>public abstract class AbstractBuilder{public abstract void Engine();public abstract void Wheels();public abstract void Light();public abstract Car Car();}
	/// <summary>/// 比亚迪工人/// </summary>public class BuilderBYD : AbstractBuilder{private Engine _Engine = null;private Wheels _Wheels = null;private Light _Light = null;public override void Engine(){this._Engine = new Engine(){Name = "_Engine"};Console.WriteLine("{0} build Engine", this.GetType().Name);}public override void Wheels(){this._Wheels = new Wheels(){Name = "_Wheels"};Console.WriteLine("{0} build Wheels", this.GetType().Name);}public override void Light(){this._Light = new Light(){Name = "_Light"};Console.WriteLine("{0} build Light", this.GetType().Name);}public override Car Car(){Console.WriteLine("组装 {0} {1} {2}", this._Engine, this._Light, this._Wheels);Console.WriteLine("{0} build 比亚迪*唐", this.GetType().Name);return new Car(this._Engine, this._Light, this._Wheels){Name = "比亚迪*唐"};}}
	public class Director{private AbstractBuilder _AbstractBuilder = null;public Director(AbstractBuilder builder){this._AbstractBuilder = builder;}public Car GetCar(){this._AbstractBuilder.Engine();this._AbstractBuilder.Wheels();this._AbstractBuilder.Light();return this._AbstractBuilder.Car();}}

调用方式:

	AbstractBuilder builder = new BuilderBYD();Director director = new Director(builder);director.GetCar();

关于建造者模式

 建造者在平常使用中并不太常见,因为一般这种模式需要创建对象过于复杂时才会使用并且创建流程不能经常变更

原型模式(Prototype)

用原型模式可以用原型实例指定创建对象的种类,它允许通过一个原型对象创建多个同类型的其他对象,而无需知道该对象的创建细节
	/// <summary>/// 原型类/// </summary>public class StudentPrototype{/// <summary>/// 1 构造函数私有化--避免随意构造/// </summary>private StudentPrototype(){Thread.Sleep(2000);long lResult = 0;for (int i = 0; i < 1000000; i++){lResult += i;}Console.WriteLine("{0}被构造..", this.GetType().Name);}/// <summary>/// 3 私有的静态字段--内存唯一,不会释放,且在第一次使用这个类被初始化且只初始化一次/// </summary>private static StudentPrototype _Student = new StudentPrototype(){Id = 123,Name = "张三",Class = new Class(){ClassId = 1,ClassName = "三年一班"}};/// <summary>/// 2 公开的静态方法来提供实例/// </summary>/// <returns></returns>public static StudentPrototype CreateInstance(){StudentPrototype student = (StudentPrototype)_Student.MemberwiseClone();//MemberwiseClone:内存拷贝,不走构造函数,直接内存copy,所以没有性能损失;而且产生的是新对象----浅拷贝--只拷贝引用return student;}public static StudentPrototype CreateInstanceSerialize(){return SerializeHelper.DeepClone<StudentPrototype>(_Student);}public int Id { get; set; }public string Name { get; set; }public Class Class { get; set; }public void Study(){Console.WriteLine("{0}在学习语文", this.Name);}}public class Class{public int ClassId { get; set; }public string ClassName { get; set; }}

调用方式:

        StudentPrototype student1 = StudentPrototype.CreateInstance();StudentPrototype student2 = StudentPrototype.CreateInstance();StudentPrototype student3 = StudentPrototype.CreateInstance();student1.Id = 234;student1.Name = "李四";student1.Class.ClassId = 2;student1.Class.ClassName = "四年一班";//因为string类型的 ="CodeMan" 等同于 new String("CodeMan"); 开辟新的空间,不影响之前的-----实际上string是不可修改的----student2.Id = 345;student2.Name = "王五";student2.Class.ClassId = 3;student2.Class.ClassName = "五年一班";student3.Id = 456;student3.Name = "赵六";student3.Class = new Class(){ClassId = 4,ClassName = "六年一班"};//换了个引用student1.Study();student2.Study();student3.Study();//输出结果:334

这里会发现一个问题,因为内存分配的原因student中的class对象会被分配到堆内存中。而class对象又保存着值类型的引用。导致每次复制出来的对象class引用都是相同的。
string类型没有这个问题,因为在MemberwiseClone方法中对string做了特殊处理,等同于 new String(“李四”);属于开辟新的空间,不会影响之前的内存。(实际上string被修改也是重新分配内存地址)
处理这种问题需要使用深度复制(deepcopy)一般有三种方法:
1. 直接new
2. 子类型提供原型方式
3. 序列化反序列化

这里演示一下第一种和第三种

直接new

	/// <summary>/// 2 公开的静态方法来提供实例/// </summary>/// <returns></returns>public static StudentPrototype CreateInstance(){StudentPrototype student = (StudentPrototype)_Student.MemberwiseClone();student.Class = new Class(){ClassId = student.Class.ClassId,ClassName = student.Class.ClassName};return student;}

序列化反序列化

	public class SerializeHelper{public static string Serializable(object target){using (MemoryStream stream = new MemoryStream()){new BinaryFormatter().Serialize(stream, target);return Convert.ToBase64String(stream.ToArray());}}public static T Derializable<T>(string target){byte[] targetArray = Convert.FromBase64String(target);using (MemoryStream stream = new MemoryStream(targetArray)){return (T)(new BinaryFormatter().Deserialize(stream));}}public static T DeepClone<T>(T t){return Derializable<T>(Serializable(t));}}
 	/// <summary>/// 2 公开的静态方法来提供实例/// </summary>/// <returns></returns>public static StudentPrototype CreateInstance(){return SerializeHelper.DeepClone<StudentPrototype>(_Student);}

关于原型模式

对于原型模式一定要特别注意内存的拷贝机制,否则就是给自己挖一个很大的坑。
原型模式在平时使用出现也比较少,但也相当有意义。原型模式就是用利用对象copy来快速获取对象,一般使用在需要大量新对象的时候( 如:画图(棋盘)、文档等)

这篇关于GOF23种设计模式系列之创建型设计模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/945776

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建