コード例 #1
0
        private static void DecoratorPatternExample()
        {
            var concreteComponent = new ConcreteComponent();
            var concreteDecorator = new ConcreteDecorator(concreteComponent);

            concreteDecorator.Operation();
        }
コード例 #2
0
        public void TestCase1()
        {
            var component1 = new ConcreteComponent();
            var component2 = new ConcreteDecorator(component1);

            component2.Operation();
        }
コード例 #3
0
        public void DecoratorPatternTest()
        {
            var concrete  = new ConcreteComponent();
            var decorator = new ConcreteDecorator(concrete);

            concrete.Should().Be(decorator.Component);
        }
コード例 #4
0
ファイル: Test.cs プロジェクト: onurak/GOFDesignPatterns
        public static void Test()
        {
            var concreteComponent = new ConcreteComponent("concrete property", "component property");
            concreteComponent.Operation();

            var concreteDecorator = new ConcreteDecorator(concreteComponent);
            concreteDecorator.AnotherOperation();
        }
コード例 #5
0
        public static void Run()
        {
            var component = new ConcreteComponent();

            var decoratorA = new ConcreteDecorator(component);
            var decoratorB = new ConcreteDecorator(decoratorA);

            decoratorB.Execute();
        }
コード例 #6
0
        public void DecoratorUsage()
        {
            ConcreteComponent c  = new ConcreteComponent();
            ConcreteDecorator d1 = new ConcreteDecorator(c);
            ConcreteDecorator d2 = new ConcreteDecorator(d1);

            d2.Operation();

            Assert.IsTrue(true);
        }
コード例 #7
0
        private static void DecoratorModeTest()
        {
            Component component, component1, component2;

            component  = new ConcreteComponent();
            component1 = new ConcreteDecorator(component);
            //二次装饰
            component2 = new ConcreteDecorator2(component1);
            component2.Operation();
        }
コード例 #8
0
        public void TestDecorator()
        {
            DecoratorComponentBase component = new DecoratorComponent();

            component.Operation();

            ConcreteDecorator decorator = new ConcreteDecorator(component);

            decorator.Operation();
        }
コード例 #9
0
        static void Main(string[] args)
        {
            Engine   engine    = new Engine();
            Consumer consumer  = new Consumer("consumer1", engine);
            Consumer consumer2 = new Consumer("consumer2", engine);

            engine.SwitchGear();
            consumer.Unsubscribe();
            engine.SwitchGear();

            Console.WriteLine("");
            Console.WriteLine("============ Structural patterns (realize relationships between entities) ============");
            Console.WriteLine("~~~~~~~~~~~~~ Facade (A single class that represents an entire subsystem) ~~~~~~~~~~~~~");
            Structural.Facade facade = new Structural.Facade();
            facade.AddMethod();
            Console.WriteLine("~~~~~~~~~~~~~ Decorator (Attach additional responsibilities to an object dynamically) ~~~~~~~~~~~~~");
            ConcreteDecorator decorator         = new ConcreteDecorator();
            ConcreteComponent concreteComponent = new ConcreteComponent();

            decorator.SetComponent(concreteComponent);
            decorator.Operation();

            Console.WriteLine("");
            Console.WriteLine("============ Behavioral patterns (communications between entities) ============");
            Console.WriteLine("~~~~~~~~~~~~~ Observer (when one object changes state, all its dependents are notified and updated automatically) ~~~~~~~~~~~~~");

            ConcreteSubject  subject  = new ConcreteSubject();
            ConcreteObserver observer = new ConcreteObserver(subject, "X");

            subject.Attach(observer);
            // Change subject and notify observers
            subject.State = "ABC";
            subject.Notify();
            Console.WriteLine("~~~~~~~~~~~~~ Strategy (Define a family of algorithms, encapsulate each one, and make them interchangeable) ~~~~~~~~~~~~~");
            Context context = new Context(new SortStrategy1());

            context.ContextSortInterface();
            context = new Context(new SortStrategy2());
            context.ContextSortInterface();

            Console.WriteLine("============ Structural patterns ============");
            Console.WriteLine("~~~~~~~~~~~~~ Singleton (Ensure a class has only one instance and provide a global point of access to it) ~~~~~~~~~~~~~");
            Cache instance1 = Cache.Instance();
            Cache instance2 = Cache.Instance();

            Console.WriteLine("instance1 == instance2? {0}", instance1 == instance2);
            Console.WriteLine("~~~~~~~~~~~~~ FactoryMethod ~~~~~~~~~~~~~");
            IComponent component = Creator.Create("first");
        }
コード例 #10
0
    void Update()
    {
        Client client = new Client();

        var simple = new ConcreteComponent();

        Debug.Log("Простой компонент");
        client.ClientCode(simple);


        ConcreteDecorator decorator = new ConcreteDecorator(simple);

        Debug.Log("Декорированый компонент");
        client.ClientCode(decorator);
    }
コード例 #11
0
        public void CommandPatternTest()
        {
            var target = new Target();

            target.DoSth();

            var targetDecorated = new ConcreteDecorator(target);

            targetDecorated.DoSth();
            targetDecorated.DoSthElse();

            var targetDecoratedAgain = new ConcreteSecondLevelDecorator(targetDecorated);

            targetDecoratedAgain.DoSth();
            targetDecoratedAgain.DoSthElse();
            targetDecoratedAgain.DoSthCompletelyElse();
        }
コード例 #12
0
        public void Show()
        {
            var decorator1 = new ConcreteDecorator(new ConcreteComponent("Comp1"), 1);

            decorator1.Operation();
        }
コード例 #13
0
        static void Main(string[] args)
        {
            /**
             * ITERATOR
             **/

            ConcreteAggregate collection = new ConcreteAggregate();

            Iterator.Iterator iterator = collection.createIterator();

            //use to iterator to go through the collection
            while (!iterator.isDone())
            {
                Console.WriteLine(iterator.Next().ToString());
            }

            /**
             * OBSERVER
             **/

            Subject subject = new ConcreteSubject(SubjectState.Red);

            subject.Attach(new ConcreteObserver());
            subject.Attach(new ConcreteObserver());
            subject.Attach(new ConcreteObserver());

            ((ConcreteSubject)subject).SubjectState = SubjectState.Blue;

            /**
             * DECORATOR
             **/

            //we create a class with a base function
            Component component = new ConcreteComponent();

            //we create the function we want the add to the first class
            Decorator.Decorator decorator = new ConcreteDecorator();

            //the decorator gets the functionality of the class we want to extend
            decorator.SetBaseFuntionality(component);

            //both ops are called
            decorator.Operation();

            /**
             * COMMAND
             **/

            Command.Command command = new ConcreteCommand(new Receiver());

            //create an invoker which in turn calls the command which IN TURN calls the receiver
            Invoker invoker = new Invoker();

            invoker.SetCommand(command);
            invoker.ExecuteCommand();

            /**
             * COMPOSITE
             **/

            //instanciate differents persons
            Composite.Component component1 = new Composite.Composite("Bob");
            Composite.Component component2 = new Composite.Composite("Patrick");
            Composite.Component component3 = new Composite.Composite("Johny");
            Composite.Component component4 = new Composite.Composite("Pierre");

            //affects differents persons to a superior
            component1.Add(component2);
            component2.Add(component3);
            component2.Add(component4);

            //default method for every objects in the tree
            component1.Operation();

            /**
             * MEMENTO
             **/

            Originator originator = new Originator();
            CareTaker  careTaker  = new CareTaker();

            //create a couple of states
            originator.State = "state1";
            originator.State = "state2";
            //save at some point
            careTaker.Add(originator.SaveToMemento());
            originator.State = "state3";

            //restore state at the time of the save
            originator.RestoreStateFromMemento(careTaker.Get(0));

            Console.WriteLine(originator.State);

            Console.ReadKey();
        }
コード例 #14
0
ファイル: UnitTestDecorator.cs プロジェクト: pmachard/Pattern
        public void TestMethod_DecoratorConcret()
        {
            var composant = new ConcreteDecorator();

            Assert.AreEqual("ConcreteDecorator", composant.Method());
        }