博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DesignPattern 设计模式(工厂模式、装饰器模式、单例模式)
阅读量:6367 次
发布时间:2019-06-23

本文共 18247 字,大约阅读时间需要 60 分钟。

装饰器模式

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 装饰器的基类    /// 也是一个学员    /// 因为 继承了抽象类    ///     public class BaseStudentDecorator : AbstractStudent    {        private AbstractStudent _Student = null;        public BaseStudentDecorator(AbstractStudent student)        {            this._Student = student;        }        public override void Show()        {            this._Student.Show();            //Console.WriteLine("****************");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 具体的装饰器    /// 也是一个学员    /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类    ///     public class StudentCoreDecorator : BaseStudentDecorator    {        public StudentCoreDecorator(AbstractStudent student)            : base(student)//表示调用父类的带参数构造函数        {        }        ///         /// 多重override        ///         public override void Show()        {            base.Show();//调用父类的show方法            Console.WriteLine("学习核心语法的内容。。。。");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 具体的装饰器    /// 也是一个学员    /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类    ///     public class StudentDesignDecorator : BaseStudentDecorator    {        public StudentDesignDecorator(AbstractStudent student)            : base(student)//表示调用父类的带参数构造函数        {        }        ///         /// 多重override        ///         public override void Show()        {            base.Show();//调用父类的show方法            Console.WriteLine("学习架构设计的内容。。。。");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 具体的装饰器    /// 也是一个学员    /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类    ///     public class StudentFrameworkDecorator : BaseStudentDecorator    {        public StudentFrameworkDecorator(AbstractStudent student)            : base(student)//表示调用父类的带参数构造函数        {        }        ///         /// 多重override        ///         public override void Show()        {            base.Show();//调用父类的show方法            Console.WriteLine("学习框架组件的内容。。。。");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 具体的装饰器    /// 也是一个学员    /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类    ///     public class StudentPayDecorator : BaseStudentDecorator    {        public StudentPayDecorator(AbstractStudent student)            : base(student)//表示调用父类的带参数构造函数        {        }        ///         /// 多重override        ///         public override void Show()        {            Console.WriteLine("通过腾讯课堂在线付费报名。。。。");            base.Show();//调用父类的show方法                    }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern.Decorator{    ///     /// 具体的装饰器    /// 也是一个学员    /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类    ///     public class StudentProjectDecorator : BaseStudentDecorator    {        public StudentProjectDecorator(AbstractStudent student)            : base(student)//表示调用父类的带参数构造函数        {        }        ///         /// 多重override        ///         public override void Show()        {            base.Show();//调用父类的show方法            Console.WriteLine("学习项目实战的内容。。。。");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern{    public abstract class AbstractStudent    {        public int Id { get; set; }        public string Name { get; set; }        public abstract void Show();    }}
using DecoratorPattern.Decorator;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern{    ///     /// 装饰器模式    ///     class Program    {        static void Main(string[] args)        {            try            {                Console.WriteLine("今天是设计模式的学习");                AbstractStudent student = new StudentVip()                {                    Id = 381,                    Name = "秋叶"                };                //student.Show();                //int i = 0;                //i = 1;                //AbstractStudent student2 = new BaseStudentDecorator(student);                //student2.Show();                student = new BaseStudentDecorator(student);//覆盖了                //student.Show();                //AbstractStudent student3 = new StudentCoreDecorator(student);                //student3.Show();                student = new StudentPayDecorator(student);                student = new StudentCoreDecorator(student);                student = new StudentFrameworkDecorator(student);                student = new StudentProjectDecorator(student);                student = new StudentDesignDecorator(student);                //student = new StudentPayDecorator(student);                student.Show();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }            Console.Read();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DecoratorPattern{    ///     /// 一个普通的vip学员    ///     public class StudentVip : AbstractStudent    {        public override void Show()        {            Console.WriteLine("{0} is a vip student...", base.Name);        }    }}

工厂模式

using FactoryPattern.War3.Interface;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.Abstract{    ///     /// 产品簇    ///     public abstract class AbstractFactory    {        public abstract IArmy CreateArmy();        public abstract IRace CreateRace();        //public abstract IHero CreateHero();    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.Abstract{    public class AHumanFactory : AbstractFactory    {        public override IArmy CreateArmy()        {            return new HumanArmy();        }        public override IRace CreateRace()        {            return new Human("");        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.Abstract{    public class AUndeadFactory : AbstractFactory    {        public override IArmy CreateArmy()        {            return new UndeadArmy();        }        public override IRace CreateRace()        {            return new Undead();        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using FactoryPattern.War3.ServiceExtend;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public class FiveFactory : IFactory    {        public IRace CreateRace()        {            return new Five();        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public class HumanFactory : IFactory    {        public IRace CreateRace()        {            return new Human("");        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public interface IFactory    {        IRace CreateRace();    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public class NEFactory : IFactory    {        public IRace CreateRace()        {            return new NE();        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public class ORCFactory : IFactory    {        public IRace CreateRace()        {            return new ORC();        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.FactoryMethod{    public class UndeadFactory : IFactory    {        public IRace CreateRace()        {            return new Undead();        }    }}
using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using FactoryPattern.War3.ServiceExtend;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory.SimpleFactory{    public class ObjectFactory    {        public static IRace CreateInstance(RaceType type)        {            IRace iRace = null;            switch (type)            {                case RaceType.Human:                    iRace = new Human("123");                    break;                case RaceType.NE:                    iRace = new NE();                    break;                case RaceType.ORC:                    iRace = new ORC();                    break;                case RaceType.Undead:                    iRace = new Undead();                    break;                case RaceType.Five:                    iRace = new Five();                    break;                default:                    throw new Exception("wrong RaceType");            }            return iRace;        }        //简单工厂+配置文件        //简单工厂+配置文件+反射    }    public enum RaceType    {        Human = 0,        NE = 1,        ORC = 2,        Undead = 3,        Five = 4    }}
using Factory.Abstract;using Factory.FactoryMethod;using Factory.SimpleFactory;using FactoryPattern.War3.Interface;using FactoryPattern.War3.Service;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Factory{    ///     /// 三大工厂    ///     class Program    {        static void Main(string[] args)        {            try            {                Console.WriteLine("欢迎来到.net高级班vip课程,今天是设计模式的学习");                {                    IRace race = ObjectFactory.CreateInstance(RaceType.Human);// new Human();                    race.ShowKing();                }                {                    IRace race = ObjectFactory.CreateInstance(RaceType.ORC);// new Human();                    race.ShowKing();                }                {                    IRace race = ObjectFactory.CreateInstance(RaceType.NE);// new Human();                    race.ShowKing();                }                {                    Human human = new Human("123");                    IFactory facetory = new HumanFactory();                    IRace race = facetory.CreateRace();                    race.ShowKing();                }                {                    //Human human = new Human();                    IFactory facetory = new FiveFactory();                    IRace race = facetory.CreateRace();                    race.ShowKing();                }                {                    AbstractFactory factory = new AHumanFactory();                    IRace race = factory.CreateRace();                    race.ShowKing();                    IArmy army = factory.CreateArmy();                    army.BuildArmy();                }                {                    AbstractFactory factory = new AUndeadFactory();                    IRace race = factory.CreateRace();                    race.ShowKing();                    IArmy army = factory.CreateArmy();                    army.BuildArmy();                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);            }        }    }}

单例模式

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SingletonPattern{    ///     ///     ///     class Program    {        //public static Singleton singleton = new Singleton();        static void Main(string[] args)        {            try            {                Console.WriteLine("今天是单例模式");                TaskFactory taskFactory = new TaskFactory();                List
taskList = new List
(); //for (int i = 0; i < 5; i++) //{ // taskList.Add(taskFactory.StartNew(() => // { // Singleton singleton = Singleton.CreateInstance(); //new Singleton(); // singleton.Write(); // })); //} //Task.WaitAll(taskList.ToArray()); //Console.WriteLine("**********************************************************"); //for (int i = 0; i < 5; i++) //{ // taskFactory.StartNew(() => // { // Singleton singleton = Singleton.CreateInstance(); //new Singleton(); // singleton.Write(); // }); //} { Singleton singleton = Singleton.CreateInstance(); } { for (int i = 0; i < 10000; i++) { taskList.Add(taskFactory.StartNew(() => { Singleton singleton = Singleton.CreateInstance(); //new Singleton(); singleton.Write(); })); } Task.WaitAll(taskList.ToArray()); } { Singleton singleton = Singleton.CreateInstance(); singleton.ShowI(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); } //method1 //method2 //method3 //method4 //method5 }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace SingletonPattern{    ///     /// 构造很麻烦  很耗资源    ///     public class Singleton    {        private Singleton()        {            long lResult = 0;            for (int i = 0; i < 10000000; i++)            {                lResult += i;            }            Thread.Sleep(1000);            Console.WriteLine("{0}被构造...", this.GetType().Name);        }        private static Singleton _Singleton = null;        private static object Singleton_Lock = new object();        public static Singleton CreateInstance()        {            if (_Singleton == null)            {                lock (Singleton_Lock)                {                    Console.WriteLine("进入lock,wait一下");                    Thread.Sleep(100);                    if (_Singleton == null)                    {                        _Singleton = new Singleton();                    }                }            }            return _Singleton;        }        private int _i = 0;        public void Write()        {                        //Console.WriteLine("这里是write a file");            this._i++;        }        public void ShowI()        {            Console.WriteLine(this._i);        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace SingletonSecondPattern{    ///     /// 构造很麻烦  很耗资源    ///     public class SingletonSecond    {        private SingletonSecond()        {            long lResult = 0;            for (int i = 0; i < 10000000; i++)            {                lResult += i;            }            Thread.Sleep(1000);            Console.WriteLine("{0}被构造...", this.GetType().Name);        }        private static SingletonSecond _SingletonSecond = null;        ///         /// 静态构造函数:只能有一个,无参数的,程序无法调用        /// 由CLR保证,在程序第一次使用该类之前被调用,而且只调用一次        ///         static SingletonSecond()        {            _SingletonSecond = new SingletonSecond();        }        public static SingletonSecond CreateInstance()        {            return _SingletonSecond;        }        private int _i = 0;        public void Write()        {                        //Console.WriteLine("这里是write a file");            this._i++;        }        public void ShowI()        {            Console.WriteLine(this._i);        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace SingletonThirdPattern{    ///     /// 构造很麻烦  很耗资源    ///     public class SingletonThird    {        private SingletonThird()        {            long lResult = 0;            for (int i = 0; i < 10000000; i++)            {                lResult += i;            }            Thread.Sleep(1000);            Console.WriteLine("{0}被构造...", this.GetType().Name);        }        ///         /// 静态变量:由CLR保证,在程序第一次使用该类之前被调用,而且只调用一次        ///         private static SingletonThird _SingletonThird = new SingletonThird();        public static SingletonThird CreateInstance()        {            return _SingletonThird;        }        private int _i = 0;        public void Write()        {            //Console.WriteLine("这里是write a file");            this._i++;        }        public void ShowI()        {            Console.WriteLine(this._i);        }    }}

 

转载于:https://www.cnblogs.com/zhengqian/p/8630547.html

你可能感兴趣的文章
【*三种电源模式笔记本降温Windows 7提供的方法*】
查看>>
GreenPlum之日常SQL脚本笔记(二)
查看>>
python进阶-面向对象编程六:元类
查看>>
c语言知识(找出大于2门成绩不及格的学生)
查看>>
如何搭建个人的yum repository
查看>>
Ant出现"使用了未经检查或不安全的操作"
查看>>
SlipJs滚动条
查看>>
leetcode第一刷_Minimum Window Substring
查看>>
2.oracle分页,找到员工表中薪水大于本部门平均薪水的员工
查看>>
Caused by: java.lang.NumberFormatException: For input string: &quot;&quot;
查看>>
彻查网络局部网段内Ping时断时续的问题
查看>>
新公式:互联网+脑科学=互联网神经学
查看>>
第一篇,测试
查看>>
[zz]Using SyncTeX with LaTeX
查看>>
冒泡法排序参考(Java)
查看>>
Silverlight TreeView 带 checkbox和图片
查看>>
武汉科技大学ACM:1007: 不高兴的津津
查看>>
《pyhton学习手册》 第33章 异常编码细节
查看>>
条目二十三《考虑用排序的vector替代关联容器》
查看>>
Keepalived 资源监控
查看>>