当前位置:编程学习 > C/C++ >>

Design patterns IV : Decorator/Wrapper Pattern

/*

Author: Jiangong SUN

*/

Decorators provide a flexible alternative to subclassing for extending functionality.

 

public class DecoratorPattern
    {
         /*
         * decorator和wrapper是一个设计模式
         * 用途:用来扩展已有类的功能
         * 实现步骤:
         * 先定义一个inte易做图ce,然后建立一个类继承这个接口,并实现接口里的方法
         * 建立一个包装类继承同一个接口,并在这个包装类的构造器中使用接口的实例做参数。
         * 建立实际包装类继承包装类,重写接口定义的方法,并可以创建自己特有的方法。
         */
        public inte易做图ce IComponent
        {
            void Operation();
        }

        public class ConcreteComponent : IComponent
        {
            public void Operation()
            {
                Console.WriteLine("ConcreteComponent operation");
            }
        }

        public class Decorator : IComponent
        {
            private readonly IComponent _decoratedComponent;

            public Decorator(IComponent iComponent)
            {
                _decoratedComponent = iComponent;
            }

            public void Operation()
            {
                _decoratedComponent.Operation();
            }
        }

        public class ConcreteDecoratorA : Decorator
        {
            private string _addState;

            public ConcreteDecoratorA(Decorator decorator)
                : base(decorator)
            {
            }

            public new void Operation()
            {
                base.Operation();
                _addState = "New State";
                Console.WriteLine("ConcreteDecoratorA.Operation");
            }
            public void CustomOperation()
            {
                Console.WriteLine("ConcreteDecoratorA custom Operation");
            }
        }

        public static void Main()
        {
            ConcreteComponent concreteComponent = new ConcreteComponent();
            concreteComponent.Operation();

            Decorator decorator = new Decorator(concreteComponent);
            ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(decorator);
            concreteDecoratorA.Operation();
            concreteDecoratorA.CustomOperation();

            Console.ReadKey();
        }
    }

 


 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,