设计模式之装饰模式

前言

装饰模式

之前我们学完了简单工厂模式,又学完了策略模式,今天来和大家一起来看看装饰模式是怎样的?

what

Decorator)–动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
作用:为已有功能动态地添加更多功能的一种方式
优点:把类中的装饰功能从类中搬移去除,这样可以简化原有的类。有效的把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

结构图

component是定义一个对象接口,可以给这些对象动态地添加职责。concretecomponent是定义了一个具体的对象,也可以给这个对象添加一些职责。decorator,装饰抽象类,继承了component,从外类来扩展component类的功能,但对于component来说,是无需知道decorator的存在的。
下面是装饰模式的结构图:

结构图
这里是有component类。日过只有一个concretecomponent类而没有抽象的component类,那么decorator类可以是concretecomponent的一个子类。同样道理,如果只有一个concretedecorator类,那么就没有必要建立一个单独的decorator类。而可以把decorator和concretedecorator的责任合并成一个类。
如图:
类

情景

大家都玩过网游,里面很多游戏开始之前都会先让用户挑一身行头,或自己搭配一身。那么这时候用装饰模式就再好不过了。装饰模式利用setcomponent来对对象进行包装,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

应用

  1. 需要扩展一个类的功能,或给一个类添加附加职责

  2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。(可添加,可撤销

  3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。

  4. 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

代码展示

concretecomponent类:

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

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

decorator类:

 class Decorator:Person   //装饰继承了人类
    {
        protected Person component;
        //打扮
        public void Decorate(Person component)
        {
            this.component = component;
        }
        //重写
        public override void Show()
        {
            if (component != null)
            {
                component.Show ();
            }
        }
    }

concretedecorator类:

//具体装饰类
//上衣
    class Tshirts:Decorator  
    {
        public override void Show()
        {
            Console.WriteLine("大T恤");
            base.Show();
        }
    }
//下衣
    class BigTrouser : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("垮裤");
            base.Show();
        }
    }
//帽子类
    class Cap : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("帽子");
            base.Show();
        }
    }
//鞋子
    class Shoes : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("皮鞋");
            base.Show();
        }
    }

    class Dress : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("连衣裙");
            base.Show();
        }
    }

    class HighheeledShoes : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("高跟鞋");
            base.Show();
        }
    }


    //可以加很大类似的类,省略。。。。

客户端代码:

class Program
    {
        static void Main(string[] args)
        {
            Person Monkey = new Person("猴子"); //装饰对象

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

            Tshirts dtx = new Tshirts();
            BigTrouser kk = new BigTrouser();
            Shoes px = new Shoes();

            dtx.Decorate(Monkey);   //装饰过程,每一步
            kk.Decorate(dtx);
            px.Decorate(kk);
            kk.Show();


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

            HighheeledShoes gg = new HighheeledShoes();
            Dress lyq = new Dress();

            lyq.Decorate(Monkey);      //装饰过程
            gg.Decorate(lyq );
            gg.Show();

            Console.Read();


        }
    }

效果展示:

此时一个身穿大T恤+垮裤的猴子就完成了。。。

效果

后记

学完了装饰模式后反而觉得没想象中这么难了,so easy~学习真的简单而快乐。
还可以有更多更好玩的装饰哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值