设计模式复习、实践和总结(二)

来源:互联网 发布:vs2010c 数据库教程 编辑:程序博客网 时间:2024/06/14 06:34

自己的一些理解,方便以后回顾。。。

五、建造者模式

建造者模式:将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示

想到一个很有意思的例子,山寨公司根据客户需求定制手机

class Program    {        static void Main(string[] args)        {            //老大说了要开发两部新款手机,一款具有苹果的外观,小米的内涵,另一款具有苹果、vivo、华为的特点综合            //创建指挥者            CheapCopyDirector director = new CheapCopyDirector();            //创建一个负责生产第一个产品的创建者b1            RealBudilder1 b1 = new RealBudilder1();            //创建一个负责生产第二个产品的创建者b2            RealBudilder2 b2 = new RealBudilder2();            //指挥者指挥工作            director.DirectorJob(b1);            director.DirectorJob(b2);            director.GetPhone(b1);            director.GetPhone(b2);            Console.ReadKey();        }    }    public class CheapCopyDirector    {        public void DirectorJob(Builder builder)        {            builder.SetCPU();            builder.SetDisplayer();            builder.SetKernel();        }        public void  GetPhone(Builder builder)        {            builder.GetPhone().Show();        }    }    public abstract class Builder    {        public abstract void SetCPU();        public abstract void SetKernel();        public abstract void SetDisplayer();        public abstract Pruduct GetPhone();    }    public class RealBudilder1:Builder    {        private Pruduct phone = new Pruduct();        public override void SetCPU()        {            phone.Add("小米cpu");        }        public override void SetKernel()        {            phone.Add("小米内核");        }        public override void SetDisplayer()        {            phone.Add("苹果显示器");        }        public override Pruduct GetPhone()        {            return phone;        }    }    public class RealBudilder2 : Builder    {        private Pruduct phone = new Pruduct();        public override void SetCPU()        {            phone.Add("小米cpu");        }        public override void SetKernel()        {            phone.Add("华为内核");        }        public override void SetDisplayer()        {            phone.Add("苹果显示器");        }        public override Pruduct GetPhone()        {            return phone;        }    }    public class Pruduct    {        List<string> compents = new List<string>();        public void Add(string str)        {            compents.Add(str);        }        public void Show()        {            for (int i = 0; i < compents.Count; i++)            {                Console.WriteLine(compents[i] + "已经组装完毕");            }            Console.WriteLine("此手机已经组装完毕");        }    }

每一次生产新的特定的产品 都要创建一个新的RealBuilder 类。。。

六、原型模式

当我们需要多个相同类实例的时候,通过new运算符创建相同的类实例需要消耗大量的
内存空间于是在特定情况下就通过浅拷贝来替换new操作
new运算符相关见
浅拷贝和深拷贝见

   class Program    {        static void Main(string[] args)        {            Prototype1 P1 = new Prototype1("1");            Prototype1 P2 = P1.Clone()as Prototype1;        }    }   public abstract class  Prototype     {        private string id;        public Prototype(string id)         {            this.id = id;         }        public abstract Prototype Clone();    }    public class Prototype1:Prototype    {        private string id;        public Prototype1(string id):base(id)        {        }        public override Prototype Clone()        {            return this.MemberwiseClone() as Prototype;        }    }

七、适配器模式

现在已经需要用到的接口和已经设计好的功能接口不一致,适配器模式提供一种接口的适配机制。接口和抽象类的区别见适配器模式又分为类的适配器模式和对象的适配器模式类适配器:继承一个类和一个接口,在接口中通过this.方法名来调用类的方法。
  class Program    {        static void Main(string[] args)        {            ShouldUse S = new Adapter();            S.INPut();            Console.ReadKey();        }    }    //现在需要使用的接口,比如手机充电的电压接口220v    public interface ShouldUse    {         void INPut();    }    //现在已经有的接口,比如手机需要的充电电压为5v    public class Have    {        public void  Charger()        {            Console.WriteLine("充电电压为5v可以正常充电");        }    }    //我们充电的时候就需要电源适配器,来吧220v的电压转换为5v的电压    //类适配器     public class Adapter : Have, ShouldUse    {        public void INPut()        {            Console.WriteLine("电源适配器 将电压降低到5v");            //用220v的电压接口,转化为5v的电压            this.Charger();        }    }

对象适配器
继承一个接口,在接口中实例化一个类的对象,调用该对象的方法

    class Program    {        static void Main(string[] args)        {            ShouldUse S = new Adapter();            S.INPut();            Console.ReadKey();        }    }    //现在需要使用的接口,比如手机充电的电压接口220v    public interface ShouldUse    {         void INPut();    }    //现在已经有的接口,比如手机需要的充电电压为5v    public class Have    {        public void  Charger()        {            Console.WriteLine("充电电压为5v可以正常充电");        }    }    //我们充电的时候就需要电源适配器,来吧220v的电压转换为5v的电压    //类适配器     public class Adapter : ShouldUse    {        Have h = new Have();        public void INPut()        {            Console.WriteLine("电源适配器 将电压降低到5v");            //用220v的电压接口,转化为5v的电压            h.Charger();        }    }

八、桥接模式
当一个抽象有多个实现时我们通常通过继承来重写多个实现,继承机制将它的抽象部分和实现部分固定在一起,使得难以对抽象部分和实现部分进行独立的修改、扩充和重用。
桥接模式是为了让抽象化和实现化脱藕,使两者可以独立的变化
想不出来好例子了 借鉴一下。。。。。

   class Program    {        static void Main(string[] args)        {            // 创建一个遥控器            RemoteControl remoteControl = new ConcreteRemote();            // 长虹电视机            remoteControl.Implementor = new ChangHong();            remoteControl.On();            remoteControl.SetChannel();            remoteControl.Off();            Console.WriteLine();            // 三星牌电视机            remoteControl.Implementor = new Samsung();            remoteControl.On();            remoteControl.SetChannel();            remoteControl.Off();            Console.Read();        }    }    /// <summary>    /// 抽象概念中的遥控器,扮演抽象化角色    /// </summary>    public class RemoteControl    {        // 字段        private TV implementor;        // 属性        public TV Implementor        {            get { return implementor; }            set { implementor = value; }        }        /// <summary>        /// 开电视机,这里抽象类中不再提供实现了,而是调用实现类中的实现        /// </summary>        public virtual void On()        {            implementor.On();        }        /// <summary>        /// 关电视机        /// </summary>        public virtual void Off()        {            implementor.Off();        }        /// <summary>        /// 换频道        /// </summary>        public virtual void SetChannel()        {            implementor.tuneChannel();        }    }    /// <summary>    /// 具体遥控器    /// </summary>    public class ConcreteRemote : RemoteControl    {        public override void SetChannel()        {            Console.WriteLine("---------------------");            base.SetChannel();            Console.WriteLine("---------------------");        }    }    /// <summary>    /// 电视机,提供抽象方法    /// </summary>    public abstract class TV    {        public abstract void On();        public abstract void Off();        public abstract void tuneChannel();    }    /// <summary>    /// 长虹牌电视机,重写基类的抽象方法    /// 提供具体的实现    /// </summary>    public class ChangHong : TV    {        public override void On()        {            Console.WriteLine("长虹牌电视机已经打开了");        }        public override void Off()        {            Console.WriteLine("长虹牌电视机已经关掉了");        }        public override void tuneChannel()        {            Console.WriteLine("长虹牌电视机换频道");        }    }    /// <summary>    /// 三星牌电视机,重写基类的抽象方法    /// </summary>    public class Samsung : TV    {        public override void On()        {            Console.WriteLine("三星牌电视机已经打开了");        }        public override void Off()        {            Console.WriteLine("三星牌电视机已经关掉了");        }        public override void tuneChannel()        {            Console.WriteLine("三星牌电视机换频道");        }    }