标签: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(); //} } }
标签:ati log 重写 namespace null static 隐藏 sign producer
原文地址:http://www.cnblogs.com/onegarden/p/7157252.html