1.单例模式:关注于对象的创建。分为懒汉式和饿汉式。
懒汉式:调用方法的时候才创建;
class Singleton { private Singleton() //1.私有构造方法 { Console.WriteLine("构造方法被创建"); } private static volatile Singleton _Singleton = null;//3.全局唯一静态,重用这个变量 private static object Singleton_Lock = new object(); public static Singleton CreateInstance()//2.公开的静态方法提供对象实例 { if(_Singleton == null)//4.多线程,双判断加锁 { lock (Singleton_Lock) { if (_Singleton == null) { _Singleton = new Singleton(); } } } return _Singleton; } public int iTotal = 0; internal void Show() { iTotal++; } }
饿汉式1:静态构造函数中创建,一开始就创建
class SingletonSecond { private SingletonSecond() { } static SingletonSecond() { _SingletonSecond = new SingletonSecond(); } public static SingletonSecond _SingletonSecond = null; public SingletonSecond CreateInstance() { return _SingletonSecond; } /// <summary> /// 原型模式--主要用于克隆的方式比创建快的场景 /// </summary> /// <returns></returns> public static SingletonSecond CreateInstancePrototype() { SingletonSecond second = (SingletonSecond)_SingletonSecond.MemberwiseClone(); return second; } }
饿汉式2:使用静态字段创建对象
class SingletonThird { private SingletonThird() { } /// <summary> /// 静态字段:在第一次使用这个类之前,初始化且只初始化一次 /// 这个比构造函数还早 /// </summary> public static SingletonThird _SingletonThird = new SingletonThird(); public static SingletonThird CreateInstance() { return _SingletonThird; } }
2.三大工厂:简单工厂、工厂方法、抽象工厂
一.简单工厂:把创建对象的过程封装成工厂。
//想去细节,想去掉什么,封装一下 IRace race = ObjectFactory.CreateRace(RaceType.Human); IRace race = ObjectFactory.CreateRaceConfig(); IRace race = ObjectFactory.CreateRaceConfigReflection();
class ObjectFactory { public static IRace CreateRace(RaceType raceType) { IRace iRace = null; switch (raceType) { case RaceType.Human: iRace = new Human(); break; case RaceType.NE: iRace = new NE(); break; case RaceType.ORC: iRace = new ORC(); break; case RaceType.Undead: iRace = new Undead(); break; default: break; } return iRace; } public static IRace CreateRaceConfig() { string IRaceTypeConfig = ""; RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), IRaceTypeConfig); return CreateRace(raceType); } public static IRace CreateRaceConfigReflection() { Assembly assembly = Assembly.Load(""); Type type = assembly.GetType(""); IRace iRace = Activator.CreateInstance(type) as IRace; return iRace; } }
二.工厂方法:每个工厂只负责一个对象的创建(建造者模式:是为了创建一个更复杂的对象)
IFactory factory = new HumanFactory(); IRace race = factory.CreateRace();
这样做的好处是:可以增加创建逻辑,屏蔽对象实例化的复杂度,对象创建的过程中,可以扩展(尤其式IOC)
public class HumanFactory : IFactory { public virtual IRace CreateRace() {
console.write("增加的创建逻辑") return new Human(); } public class HumanFactoryAdvanced : HumanFactory //可扩展体现在此处 { public override IRace CreateRace() { Console.WriteLine("123"); return new Human(); } } }
三.抽象工厂:一个工厂负责产品簇的创建:
class HumanFactory : FactoryAbstract { public override IArmy CreateArmy() { throw new NotImplementedException(); } public override IHero CreateHero() { throw new NotImplementedException(); } public override IRace CreateRace() { throw new NotImplementedException(); } public override IResource CreateResource() { throw new NotImplementedException(); } }
标签:Singleton,IRace,--,SingletonSecond,static,单例,new,设计模式,public From: https://www.cnblogs.com/guoxu486/p/18383722