• DesignPattern 设计模式(工厂模式、装饰器模式、单例模式)


    装饰器模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DecoratorPattern.Decorator
    {
        /// <summary>
        /// 装饰器的基类
        /// 也是一个学员
        /// 因为 继承了抽象类
        /// </summary>
        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
    {
        /// <summary>
        /// 具体的装饰器
        /// 也是一个学员
        /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类
        /// </summary>
        public class StudentCoreDecorator : BaseStudentDecorator
        {
            public StudentCoreDecorator(AbstractStudent student)
                : base(student)//表示调用父类的带参数构造函数
            {
    
            }
    
            /// <summary>
            /// 多重override
            /// </summary>
            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
    {
        /// <summary>
        /// 具体的装饰器
        /// 也是一个学员
        /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类
        /// </summary>
        public class StudentDesignDecorator : BaseStudentDecorator
        {
            public StudentDesignDecorator(AbstractStudent student)
                : base(student)//表示调用父类的带参数构造函数
            {
    
            }
    
            /// <summary>
            /// 多重override
            /// </summary>
            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
    {
        /// <summary>
        /// 具体的装饰器
        /// 也是一个学员
        /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类
        /// </summary>
        public class StudentFrameworkDecorator : BaseStudentDecorator
        {
            public StudentFrameworkDecorator(AbstractStudent student)
                : base(student)//表示调用父类的带参数构造函数
            {
    
            }
    
            /// <summary>
            /// 多重override
            /// </summary>
            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
    {
        /// <summary>
        /// 具体的装饰器
        /// 也是一个学员
        /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类
        /// </summary>
        public class StudentPayDecorator : BaseStudentDecorator
        {
            public StudentPayDecorator(AbstractStudent student)
                : base(student)//表示调用父类的带参数构造函数
            {
    
            }
    
            /// <summary>
            /// 多重override
            /// </summary>
            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
    {
        /// <summary>
        /// 具体的装饰器
        /// 也是一个学员
        /// 因为 继承了BaseStudentDecorator,也就是间接继承了抽象类
        /// </summary>
        public class StudentProjectDecorator : BaseStudentDecorator
        {
            public StudentProjectDecorator(AbstractStudent student)
                : base(student)//表示调用父类的带参数构造函数
            {
    
            }
    
            /// <summary>
            /// 多重override
            /// </summary>
            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
    {
        /// <summary>
        /// 装饰器模式
        /// </summary>
        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
    {
        /// <summary>
        /// 一个普通的vip学员
        /// </summary>
        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
    {
        /// <summary>
        /// 产品簇
        /// </summary>
        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
    {
        /// <summary>
        /// 三大工厂
        /// </summary>
        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
    {
        /// <summary>
        /// 
        /// </summary>
        class Program
        {
    
            //public static Singleton singleton = new Singleton();
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("今天是单例模式");
    
                    TaskFactory taskFactory = new TaskFactory();
                    List<Task> taskList = new List<Task>();
    
    
                    //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
    {
        /// <summary>
        /// 构造很麻烦  很耗资源
        /// </summary>
        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
    {
        /// <summary>
        /// 构造很麻烦  很耗资源
        /// </summary>
        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;
    
            /// <summary>
            /// 静态构造函数:只能有一个,无参数的,程序无法调用
            /// 由CLR保证,在程序第一次使用该类之前被调用,而且只调用一次
            /// </summary>
            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
    {
        /// <summary>
        /// 构造很麻烦  很耗资源
        /// </summary>
        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);
            }
    
            /// <summary>
            /// 静态变量:由CLR保证,在程序第一次使用该类之前被调用,而且只调用一次
            /// </summary>
            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);
            }
    
        }
    }
  • 相关阅读:
    重载函规则
    lambd
    内联函数
    c和c++中的枚举和 区别
    关于于c++中的类型转换
    作用域解析运算符
    day01
    二级指针输入特性
    二级指针的 数出特性
    influence maximization 第二弹
  • 原文地址:https://www.cnblogs.com/zhengqian/p/8630547.html
Copyright © 2020-2023  润新知