大话设计模式之装饰模式(小菜扮靓)

需求:


装扮小菜,保证小菜想穿什么就穿什么(需要保证装饰类之间彼此独立)


定义:


动态的为对象添加更多的功能


什么时候使用装饰模式?


当系统需要添加更多的功能的时候,向旧的类中添加新的代码.这些新加的代码通常装饰了原有 类的核心职责或主

要行为.,就像下面的例子中提到的用西装或大T恤装饰小菜的问题,设计模式采取的办法是把每个装饰的功能放在单独

的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选

择的,按顺序使用装饰功能包装对象



类图:



说明:


Component是定义一个对象接口,可以给这些对象动态添加职责。ConcreteComponent是定义了一个具体体的

对象,也乐意给这个对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类

功能,但对于Component来说,是无需知道Decorator的存在的,至于ConcreteDecorator就是具体的装饰对象,起

到给Component添加职责的功能


如果只有一个ConcreteComponent类而没有抽象的类Component类,那么Decorator类就可以是

ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要 建立一个单独的

Decorator类,而可以吧Decorator和ConcreteDecorator的责任合并成一个类


例子:


类图:

代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 小菜扮靓3
{
    class Program
    {
        static void Main(string[] args)
        {
            Person xc = new Person("小菜");
            
            Console.WriteLine("\n第一种装扮");

            Sneaker pqx = new Sneaker();
            BigTrouser kk = new BigTrouser();
            TShirts dtx = new TShirts();

            //装饰过程
            pqx.Decorate(xc);//先给xc这个人穿上pqx
            kk.Decorate(pqx);//再给穿着pqx的xc穿上kk
            dtx.Decorate(kk);//再给穿着pqx和kk的xc穿上dtx
            dtx.Show();//最后将穿着pqx kk和 dtx的xc 显示出来

            Console.Read();

            Console.WriteLine("\n 第二种装扮");

            LeatherShoes px = new LeatherShoes();
            Tie id = new Tie();
            Suit xz = new Suit ();

            px.Decorate(xc);
            id.Decorate(px);
            xz.Decorate(id);
            xz.Show();


        }

    }
    //Person类
    class Person
    {
        public Person()
        { }
        private string name;
        public Person(string name)
        {
            this.name = name;
        }

        public virtual void Show()
        {
            Console.WriteLine("装扮的{0}", name);
        }

    }

    //服饰类
    class Finery : Person
    {
        protected Person component;
        public void Decorate(Person component)
        {
            this.component = component;
        }
        public override void Show()
        {
            if (component != null)
            {
                component.Show();
            }

        }
    }  
    
    //具体服饰类
    class TShirts : Finery
    {
        public override void Show()
        {
            Console.Write("大T恤");
            base.Show();
        }
    }

    class BigTrouser : Finery
    {
        public override void Show()
        {
            Console.Write("垮裤");
            base.Show();
        }
    }

    class Sneaker : Finery
    {
        public override void Show()
        {
            Console.Write("破球鞋");
            base.Show();
        }
    }

    class Suit : Finery
    {
        public override void Show()
        {
            Console.Write("西装");
            base.Show();
        }
    }

    class Tie : Finery
    {
        public override void Show()
        {
            Console.Write("领带");
            base.Show();
        }
    }

    class LeatherShoes : Finery
    {
        public override void Show()
        {
            Console.Write("皮鞋");
            base.Show();
        }
    }                                             

}

结果:



装饰模式的优点:


  • 把类中的装饰功能从类中搬移出去,达到简化原有的类的目的。
  • 装饰模式允许系统动态地决定“贴上”一个需要的“装饰”,或者除掉一个不需要的“装饰”。
  • 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值