C#设计模式:装饰者模式(Decorator Pattern)

装饰者模式详解

一,装饰者模式(Decorator Pattern):装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。

二,在以上代码中我们是中国人是根本行为,我们给中国人装饰我会说英语,我会说日语和我会说英语我会说日语这三种行为,那按正常的理解来说,这是多态,那我们对People中的Say我们是不是需要有三种实现方法呢?三种还是比较好解决,但是如果我们有N种呢?难道我们要有N个实现类么?这时我们就可以使用装饰者模式。

1》我们抽象People这个类,还有抽象的Say方法方法,这时People就可以有多国人的实现,我们使用一种中国人,假设,这是我们现有的代码的,但根据需求,我们需要给中国人添加我会说英语,我会说日语和我会说英语我会说日语这三种行为,那该怎么办?根据以往想法,一般如下设计:

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

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            People p = new Englist();
            p.Say();
            People p1 = new Jan();
            p1.Say();
            People p2 = new JE();
            p2.Say();
        }
    }
    public abstract class People 
    {
        public abstract void Say();
    }
    public class Chinese : People
    {
        public override void Say()
        {
            Console.WriteLine("我是中国人");
        }
    }
    public class Englist : Chinese
    {
        public override void Say()
        {
            base.Say();
            Console.WriteLine("我是会说英语");
        }
    }
    public class Jan : Chinese
    {
        public override void Say()
        {
            base.Say();
            Console.WriteLine("我是会说日语");
        }
    }
    public class JE : Chinese
    {
        public override void Say()
        {
            base.Say();
            Console.WriteLine("我是会说英语和日语");
        }
    }
}

2》然而我们如果还有N种装饰中国人,难道要写N个子类?但是明显是不合理的,这时我们该怎么对chinese扩展呢?我们使用装饰者模式看看,如下代码:

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

namespace _8.装饰者模式
{
    //装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。
    class Program
    {
        static void Main(string[] args)
        {
            People p = new Chinese();            //没有扩展的实现

            Decorator d = new Englist(p);        //将会说英语的扩展给中国人
            d.Say();
            Console.WriteLine("----------------------\n");

            Decorator d1 = new Jan(p);            //将会说日语的扩展给中国人
            d1.Say();
            Console.WriteLine("----------------------\n");

            Englist e = new Englist(p);     //将会说英语和日语的扩展给中国人,这里是装饰者模式的核心,当我们存在这里三种可能的组合时,正常逻辑可能是每一种都实现一次,这样就存在三个实现子类,但是如果可能组合有几十上百种怎么办,这时我们可以使用装饰者模式,在代码中,我们只是实现说英语和说日语的两种可能,但我们可以实现组合得出第三种可能,这样代码就可以减少很多。
            Jan j = new Jan(e);
            j.Say();
        }
    }

    public abstract class People
    {
        public abstract void Say();
    }
    public class Chinese : People
    {
        public override void Say()         //根本的行为
        {
            Console.Write("我是中国人");
        }
    }

    public abstract class Decorator : People
    {
        public People people;
        public Decorator(People p)
        {
            this.people = p;
        }

        public override void Say()
        {
            people.Say();
        }
    }
    public class Englist : Decorator
    {
        public Englist(People p)
            : base(p)
        {
        }

        public override void Say()
        {
            base.Say();

            // 添加新的行为,动态地扩展一个对象的功能
            SayEnglish();
        }

        /// <summary>
        /// 新的行为方法
        /// </summary>
        public void SayEnglish()
        {
            Console.WriteLine("我会说英语");
        }
    }

    public class Jan : Decorator
    {
        public Jan(People p)
            : base(p)
        {
        }

        public override void Say()
        {
            base.Say();

            // 添加新的行为,动态地扩展一个对象的功能
            SayJan();
        }

        /// <summary>
        /// 新的行为方法
        /// </summary>
        public void SayJan()
        {
            Console.WriteLine("我会说日语");
        }
    }
}

 两个代码对比,我们是不是发现少了一个子类的实现?,这样也减少了代码量。

3》但为什么下面代码可以实现两种组合?我们来理清点思路

Englist e = new Englist(p);     
Jan j = new Jan(e);
j.Say();

在上文中,我们的Englist 是继承Decorator而构造变量是People类型,但我们实例Englist 时,我们也传入了一个抽象people接收Chinese创建的中国人实例的变量进来,然后传给Englist ,而此时,人的对象已经传给Englist ,而Englist继承decorator,同时decorator继承people,我们重写Say()并调用的中国人的实现的方法Say()和调用SayEnglist(),这样我们就可以给原本的中国人装饰上说英语这一行为,如下代码:

     public override void Say()
        {
            base.Say();

            // 添加新的行为,动态地扩展一个对象的功能
            SayEnglist();
        }

 4》那我们要同时说英语的日语呢?

根据3》中我么可以知道3中的Say方法代码已经综合了说英文,我们的思路是不是可以说,重写英语的Say(),调用英语的Say()和调用说日语的方法SayJan()就可以了?而不是说实现多一个子类。那为什么下面这句代码可以实现呢?综合了两种说话的行为呢?

Jan j = new Jan(e);

我们来理解下,在上文我们实例的英语已经重写了Say的方法,而此时的Say()是已经包含了说英语的功能,同时Englist继承的父类最根本的就是people,根据继承的原则,我们是不是可以将Englist对象传给people,这里我们在看看Jan内重写Say()方法,并调用说日语的方法,这时不就可以同时给中国人添加两种行为了么

转载于:https://www.cnblogs.com/May-day/p/8574842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值