美文网首页
9.装饰模式

9.装饰模式

作者: 卡布萨岛 | 来源:发表于2018-10-23 22:39 被阅读0次

原理:

装饰器模式(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();    
        }
    }
}

相关文章

  • 9.装饰模式

    原理: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种...

  • C++ 设计模式 —— 9.装饰模式

    装饰模式:一种结构型设计模式 应用场景:想要动态的给一个对象添加功能,即希望可以根据需求对现有对象添加不同的功能,...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

  • 11.4设计模式-装饰模式-讲解

    设计模式-装饰模式 装饰模式详解 装饰模式在android中的实际运用,避免了耦合 1. 装饰模式详解 2.装饰模...

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • 第4章 结构型模式-装饰模式

    一、装饰模式简介 二、装饰模式的优缺点 三、装饰模式的使用场景 四、装饰模式的实例

  • 让你再也忘不了IO相关知识-Java IO图文详解

    1 装饰模式 Java中IO使用的是装饰模式,装饰模式在Android中很常见,比如系统的Context。 装饰模...

  • 装饰者模式

    在《JAVA与模式》一书开头是这样描述装饰(Decorator)模式的: 装饰模式又名包装模式。装饰模式以对客户端...

  • 设计模式之装饰器模式

    在阎宏博士的《JAVA与模式》的书中,对装饰器模式的描述如下:装饰模式又名包装(Wrapper)模式。装饰模式以对...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

网友评论

      本文标题:9.装饰模式

      本文链接:https://www.haomeiwen.com/subject/iyyzzftx.html