面試問到這個··答不出來就是沒有架構能力···這裡學習一下···面試的時候直接讓我說出26種設計模式··當時就懵逼了··我記得好像之前看的時候是23種的 還有3個是啥的··· 這裡先列出簡單的三種,工廠、抽象工廠、單例,後續在更新 工廠模式:缺點是每增加一個類型就得增加一個工具類和對象工廠類(反射可 ...
面試問到這個··答不出來就是沒有架構能力···這裡學習一下···面試的時候直接讓我說出26種設計模式··當時就懵逼了··我記得好像之前看的時候是23種的 還有3個是啥的···
這裡先列出簡單的三種,工廠、抽象工廠、單例,後續在更新
工廠模式:缺點是每增加一個類型就得增加一個工具類和對象工廠類(反射可以避免修改這個···)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace ExercisePrj.Dsignmode { public class ShapeFactory { public static IShape CtreateShape(string shape) { if (shape == "Line") { return new Line(); } else if (shape == "Circle") { return new Circle(); } return null; }
//反射的實現方式,規定一個統一的類命名方式,通過反射初始化 public static IShape CtreateWithReflection(string shape) { Assembly assembly = Assembly.GetExecutingAssembly(); var ishape = assembly.CreateInstance("ExercisePrj.Dsignmode."+shape); return ishape as IShape; } } public interface IShape { void Draw(); } public class Line: IShape { public void Draw()//隱式封閉實現,子類可以隱藏不能重寫,類調用會執行這個 { Console.WriteLine("draw line"); } void IShape.Draw()//顯示實現,介面調用會執行這個 { Console.WriteLine("IShape.DrawLine"); } } public class Circle:IShape { public void Draw() { Console.WriteLine("draw Circle"); } } }
抽象工廠模式,簡單講就是比上邊更流弊的工廠模式···這裡有用到上邊的類型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExercisePrj.Dsignmode { //抽象工廠類 public abstract class AbstractFactory { public abstract IShape GetShape(string shape); public abstract IColor GetColor(string color); } //工廠類子類 public class ShapeFactoryEx:AbstractFactory { public override IShape GetShape(string shape) { return ShapeFactory.CtreateShape(shape);//偷個懶 } public override IColor GetColor(string color) { return null; } } public class ColorFactory : AbstractFactory { public override IShape GetShape(string shape) { return null; } public override IColor GetColor(string color) { if(color=="blue") { return new Blue(); } else if (color=="red") { return new Red(); } return null; } } //工廠創造器 public class FactoryProducer { public static AbstractFactory getFactory( string SType) { if(SType=="shape") { return new ShapeFactoryEx(); } else if(SType=="color") { return new ColorFactory(); } return null; } } public interface IColor { void Fill(); } public class Blue:IColor { public void Fill() { Console.WriteLine("Blue"); } } public class Red : IColor { public void Fill() { Console.WriteLine("Red"); } } }
單例模式:平時用的時候連鎖都沒加···上次面試的時候,人家問在多線程裡邊會出啥問題···當時就沒反應過來·,說這有啥問題的·都是一個對象調方法就是··完事才想起來,如果初始化的函數在多線程裡邊就是線程不安全了··簡直矇蔽··這裡列好幾種寫法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExercisePrj.Dsignmode { public class Singleton { private Singleton() { } //private static Singleton m_Singleton; //private static readonly object lockvalue = new object(); //public static Singleton GetInstance() //{ // //return m_Singleton ?? new Singleton();//不加鎖 線程不安全 // if (m_Singleton == null) // { // lock (lockvalue)//枷鎖//這裡還可以加雙鎖,就是在裡邊判斷是不是空 // { // return new Singleton(); // } // } // return m_Singleton; //} public static readonly Singleton Instance = new Singleton();//據說這個是最流弊的寫法··跟下邊的寫法是一個意思·· //public static readonly Singleton Instance=null //static Singleton() //{ // Instance = new Singleton(); //} } }