欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

大话设计-装饰模式

程序员文章站 2022-07-06 12:26:06
装饰模式的核心:被装饰对象(最原始),装饰对象(带有装饰功能的被装饰对象)。 假设有一个被装饰对象叫“ConcreteComponent ”,两个装饰对象叫“ConcreteDecoratorA”、“ConcreteDecoratorB”。装饰是锦上添花的事情,ConcreteComponent 被 ......

装饰模式的核心:被装饰对象(最原始),装饰对象(带有装饰功能的被装饰对象)。

假设有一个被装饰对象叫“concretecomponent ”,两个装饰对象叫“concretedecoratora”、“concretedecoratorb”。装饰是锦上添花的事情,concretecomponent 被装饰之前已经最好了最基本的工作,concretedecoratoraconcretedecoratorb只是在这个基本工作里额外加了些工作

装饰是无序的,可以concretedecoratora先装饰concretecomponent,然后concretedecoratorb装饰concretedecoratora。也可以concretedecoratorb先装饰concretecomponent,然后concretedecoratora装饰concretedecoratorb

被装饰对象和装饰类是否需要抽象跟这里的装饰模式无关。

using system;

namespace consoleapp4
{
    class program
    {
        public static void main(string[] args)
        {
            concretecomponent concretecomponent = new concretecomponent();
            concretedecoratora concretedecoratora = new concretedecoratora();
            concretedecoratorb concretedecoratorb = new concretedecoratorb();

            concretedecoratora.setcomponent(concretecomponent);
            concretedecoratorb.setcomponent(concretedecoratora);
            concretedecoratorb.operation();
        }
    }
    
    abstract class component
    {
        public abstract void operation();
    }

    class concretecomponent:component
    {
        public override void operation()
        {
            console.writeline("具体对象的操作");
        }
    }

     abstract class decorator : component
    {
        protected component component;
        public void setcomponent(component component)
        {
            this.component = component;
        }
        public override void operation()
        {
            if (component != null)
            {
                component.operation();
            }
        }
    }

     class concretedecoratora : decorator
    {
        private string addstate;
        public override void operation()
        {
            base.operation();
            addstate = "a addstate";
            console.writeline("a类装饰");
        }
    }

    class concretedecoratorb : decorator
    {
        private string addstate;
        public override void operation()
        {
            base.operation();
            addstate = "b addstate";
            console.writeline("b类装饰");
        }
    }
}
using system;

namespace consoleapp4
{
    class program
    {
        public static void main(string[] args)
        {            
            person person = new person("白石");
            tshirts shirts = new tshirts();
            overalls overalls = new overalls();

            shirts.decorate(person);
            overalls.decorate(shirts);
        }
    }
    
   class person
    {
        public person()
        {

        }
        private string name;
        public person (string name)
        {
            this.name = name;
        }
        public virtual void show()
        {
            // 什么装饰都没有的基本展示。
            console.writeline($"{name} 一个人的基本show");
        }
    }

    abstract class finery : person
    {
        protected person component;
        public void decorate(person person)
        {
            this.component = person;
        }
        public override void show()
        {
            if (component != null)
            {
                component.show();
            }
        }
    }

    class tshirts : finery
    {
        public override void show()
        {
            base.show();
            console.writeline("穿t恤");
        }
    }

    class overalls : finery
    {
        public override void show()
        {
            base.show();
            console.writeline("工装裤");
        }
    }
}