设计模式(七)之装饰模式

16 篇文章 0 订阅
16 篇文章 0 订阅

装饰器模式,顾名思义,就是对已经存在的某些类进行装饰,以此来扩展一些功能。

下图为其结构图:

12121212.png

四个主要角色分别为:
抽象构件(Component)角色:给出一个抽象接口,以规范准备接受附加责任的对象。
具体构件(ConCreteComponent)角色:定义一个将要接受附加责任的类。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

举个例子:

我现在衣柜中有短裤、T恤、西服、长裤。我现在需要将其搭配两身的装扮。

下边我使用面向对象的虚方法来实现:

基类:Perso.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class Person
    {
        public Person()
        {
            this.name = name;
        }
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
  
        public virtual void Show()
        {
            Console.WriteLine("装扮的"+name);
        }
  
    }
}

T恤类:Txu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class Txu : Person
    {
        public override void Show()
        {
            Console.WriteLine("大T恤");
        }
    }
}

短裤类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class duanku: Person
    {
        public override void Show()
        {
            Console.WriteLine("短裤");
        }
    }
}

西服类:xifu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class xifu: Person
    {
        public override void Show()
        {
            Console.WriteLine("西服");
        }
    }
}

长裤类:changku.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class changku: Person
    {
        public override void Show()
        {
            Console.WriteLine("长裤");
        }
    }
}

高层调用模块:Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    class Program
    {
        static void Main(string[] args)
        {
            // 实例化一个人
            Person camellia = new Person("camellia");
            Txu txu = new Txu();
            duanku duan = new duanku();
            changku kuu = new changku();
            xifu fuu = new xifu();
  
            Console.WriteLine("第一种装扮:");
            txu.Show();
            duan.Show();
            camellia.Show();
            Console.ReadKey();
        }
    }
}

上边的代码实现了我们的要求,但是其有一个问题,在Program.cs中每次调用的过程都展示在外边。让其别人看的清清楚楚,就好像没穿衣服的你站在众人面前一件一件换衣服。总觉得不太好,这个时候,我们就可以使用装饰模式。

1:定义抽象构件(Component)角色

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class Person
    {
        public Person()
        {
            this.name = name;
        }
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
  
        public virtual void Show()
        {
            Console.WriteLine("装扮的"+name);
        }
    }
}

2:装饰(Decorator)角色(这个是精髓)

Decorate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    /// <summary>
    /// 装饰类
    /// </summary>
    public class Decorator : Person
    {
        // 声明一个父类对象
        protected Person component;
        // 打扮
        public void Decora(Person component)
        {
            // 将传进来的对象赋到已声明的父类对象中。这个很重要,一定要明白
            this.component = component;
        }
  
        public override void Show()
        {
            // 如果传进来的对象不是null  ,执行其下的show方法
            if (component != null)
            {
                component.Show();
            }
        }
    }
}

3:具体构件(ConCreteComponent)角色:定义一个将要接受附加责任的类。

Me.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class me : Person
    {
        public override void Show()
        {
            Console.WriteLine("我在换衣服");
        }
  
        public void Song()
        {
            Console.WriteLine("我在唱歌");
        }
    }
}

4:具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

西服:xifu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class xifu:Decorator
    {
        public override void Show()
        {
            Console.WriteLine("西服");
            // 展示父类方法
            base.Show();
        }
    }
}

短裤:duanku.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class duanku:Decorator
    {
        public override void Show()
        {
            Console.WriteLine("短裤");
            // 展示父类方法
            base.Show();
        }
    }
}

T恤:Txu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class Txu : Decorator
    {
        public override void Show()
        {
            Console.WriteLine("大T恤");
            // 展示父类方法
            base.Show();
        }
    }
}
 

长裤:changku.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    public class changku:Decorator
    {
        public override void Show()
        {
            Console.WriteLine("长裤");
            // 展示父类方法
            base.Show();
        }
    }
}

高层模块:调用Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace Decorate
{
    class Program
    {
        static void Main(string[] args)
        {
            // 实例化一个人
            Person camellia = new Person("camellia");
            Txu txu = new Txu();
            duanku duan = new duanku();
            changku kuu = new changku();
            xifu fuu = new xifu();
            me m = new me();
  
            m.Show();
            Console.WriteLine("第一种装扮:");
            //txu.Show();
            //duan.Show();
            //camellia.Show();
            txu.Decora(camellia);
            duan.Decora(txu);
            duan.Show();
            Console.WriteLine("");
            Console.WriteLine("--------------------------------------------------------");
            Console.WriteLine("");
            m.Show();
            Console.WriteLine("第二种装扮:");
  
            // 注意下边注释这两行代码,打开注释,可能会有不一样的效果~
            //kuu.Decora(camellia);
            //kuu.Show();
  
            fuu.Decora(kuu);
            fuu.Show();
  
            Console.ReadKey();
        }
    }
}

以上就是使用装饰模式实现的所有代码:精髓主要在装饰类Decorate.cs及子类中的base(执行父类方法)关键字,这二者体现了设计模式的精髓。

代码再展示一次:

  /// <summary>
    /// 装饰类
    /// </summary>
    public class Decorator : Person
    {
        // 声明一个父类对象
        protected Person component;
        // 打扮
        public void Decora(Person component)
        {
            // 将传进来的对象赋到已声明的父类对象中。这个很重要,一定要明白
            this.component = component;
        }
  
        public override void Show()
        {
            // 如果传进来的对象不是null  ,执行其下的show方法
            if (component != null)
            {
                component.Show();
            }
        }
    }

子类:

 public class changku:Decorator
    {
        public override void Show()
        {
            Console.WriteLine("长裤");
            // 展示父类方法
            base.Show();
        }
    }

输出结果如下:

13131313.png

最后对装饰模式做个简单的总结:

装饰器的价值在于装饰,他并不影响被装饰类本身的核心功能。在一个继承的体系中,子类通常是互斥的。比如一辆车,品牌只能要么是奥迪、要么是宝马,不可能同时属于奥迪和宝马,而品牌也是一辆车本身的重要属性特征。但当你想要给汽车喷漆,换坐垫,或者更换音响时,这些功能是互相可能兼容的,并且他们的存在不会影响车的核心属性:那就是他是一辆什么车。这时你就可以定义一个装饰器:喷了漆的车。不管他装饰的车是宝马还是奥迪,他的喷漆效果都可以实现。

装饰模式更多的是用来为已有功能动态的添加更多功能的一种方式。

去掉我们添加的这些装饰,并不影响主体功能。
欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值