Ejemplo n.º 1
0
 //
 static void Main(string[] args) {
     //
     ConcreteBuilder builder = new ConcreteBuilder();
     Director director = new Director(builder);
     director.Construct();
     Product product = builder.GetProduct();
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // 装饰
            //var tt = new ConcreteDecoratorAImpl();
            //var ss = new ConcreteComponent();
            //tt.SetComponet(ss);
            //tt.Operation();

            // 代理
            //var proxyImpl = new ProxyImpl("嘿嘿");
            //proxyImpl.SendFlowers();
            //proxyImpl.SendDolls();

            // 简单工厂
            //var calcSubImpl = SimpleFactoryHandle.Create("-");
            //calcSubImpl.NumberB = 10;
            //calcSubImpl.NumberA = 9;
            //Console.WriteLine(calcSubImpl.GetResult());

            // 工厂方法
            //var addFatoryImpl = new AddFatoryImpl();
            //var calcAddImpl = addFatoryImpl.Create();
            //calcAddImpl.NumberB = 10;
            //calcAddImpl.NumberA = 9;
            //Console.WriteLine(calcAddImpl.GetResult());

            // 模板模式
            //var copyFightImpl = new CopyFightImpl();
            //copyFightImpl.BuildFight();

            //var earthFightImpl = new EarthFightImpl();
            //earthFightImpl.BuildFight();

            // 建造者模式
            var concreteBuilderImpl = new ConcreteBuilderImpl();

            Director.Construct(concreteBuilderImpl);// 构造数据
            Console.WriteLine(concreteBuilderImpl.GetBody());

            Console.ReadKey();
        }
        public static void Main(string[] args)
        {
            // Simple Builder
            var director = new Director();
            var product  = director.Construct();

            product.Parts.ForEach((part) => Console.WriteLine(part));

            // Fluent Builder
            var builder = new FluentBuilder();

            product = builder.Begin()
                      .Engine
                      .SteeringWheel
                      .Tire()
                      .Tire()
                      .Build();
            product.Parts.ForEach((part) => Console.WriteLine(part));

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            //--------------------------- AbstractFactory
            Console.WriteLine("--------------------------- AbstractFactory");

            Client client = null;

            client = new Client(new CocaColaFactory());
            client.Run();

            client = null;
            client = new Client(new PepsiColaFactory());
            client.Run();

            /*
             *  AbstractFactory.Program + CocaColaBottle interacts with AbstractFactory.Program + CocaColaWater
             *  AbstractFactory.Program + CocaColaCover closes AbstractFactory.Program + CocaColaBottle
             *  AbstractFactory.Program + PepsiColaBottle interacts with AbstractFactory.Program + PepsiColaWater
             *  AbstractFactory.Program + PepsiColaCover closes AbstractFactory.Program + PepsiColaBottle
             */


            //--------------------------- Builder
            Console.WriteLine("--------------------------- Builder");

            Builder  stoneHouseBuilder = new ConcreteBuilder();
            Director director          = new Director(stoneHouseBuilder);

            director.Construct();

            House house = stoneHouseBuilder.GetResult();

            house.Show();

            /*
             *  Basement Created
             *  Floor Created
             *  Roof Created
             *  Element: DesignPatterns.Basement builded.
             *  Element: DesignPatterns.Floor builded.
             *  Element: DesignPatterns.Roof builded.
             */


            //--------------------------- FactoryMethod
            Console.WriteLine("--------------------------- FactoryMethod");

            Creator creator = new ConcreteCreator();
            Product product = null;

            product = creator.FactoryMethod();
            //creator.AnOperation();

            /* 46104728 */

            //--------------------------- Prototype
            Console.WriteLine("--------------------------- Prototype");

            Prototype prototype = new ConcretePrototype1(1);
            Prototype clone     = prototype.Clone();

            clone.AnOperation();

            Prototype prototype2 = new ConcretePrototype2(2);
            Prototype clone2     = prototype2.Clone();

            clone2.AnOperation();

            /*
             *  ID : 1
             *  Class : DesignPatterns.ConcretePrototype1
             *  ID : 2
             *  Class : DesignPatterns.ConcretePrototype2
             */


            //--------------------------- Singleton
            Console.WriteLine("--------------------------- Singleton");

            Singleton firstInstance  = Singleton.Instance();
            Singleton secondInstance = Singleton.Instance();

            // Singleton thirdinstance = new Singleton(); // error

            Console.WriteLine(firstInstance.GetHashCode());
            Console.WriteLine(secondInstance.GetHashCode());
            Console.WriteLine(ReferenceEquals(firstInstance, secondInstance));

            firstInstance.SampleOperation();
            Console.WriteLine(secondInstance.GetSampleData());

            /*
             *  12289376
             *  12289376
             *  True
             *  Singleton Sample Data
             */


            //Console.WriteLine("--------------------------- Adapter (Class)");
            //iTarget target = new Adapter();
            //target.Operation();

            Console.WriteLine("--------------------------- Adapter (Object)");
            Target target = new Adapter();

            target.Operation();

            /*
             *  SpecificOperation
             */


            //--------------------------- Bridge
            Console.WriteLine("--------------------------- Bridge");
            Abstract AnotherAbs11 = new RefinedAbstract1(new ConcreteImp1());

            Console.WriteLine(AnotherAbs11.DoSomething());

            Abstract AnotherAbs21 = new RefinedAbstract2(new ConcreteImp1());

            Console.WriteLine(AnotherAbs21.DoSomething());

            Abstract AnotherAbs12 = new RefinedAbstract1(new ConcreteImp2());

            Console.WriteLine(AnotherAbs12.DoSomething());

            Abstract AnotherAbs22 = new RefinedAbstract2(new ConcreteImp2());

            Console.WriteLine(AnotherAbs22.DoSomething());

            /*
             *  RefinedAbstract1 is doing smth. with Imp1
             *  RefinedAbstract2 is doing different things with Imp1
             *  RefinedAbstract1 is doing smth. with Imp2
             *  RefinedAbstract2 is doing different things with Imp2
             */

            //--------------------------- Composite
            Console.WriteLine("--------------------------- Composite");
            Component root    = new Composite("root");
            Component branch1 = new Composite(" - branch1");
            Component branch2 = new Composite(" - branch2");
            Component leaf1   = new Leaf(" - - leaf1");
            Component leaf2   = new Leaf(" - - leaf2");
            Component leaf3   = new Leaf(" - - leaf3");

            root.Add(branch1);
            root.Add(branch2);
            branch1.Add(leaf1);
            branch1.Add(leaf2);
            branch2.Add(leaf3);

            root.Operation();

            /*
             *  root
             *   - branch1
             *   - - leaf1
             *   - - leaf2
             *   - branch2
             *   - - leaf3
             */


            //--------------------------- Decorator
            Console.WriteLine("--------------------------- Decorator");
            AbstractComponent someComponenet = new ConcreteComponenet();
            Decorator         decoratorA     = new ConcreteDecoratorA();
            Decorator         decoratorB     = new ConcreteDecoratorB();

            decoratorA.component = someComponenet;
            decoratorB.component = decoratorA;
            decoratorB.Operation();


            /*
             *  Concrete Componenet Operation
             *  Some State
             *  Some Behavior
             */


            //--------------------------- Facade
            Console.WriteLine("--------------------------- Facade");
            Facade facade = new Facade();

            facade.OperationAB();
            facade.OperationCD();

            /*
             *  Operation A
             *  Operation B
             *  Operation C
             *  Operation D
             */


            //--------------------------- Flyweight
            Console.WriteLine("--------------------------- Flyweight");

            /*
             *  int externalState = 0;
             *
             *  Flyweight flyweight = null;
             *  FlyweightFactory factory = new FlyweightFactory();
             *
             *  flyweight = factory.GetFlyweight("1");
             *  flyweight.Operation(externalState);
             *
             *  flyweight = factory.GetFlyweight("10");
             *  flyweight.Operation(externalState);
             *
             *  flyweight = new UnsharedConcreteFlyweight();
             *  flyweight.Operation(externalState);
             */

            ActorMikeMyers mike = new ActorMikeMyers();

            RoleAustinPowers austin = new RoleAustinPowers(mike);

            austin.Greetings("Hello! I'm Austin Powers!");

            RoleDoctorEvil dr = new RoleDoctorEvil(mike);

            dr.Greetings("Hello! I'm Dr. Evil!");

            /*
             *  Hello! I'm Austin Powers!
             *  Hello! I'm Dr. Evil!
             */


            //--------------------------- Proxy
            Console.WriteLine("--------------------------- Proxy");
            //Subject original = new Original();
            //Subject proxy = new Proxy(original);
            //proxy.Operation();

            Subject anotherProxy = new Proxy();

            anotherProxy.Operation();

            /*
             *  Do Smth
             *  Do Smth
             */


            //--------------------------- ChainOfResponsibility
            Console.WriteLine("--------------------------- ChainOfResponsibility");
            Handler concreteHandler1 = new ConcreteHandler1();
            Handler concreteHandler2 = new ConcreteHandler2();

            concreteHandler1.successor = concreteHandler2;
            concreteHandler1.Operation(1);
            concreteHandler1.Operation(2);

            /*
             *  One
             *  Two
             */


            //--------------------------- Command
            Console.WriteLine("--------------------------- Command");
            Receiver povar = new Receiver();
            Command  zakaz = new ConcreteCommand(povar);
            //Invoker ofichiant = new Invoker(zakaz);
            Invoker ofichiant = new Invoker();

            ofichiant.StoreCommand(zakaz);
            ofichiant.Execute();

            /*
             *  Receiver!
             */


            //--------------------------- Interpreteur
            Console.WriteLine("--------------------------- Interpreteur");
            Context context = new Context
            {
                Vocabulary = 'a',
                Source     = "aaa"
            };

            var expression = new NonTerminalExpression();

            expression.Interpret(context);
            Console.WriteLine(context.Result);

            /*
             *  True
             */

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            #region 抽象工程模式

            // ef 打开和关闭
            AbstractFactory efFactory = new EfFactory();
            efFactory.CreateOpen().Print();
            efFactory.CreateClose().Print();

            // dapper 打开和关闭
            AbstractFactory dapperFactory = new DapperFactory();
            dapperFactory.CreateOpen().Print();
            dapperFactory.CreateClose().Print();

            #endregion

            Console.WriteLine("------------------------------");

            #region 建造者模式

            var director      = new Director();
            var saiyanBuilder = new SaiyanBuilder();
            var naimBuilder   = new NaimBuilder();

            director.Construct(saiyanBuilder);

            // 组装赛亚人
            var saiyanPerson = saiyanBuilder.GetPerson();
            saiyanPerson.Show();

            // 组装那美克人
            director.Construct(naimBuilder);
            var naimPerson = naimBuilder.GetPerson();
            naimPerson.Show();

            #endregion

            Console.WriteLine("------------------------------");

            #region MyRegion

            var mingren1 = new MingrenPrototype();
            var mingren2 = mingren1.Clone() as MingrenPrototype;
            //mingren1 负责攻击
            mingren1.Attack();
            //mingren2 负责保护
            mingren2?.Protect();

            #endregion

            Console.WriteLine("------------------------------");

            #region 适配器模式

            //类的适配器模式
            var baiduMap = new BaiduAdapter();
            baiduMap.Gen();

            //对象的适配器模式
            var baiduMap1 = new BaiduAdapter1();
            baiduMap1.Gen();

            #endregion

            Console.WriteLine("------------------------------");

            #region 桥接模式

            DbControlAbstract dbControlAbstract = new DbControl();
            // Sql Server
            dbControlAbstract.Db = new SqlServerDb();
            dbControlAbstract.Open();
            dbControlAbstract.Add();
            dbControlAbstract.Close();

            // MySql
            dbControlAbstract.Db = new MySqlDb();
            dbControlAbstract.Open();
            dbControlAbstract.Add();
            dbControlAbstract.Close();

            #endregion

            Console.WriteLine("------------------------------");

            #region 装饰者模式

            // SqlServerDbHelper
            DbHelper dbHelper = new SqlServerDbHelper();
            // check
            Decorator decorator = new CheckDecorator(dbHelper);
            decorator.Add();

            #endregion

            Console.WriteLine("------------------------------");

            //透明式
            Car car = new Motorcycle();
            car.Travel();
            car.Two(new SuvCar());
            car.Ten(new SuvCar());

            car = new SuvCar();
            car.Travel();
            car.Two(new SuvCar());
            car.Ten(new SuvCar());

            //安全式
            Car1 car1 = new Motorcycle1();
            car1.Travel();

            Car1 bus = new Bus();
            bus.Travel();
            ((FourCar)bus).Two(new Bus());
            ((FourCar)bus).Ten(new Bus());

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            #region 简单工厂

            var add = Factory.GetOperat("1");
            var sub = Factory.GetOperat("2");

            #endregion

            Console.WriteLine("------------------------------");

            #region 单例模式

            Singleton.GetInstance();
            var s1 = Singleton1.Instance;
            var s2 = Singleton2.Instance;

            #endregion

            Console.WriteLine("------------------------------");

            #region 抽象工程模式

            // ef 打开和关闭
            AbstractFactory efFactory = new EfFactory();
            efFactory.CreateOpen().Print();
            efFactory.CreateClose().Print();

            // dapper 打开和关闭
            AbstractFactory dapperFactory = new DapperFactory();
            dapperFactory.CreateOpen().Print();
            dapperFactory.CreateClose().Print();

            #endregion

            Console.WriteLine("------------------------------");

            #region 建造者模式

            var director      = new Director();
            var saiyanBuilder = new SaiyanBuilder();
            var naimBuilder   = new NaimBuilder();

            director.Construct(saiyanBuilder);

            // 组装赛亚人
            var saiyanPerson = saiyanBuilder.GetPerson();
            saiyanPerson.Show();

            // 组装那美克人
            director.Construct(naimBuilder);
            var naimPerson = naimBuilder.GetPerson();
            naimPerson.Show();

            #endregion

            Console.WriteLine("------------------------------");

            #region 原型模式

            var mingren1 = new MingrenPrototype();
            var mingren2 = mingren1.Clone() as MingrenPrototype;
            //mingren1 负责攻击
            mingren1.Attack();
            //mingren2 负责保护
            mingren2?.Protect();

            #endregion

            Console.WriteLine("------------------------------");

            #region 适配器模式

            //类的适配器模式
            var baiduMap = new BaiduAdapter();
            baiduMap.Gen();

            //对象的适配器模式
            var baiduMap1 = new BaiduAdapter1();
            baiduMap1.Gen();

            #endregion

            Console.WriteLine("------------------------------");

            #region 桥接模式

            DbControlAbstract dbControlAbstract = new DbControl();
            // Sql Server
            dbControlAbstract.Db = new SqlServerDb();
            dbControlAbstract.Open();
            dbControlAbstract.Add();
            dbControlAbstract.Close();

            // MySql
            dbControlAbstract.Db = new MySqlDb();
            dbControlAbstract.Open();
            dbControlAbstract.Add();
            dbControlAbstract.Close();

            #endregion

            Console.WriteLine("------------------------------");

            #region 装饰者模式

            // SqlServerDbHelper
            DbHelper dbHelper = new SqlServerDbHelper();
            // check
            Decorator decorator = new CheckDecorator(dbHelper);
            decorator.Add();

            #endregion

            Console.WriteLine("------------------------------");

            #region 组合模式

            //透明式
            Car car = new Motorcycle();
            car.Travel();
            car.Two(new SuvCar());
            car.Ten(new SuvCar());

            car = new SuvCar();
            car.Travel();
            car.Two(new SuvCar());
            car.Ten(new SuvCar());

            //安全式
            Car1 car1 = new Motorcycle1();
            car1.Travel();

            Car1 bus = new Bus();
            bus.Travel();
            ((FourCar)bus).Two(new Bus());
            ((FourCar)bus).Ten(new Bus());

            #endregion

            Console.WriteLine("------------------------------");

            #region 外观模式

            var facade = new Facade();
            facade.Buy();

            #endregion

            Console.WriteLine("------------------------------");

            #region 享元模式

            foreach (var item in new[] { "a", "b", "c", "a", "b" })
            {
                var flyweight = FlyweightFactory.GetFlyweight(item);
                flyweight.Operation(1);
            }
            Console.WriteLine(FlyweightFactory.DicFlyweight.Count);

            #endregion

            Console.WriteLine("------------------------------");

            #region 代理模式

            var proxy = new Proxy();
            proxy.Do("上网");

            #endregion

            Console.WriteLine("------------------------------");

            #region 模板方法模式

            var chain = new ChinaBank();
            chain.Get();
            var shBank = new ShanghaiBank();
            shBank.Get();

            #endregion

            Console.WriteLine("------------------------------");

            #region 命令模式

            var r = new Receiver1();
            var c = new CommandImp(r);
            var i = new Invoke(c);
            i.ExecuteCommand();

            #endregion

            Console.WriteLine("------------------------------");

            #region 迭代器模式

            ICollection list     = new Collection();
            var         iterator = list.GetIterator();

            while (iterator.MoveNext())
            {
                var j = (int)iterator.GetCurrent();
                Console.WriteLine(j.ToString());
                iterator.Next();
            }

            #endregion

            Console.WriteLine("------------------------------");

            #region 观察者模式

            HousingDeveloper hd = new WuhanHousingDeveloper();
            hd.Add(new NoHouseObserver("A"));
            hd.Add(new NoHouseObserver("B"));
            hd.Add(new NoHouseObserver("C"));

            hd.Add(new HasHouseObserver("D"));
            hd.Add(new HasHouseObserver("E"));
            hd.Add(new HasHouseObserver("F"));

            hd.Notify();

            #endregion

            Console.WriteLine("------------------------------");

            #region 中介者模式

            var buyer    = new Buyer();
            var seller   = new Seller();
            var mediator = new MediatorImp(buyer, seller);
            buyer.MoneyChange(5, mediator);
            seller.MoneyChange(10, mediator);

            #endregion

            Console.WriteLine("------------------------------");

            #region 状态模式

            var user = new User("admin");
            for (var j = 0; j < 10; j++)
            {
                user.Recharge(20 * j);
            }

            #endregion

            Console.WriteLine("------------------------------");

            #region 策略模式

            var context = new NotifyContext(new EmailStragety());
            context.Send("新的消息");
            context.Stragety = new SmsStragety();
            context.Send("第二条消息");

            #endregion

            Console.WriteLine("------------------------------");

            #region 责任链模式

            var leaveRequest1 = new LeaveRequest("张三", 1);
            var leaveRequest2 = new LeaveRequest("李四", 4);
            var leaveRequest3 = new LeaveRequest("王五", 7);
            var leaveRequest4 = new LeaveRequest("赵六", 11);

            Approver projectManager = new ProjectManager("项目管理者");
            Approver departManager  = new DepartManager("部门管理者");
            Approver ceo            = new Ceo("CEO");

            // 设置责任链
            projectManager.NextApprover = departManager;
            departManager.NextApprover  = ceo;

            // 处理请求
            projectManager.ProcessRequest(leaveRequest1);
            projectManager.ProcessRequest(leaveRequest2);
            projectManager.ProcessRequest(leaveRequest3);
            projectManager.ProcessRequest(leaveRequest4);

            #endregion

            Console.WriteLine("------------------------------");

            #region 访问者模式

            Visitor visitor = new VisitorImp();
            var     app     = new AppStructure(visitor);

            FinancialManagement financialManagement = new Bank();
            financialManagement.SaveMoney();
            app.Process(financialManagement);


            financialManagement = new Yeb();
            financialManagement.SaveMoney();
            app.Process(financialManagement);


            financialManagement = new Jj();
            financialManagement.SaveMoney();
            app.Process(financialManagement);

            #endregion

            Console.WriteLine("------------------------------");

            #region 备忘录模式

            var persons = new List <ContactPerson>()
            {
                new ContactPerson {
                    Name = "A", MobileNumber = "13533332222"
                },
                new ContactPerson {
                    Name = "B", MobileNumber = "13966554433"
                },
                new ContactPerson {
                    Name = "C", MobileNumber = "13198765544"
                }
            };

            //手机名单发起人
            var mobileOriginator = new MobileBackOriginator(persons);
            mobileOriginator.Show();

            // 创建备忘录并保存备忘录对象
            var manager = new MementoManager {
                ContactPersonMemento = mobileOriginator.CreateMemento()
            };

            // 更改发起人联系人列表
            Console.WriteLine("----移除最后一个联系人--------");
            mobileOriginator.ContactPersonList.RemoveAt(2);
            mobileOriginator.Show();

            // 恢复到原始状态
            Console.WriteLine("-------恢复联系人列表------");
            mobileOriginator.RestoreMemento(manager.ContactPersonMemento);
            mobileOriginator.Show();

            #endregion

            #region 解释器模式

            Console.WriteLine("------------------------------");

            const string roman = "六千四百五十二";
            var          interpreterContext = new InterpreterContext(roman);
            var          tree = new ArrayList
            {
                new GeExpression(),
                new ShiExpression(),
                new BaiExpression(),
                new QianExpression()
            };
            foreach (Expression exp in tree)
            {
                exp.Interpreter(interpreterContext);
            }
            Console.Write(interpreterContext.Data);

            #endregion

            Console.ReadKey();
        }