Пример #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
        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();
        }