C#设计模式--装饰器模式

0.C#设计模式--简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

4.C#设计模式--建造者模式

5.C#设计模式--原型模式

6.C#设计模式--适配器模式

设计模式:

装饰器模式(Decorator Pattern)

简单介绍:

装饰器模式(Decorator Pattern):

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等,这个就是装饰者模式的思想

装饰器模式主要组成部分:

Component:定义一个对象接口,可以给这些对象动态地添加职责

ConcreteComponent:定义一个对象,可以给这个对象添加一些职责

Decorator:维持一个指向Component的指针,并定义一个与Component接口一致的接口

ConcreteDecorator:负责向ConcreteComponent添加功能

在装饰模式中,Decorator定义了一个装饰接口类。因为Decorator与ConcreteComponent继承同一个接口,所以继承Decorator的类ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是装饰,就成为了一个包装好的装饰类。

适配器模式类图:

装饰者模式C#代码举例:

Component 对象类

    /// <summary>
    /// 对象类
    /// </summary>
    public abstract class Component
    {
        public abstract void Print();
    }

ConcreteComponenet 类 对象的修饰实现类

    public class ConcreteComponent:Component
    {
        /// <summary>
        /// 对象修饰类
        /// </summary>

        public override void Print()
        {
            Console.WriteLine("我是ConcreteComponent!");
        }
    }

Decorator类 装饰器类

    /// <summary>
    /// 装饰器类
    /// </summary>
    public class Decorator:Component
    {
        protected Component _component;

        public void SetComponent(Component component)
        {
            this._component = component;
        }
        public override void Print()
        {
            if (_component != null)
            {
                _component.Print();
            }
        }
    }

ConcreteDecoratorA类 装饰子类A

    /// <summary>
    /// 装饰子类A
    /// </summary>
    public class ConcreteDecoratorA: Decorator
    {
        public override void Print()
        {
            base.Print();
            Console.WriteLine("我是ConcreteDecoratorA!");
        }
    }

ConcreteDecoratorB 装饰子类B

    /// <summary>
    /// 装饰子类B
    /// </summary>
    public class ConcreteDecoratorB:Decorator
    {
        public override void Print()
        {
            base.Print();
            Console.WriteLine("我是ConcreteDecoratorB!");
        }
    }

用户测试类:

    class Client
    {
        static void Main(string[] args)
        {
            ConcreteComponent concreteComponent = new ConcreteComponent();
            ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
            ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB();

            concreteDecoratorA.SetComponent(concreteComponent);
            concreteDecoratorA.Print();
            Console.WriteLine("--------------------------------");
            concreteDecoratorB.SetComponent(concreteDecoratorA);

            concreteDecoratorB.Print();
            Console.Read();

        }
    }

运行结果:

源代码工程文件下载

装饰者模式实际生活举例

举例说明

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等

mobilephone 原手机接口

    /// <summary>
    /// 手机接口
    /// </summary>
    public abstract class MobilePhone
    {
        public abstract void Print();
    }

AppleMobilePhone类 手机的修饰类

    /// <summary>
    /// 手机的修饰
    /// </summary>
    public class AppleMobilePhone :MobilePhone
    {
        public override void Print()
        {
            Console.WriteLine("我是一部苹果手机!");
        }
    }

Decorator类 装饰器类

    /// <summary>
    /// 装饰器
    /// </summary>
    public class Decorator:MobilePhone
    {
        protected MobilePhone mobliePhone;
        public void SetMobilePhone(MobilePhone phone)
        {
            this.mobliePhone = phone;
        }

        public override void Print()
        {
            if (mobliePhone != null)
            {
                mobliePhone.Print();
            }
        }
    }

MobilePhoneShell类 手机壳修饰类

    /// <summary>
    /// 手机壳类
    /// </summary>
    public class MobilePhoneShell:Decorator
    {
        public override void Print()
        {
            base.Print();
            Console.WriteLine("我是一个手机保护壳!");
        }
    }

MobilePhoneStickers类 手机贴画修饰类

    /// <summary>
    /// 手机贴画修饰类
    /// </summary>
    public class MobilePhoneStickers:Decorator
    {
        public override void Print()
        {
            base.Print();
            Console.WriteLine("我是一个手机贴画!");
        }
    }

用户测试类

    class Client
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------------手机-----------------");
            AppleMobilePhone appleMobilePhone = new AppleMobilePhone();
            appleMobilePhone.Print();

            Console.WriteLine("---------加了手机壳的手机----------------");
            MobilePhoneShell mobilePhoneShell = new MobilePhoneShell();
            mobilePhoneShell.SetMobilePhone(appleMobilePhone);
            mobilePhoneShell.Print();

            Console.WriteLine("----------加了手机贴画的手机---------------");
            MobilePhoneStickers mobilePhoneStickers = new MobilePhoneStickers();
            mobilePhoneStickers.SetMobilePhone(appleMobilePhone);
            mobilePhoneStickers.Print();
            Console.WriteLine("----------既加了手机壳又加了手机贴画的手机--------");

            mobilePhoneStickers.SetMobilePhone(mobilePhoneShell);
            mobilePhoneStickers.Print();

            Console.Read();

        }
    }

运行结果

源代码工程文件下载地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值