C# 設計模式··

來源:互聯網
上載者:User

標籤:ati   log   重寫   namespace   null   static   隱藏   sign   producer   

面試問到這個··答不出來就是沒有架構能力···這裡學習一下···面試的時候直接讓我說出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();        //}    }}

 

C# 設計模式··

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.