9.装饰模式

原理:

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式, 它是作为现有的类的一个包装。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

意图:

动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。

主要解决:

一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。

注意:

如果只有一个ConcreteComponent类,而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类 同理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类

应用实例:

1、孙悟空有 72 变,当他变成"庙宇"后,他的根本还是一只猴子,但是他又有了庙宇的功能。 2、不论一幅画有没有画框都可以挂在墙上,但是通常都是有画框的,并且实际上是画框被挂在墙上。在挂在墙上之前,画可以被蒙上玻璃,装到框子里;这时画、玻璃和画框形成了一个物体。

代码

Prpgram.cs

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

namespace 装饰模式
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteComponent c = new ConcreteComponent();
            //装饰过程
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            d1.SetComponent(c);
            d2.SetComponent(d1);
            d2.Operation();

            Console.ReadKey();
        }
    }
}

Decorator.cs

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

namespace 装饰模式
{
    //Component类
    abstract class Component
    {
        public abstract void Operation();
    }

    //ConcreteComponent
    class ConcreteComponent : Component
    {
        public override void Operation()
        {
            Console.Write("具体对象的操作 ");
        }
    }
    //Decorator 类
    abstract class Decorator : Component
    {
        protected Component component;
        public void SetComponent(Component component)
        {
            this.component = component;
        }
        public override void Operation()
        {
            if (component != null) {
                component.Operation();
            }
        }
    }
    //ConcreteDecoratorA类
    class ConcreteDecoratorA : Decorator
    {
        private string addedState;//本类的独有功能,以区别于ConcreteDecoratorB类
        public override void Operation()
        {
            //首先执行本类的功能,如addedState,再运行原Component的Operation(),相当于对原Component进行了装饰
            addedState = "New State";
            Console.Write("具体装饰对象A的操作 ");
            base.Operation();
            
        }
    }
    //ConcreteDecoratorB类
    class ConcreteDecoratorB : Decorator
    {
        private void AddedBehavior()//本类的独有功能,以区别于ConcreteDecoratorA类
        {

        }
        public override void Operation()
        {
            //首先执行本类的功能,AddedBehavior(),再运行原Component的Operation(),相当于对原Component进行了装饰
            AddedBehavior();
            Console.Write("具体装饰对象B的操作 ");
            base.Operation();    
        }
    }
}

参考

参考书籍:《大话设计模式》
参考链接: 菜鸟教程.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值