Exemple #1
0
        static void Main()
        {
            Trace.Listeners.Add(new TextWriterTraceListener("SoftwareDesignPaternsOutput.log"));
            Trace.AutoFlush = true;
            Trace.Indent();
            Console.WriteLine("Output text file location ==> \n");
            Console.WriteLine("./SoftwareDesignPatterns/DesignPatterns.ConsoleApp/bin/Debug/SoftwareDesignPatternsOutput.log");
            Console.WriteLine("\nAlternatively, see the visual studio output window.");

            #region Writing All Output in Trace File
            Trace.WriteLine("=================== PROGRAM STARTS HERE ===================" + Environment.NewLine);

            Trace.WriteLine("========== FACTORY PATTERN RESULT ===========" + Environment.NewLine);
            FactoryPatternDemo.Output();

            Trace.WriteLine("========== ABSTRACT FACTORY PATTERN RESULT ===========" + Environment.NewLine);
            AbstractFactoryPatternDemo.Output();

            Trace.WriteLine("========== SINGLETON PATTERN RESULT ===========" + Environment.NewLine);
            SingletonPatternDemo.Output();

            Trace.WriteLine("========== BUILDER PATTERN RESULT ===========" + Environment.NewLine);
            BuilderPatternDemo.Output();

            Trace.WriteLine("========== PROTOTYPE PATTERN RESULT ===========" + Environment.NewLine);
            PrototypePatternDemo.Output();

            Trace.WriteLine("========== ADAPTER PATTERN RESULT ===========" + Environment.NewLine);
            AdapterPatternDemo.Output();

            Trace.WriteLine("========== BRIDGE PATTERN RESULT ===========" + Environment.NewLine);
            BridgePatternDemo.Output();

            Trace.WriteLine("========== FILTER PATTERN RESULT ===========" + Environment.NewLine);
            FilterPatternDemo.Output();

            Trace.WriteLine("========== COMPOSITE PATTERN RESULT ===========" + Environment.NewLine);
            CompositePatternDemo.Output();

            Trace.WriteLine("========== DECORATOR PATTERN RESULT ===========" + Environment.NewLine);
            DecoratorPatternDemo.Output();

            Trace.WriteLine("========== FACADE PATTERN RESULT ===========" + Environment.NewLine);
            FacadePatternDemo.Output();

            Trace.WriteLine("========== FLYWEIGHT PATTERN RESULT ===========" + Environment.NewLine);
            FlyweightPatternDemo.Output();

            Trace.WriteLine("========== PROXY PATTERN RESULT ===========" + Environment.NewLine);
            ProxyPatternDemo.Output();

            Trace.WriteLine("========== PROXY PATTERN RESULT ===========" + Environment.NewLine);
            ChainPatternDemo.Output();

            Trace.WriteLine("========== COMMAND PATTERN RESULT ===========" + Environment.NewLine);
            CommandPatternDemo.Output();

            Trace.WriteLine("========== INTERPRETER PATTERN RESULT ===========" + Environment.NewLine);
            InterpreterPatternDemo.Output();

            Trace.WriteLine("========== ITERATOR PATTERN RESULT ===========" + Environment.NewLine);
            IteratorPatternDemo.Output();

            Trace.WriteLine("========== MEDIATOR PATTERN RESULT ===========" + Environment.NewLine);
            MediatorPatternDemo.Output();

            Trace.WriteLine("========== MEMENTO PATTERN RESULT ===========" + Environment.NewLine);
            MementoPatternDemo.Output();

            Trace.WriteLine("========== OBSERVER PATTERN RESULT ===========" + Environment.NewLine);
            ObserverPatternDemo.Output();

            Trace.WriteLine("========== STATE PATTERN RESULT ===========" + Environment.NewLine);
            StatePatternDemo.Output();

            Trace.WriteLine("========== NULL OBJECT PATTERN RESULT ===========" + Environment.NewLine);
            NullPatternDemo.Output();

            Trace.WriteLine("========== STRATEGY PATTERN RESULT ===========" + Environment.NewLine);
            StrategyPatternDemo.Output();

            Trace.WriteLine("========== TEMPLATE PATTERN RESULT ===========" + Environment.NewLine);
            TemplatePatternDemo.Output();

            Trace.WriteLine("========== VISITOR PATTERN RESULT ===========" + Environment.NewLine);
            VisitorPatternDemo.Output();

            Trace.WriteLine("========== MVC PATTERN RESULT ===========" + Environment.NewLine);
            MvcPatternDemo.Output();

            Trace.WriteLine("========== BUSINESS DELEGATE PATTERN RESULT ===========" + Environment.NewLine);
            BusinessDelegatePatternDemo.Output();

            Trace.WriteLine("========== COMPOSITE ENTITY PATTERN RESULT ===========" + Environment.NewLine);
            CompositeEntityPatternDemo.Output();

            Trace.WriteLine("========== DATA ACCESS OBJECT PATTERN RESULT ===========" + Environment.NewLine);
            DaoPatternDemo.Output();

            Trace.WriteLine("========== FRONT CONTROLLER PATTERN RESULT ===========" + Environment.NewLine);
            FrontControllerPatternDemo.Output();

            Trace.WriteLine("========== INTERCEPTING FILTER PATTERN RESULT ===========" + Environment.NewLine);
            InterceptingFilterPatternDemo.Output();

            Trace.WriteLine("========== SERVICE LOCATOR PATTERN RESULT ===========" + Environment.NewLine);
            ServiceLocatorPatternDemo.Output();

            Trace.WriteLine("========== TRANSFER OBJECT PATTERN RESULT ===========" + Environment.NewLine);
            TransferObjectPatternDemo.Output();

            Trace.WriteLine("=================== ROGRAM ENDS HERE ===================");
            #endregion

            Trace.Unindent();
            Trace.Flush();
            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            #region 1.创建者模式
            //工厂模式测试
            Patterns.FactoryPattern.ShapeFactory.Test();
            //抽象工厂模式测试
            FactoryProducer.Test();
            //单例模式测试
            Singleton.Test();
            //建造者模式
            FoodPattern.Test();
            //原型模式
            PrototypeDemo.Test();
            #endregion

            #region 2.结构型模式
            //适配器模式
            AdapterPatternDemo.Test();
            //桥接模式
            BridgePatternDemo.Test();
            //过滤器模式
            FilterPatternDemo.Test();
            //组合模式
            CompositePattern.Test();
            //装饰器模式
            DecoratorPatternDemo.Test();
            //外观模式
            FacadePatternDemo.Test();
            //享元模式
            FlyWeightPatternDemo.Test();
            //代理模式
            ProxyPatternDemo.Test();
            #endregion

            #region 3.行为型模式
            //责任链模式
            ChainPatternDemo.Test();
            //命令模式
            CommandPatternDemo.Test();
            //解释器模式
            InterpreterPatternDemo.Test();
            //迭代器模式
            IteratorPatternDemo.Test();
            //中介者模式
            MediatorPatternDemo.Run();
            //备忘录模式
            MementoPatternDemo.Run();
            //观察者模式
            ObserverPatternDemo.Run();
            //状态模式
            StatePatternDemo.Run();
            //空对象模式
            NullObjectPatternDemo.Run();
            //策略模式
            StrategyPatternDemo.Run();
            //模板模式
            TemplatePatternDemo.Run();
            //访问者模式
            VisitorPatternDemo.Run();
            #endregion

            #region 4.J2EE模式
            //MVC模式
            MVCPatternDemo.Run();
            //业务代表模式
            BusinessDelegatePatternDemo.Run();
            //组合实体模式
            CompositeEntityPatternDemo.Run();
            //数据访问对象模式
            DataAccessObjectPatternDemo.Run();
            //前端控制器模式
            FrontControllerPatternDemo.Run();
            //拦截过滤器模式
            InterceptingFilterPatternDemo.Run();
            //服务定位器模式
            ServiceLocatorPatternDemo.Run();
            //传输对象模式
            TransferObjectPatternDemo.Run();
            #endregion

            Console.ReadLine();
        }
Exemple #3
0
        private static void InterpreterPatternDemo()
        {
            InterpreterPatternDemo demo = new InterpreterPatternDemo();

            demo.Test();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton Pattern");

            // Singleton Pattern
            SingleObject singleObject = SingleObject.Instance();

            singleObject.ShowMessage();

            Console.WriteLine("\nBuilder Pattern");

            // Builder Pattern
            MealBuilder mealBuilder = new MealBuilder();

            Meal vegMeal = mealBuilder.PrepareVegMeal();

            Console.WriteLine("Veg Meal");
            vegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + vegMeal.GetCost());

            Meal nonVegMeal = mealBuilder.PrepareNonVegMeal();

            Console.WriteLine("NonVeg Meal");
            nonVegMeal.ShowItems();
            Console.WriteLine("Total Cost : " + nonVegMeal.GetCost());

            Console.WriteLine("\nAbstract Factory");

            // Abstract Factory Pattern
            AbstractFactory shapeFactory = FactoryProducer.GetFactory("shape");
            IShape          circleShape  = shapeFactory.GetShape("circle");

            circleShape.Draw();

            AbstractFactory colorFactory = FactoryProducer.GetFactory("color");
            IColor          blueColor    = colorFactory.GetColor("blue");

            blueColor.Fill();

            Console.WriteLine("\nAdapter");

            //Adapter Pattern
            AudioPlayer audioPlayer = new AudioPlayer();

            audioPlayer.Play("mp4");
            audioPlayer.Play("flac");
            audioPlayer.Play("vlc");

            Console.WriteLine("\nFacade");

            //Facade Pattern
            ShapeMaker shapeMaker = new ShapeMaker();

            shapeMaker.DrawCircle();
            shapeMaker.DrawRectangle();
            shapeMaker.DrawSquare();

            Console.WriteLine("\nDecorator doesn't work");

            //Decorator Pattern
            IDecoratorShape rectangle    = new RectangleDecorator();
            IDecoratorShape redRectangle = new RedShapeDecorator(new RectangleDecorator());

            Console.WriteLine("Rectangle with no color");
            rectangle.Draw();
            Console.WriteLine("Rectangle with color");
            redRectangle.Draw();

            Console.WriteLine("\nComposite");

            //Composite Pattern
            Employee DSI           = new Employee("employee1", "DSI", 100000);
            Employee chefDeProjet1 = new Employee("employee2", "Chef de projet", 60000);
            Employee chefDeProjet2 = new Employee("employee3", "Chef de projet", 60000);
            Employee dev1          = new Employee("employee4", "Développeur", 40000);
            Employee dev2          = new Employee("employee5", "Développeur", 40000);

            DSI.Add(chefDeProjet1);
            DSI.Add(chefDeProjet2);
            chefDeProjet1.Add(dev1);
            chefDeProjet2.Add(dev2);

            Console.WriteLine(DSI.Details());
            foreach (Employee e1 in DSI.GetSubordinates())
            {
                Console.WriteLine(e1.Details());
                foreach (Employee e2 in e1.GetSubordinates())
                {
                    Console.WriteLine(e2.Details());
                }
            }

            Console.WriteLine("\nBridge");

            //Bridge Pattern
            BridgeShape shape1 = new BridgeCircle(10, 10, 1, new GreenCircle());
            BridgeShape shape2 = new BridgeCircle(100, 100, 10, new RedCircle());

            shape1.Draw();
            shape2.Draw();

            Console.WriteLine("\nCommand");

            //Command Pattern
            Stock    stock1   = new Stock("laptop", 100);
            BuyStock buyStock = new BuyStock(stock1);

            Stock     stock2    = new Stock("screen", 30);
            SellStock sellStock = new SellStock(stock2);

            Broker broker = new Broker();

            broker.TakeOrder(buyStock);
            broker.TakeOrder(sellStock);
            broker.PlaceOrders();

            Console.WriteLine("\nInterpreter");

            //Interpreter Pattern
            IExpression isMale    = InterpreterPatternDemo.GetMaleExpression();
            IExpression isMarried = InterpreterPatternDemo.GetMarriedWomanExpression();

            Console.WriteLine("John is male ? " + isMale.Interpret("john"));
            Console.WriteLine("Barbara is male ? " + isMale.Interpret("barbara"));

            Console.WriteLine("Julie is married ? " + isMarried.Interpret("julie married"));
            Console.WriteLine("Bob is married ? " + isMarried.Interpret("bob married"));
        }