예제 #1
0
        static void Main(string[] args)
        {
            //================FACTORY====================================================
            AnswerFactory yesFactory = AnswerFactory.getFactory(typeOfFactory.yesFactory);
            bool          yesAnswer  = yesFactory.getAnswer();

            Console.WriteLine("YES ANSWER: " + yesAnswer);
            AnswerFactory noFactory = AnswerFactory.getFactory(typeOfFactory.noFactory);
            bool          noAnswer  = noFactory.getAnswer();

            Console.WriteLine("NO ANSWER: " + noAnswer);
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================SINGLETON==================================================
            Singleton s1 = Singleton.getInstance();

            Console.WriteLine("Singleton state: " + s1.state);
            Singleton s2 = Singleton.getInstance();

            s2.state = true;
            Console.WriteLine("Singleton state: " + s1.state);
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================DECORATOR==================================================
            ConcreteComponent component = new ConcreteComponent();

            component.Operation();
            ConcreteDecorator decorator = new ConcreteDecorator();

            decorator.SetComponent(component);
            decorator.Operation();
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================STRATEGY===================================================
            Context context;

            context = new Context(new StrategyA());
            context.ContextInterface();
            context = new Context(new StrategyB());
            context.ContextInterface();
            //===========================================================================
            Console.WriteLine();
            Console.WriteLine();
            //================OBSERVER===================================================
            Observable provider = new Observable();
            Observer   reciever = new Observer();

            reciever.Subscribe(provider);
            provider.sendString();
            //===========================================================================

            Console.ReadLine();
        }
예제 #2
0
        static void Invock3()
        {
            //基础功能类
            var c = new ConcreteComponent();
            //装饰A类
            var dA = new ConcreteDecoratorA();
            //装饰B类
            var dB = new ConcreteDecoratorB();

            dA.Component = c;
            dB.Component = dA;
            dB.Operation();
        }
예제 #3
0
        static void Main(string[] args)
        {
            var a = DesignPatterns.Singleton.Singleton.GetInstanceUnSafe;

            var b = DesignPatterns.Singleton.Singleton.GetInstanceUnSafe;

            if (a == b)
            {
                Console.WriteLine("Singleton works, both variables contain the same instance.");
            }
            else
            {
                Console.WriteLine("Singleton failed, variables contain different instances.");
            }

            var c = NotSingleton.GetInstanceUnSafe;

            var d = NotSingleton.GetInstanceUnSafe;

            if (c == d)
            {
                Console.WriteLine("Singleton works, both variables contain the same instance.");
            }
            else
            {
                Console.WriteLine("Singleton failed, variables contain different instances.");
            }

            Client client = new Client();

            var simple = new ConcreteComponent();

            Console.WriteLine("Client: I get a simple component:");
            client.ClientCode(simple);
            Console.WriteLine();

            ConcreteDecoratorA decorator1 = new ConcreteDecoratorA(simple);
            ConcreteDecoratorB decorator2 = new ConcreteDecoratorB(decorator1);
            ConcreteDecoratorC decorator3 = new ConcreteDecoratorC(decorator2);

            Console.WriteLine("Client: Now I've got a decorated component:");
            client.ClientCode(decorator3);

            new DesignPatterns.Factory.Client().Main();
        }
예제 #4
0
        private static void DemonstrateDecorator()
        {
            // First create a component that should be decorated
            var component = new ConcreteComponent();

            // Then create a decorator and supply the component to it, this expands the components functionality.
            var decoratorA = new ConcreteDecoratorA(component);

            // The pattern allows both adding the base component, or another decorator.
            var decoratorB1 = new ConcreteDecoratorB(component);
            var decoratorB2 = new ConcreteDecoratorB(decoratorA); // Expands the functionality even more.

            component.Operation();
            decoratorA.Operation();
            decoratorB1.Operation();
            decoratorB2.Operation();
            decoratorB2.AddedBehavior();
        }
예제 #5
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Component skill = new ConcreteComponent();

            if (Isxuanyun)
            {
                ConcreteDecoratorXuanyun xunyun = new ConcreteDecoratorXuanyun()
                {
                };
                xunyun.SetComponent(skill);
                skill = xunyun;
            }
            if (Iszhongdu)
            {
                ConcreteDecoratorZhongdu xunyun = new ConcreteDecoratorZhongdu()
                {
                };
                xunyun.SetComponent(skill);
                skill = xunyun;
            }
            if (Iszhuoshao)
            {
                ConcreteDecoratorZhuoshao xunyun = new ConcreteDecoratorZhuoshao()
                {
                };
                xunyun.SetComponent(skill);
                skill = xunyun;
            }
            if (Isbingdong)
            {
                ConcreteDecoratorBingdong xunyun = new ConcreteDecoratorBingdong()
                {
                };
                xunyun.SetComponent(skill);
                skill = xunyun;
            }
            TbMsg.Text += skill.Attack();
        }