设计模式之装饰模式

简介:

   学习设计模式已经有段时间了,初接触设计模式。虽然样例简单、生动。但还是感觉非常是抽象。今天又学习了设计模式中的装饰模式,也就是装饰模式让自己对模式略有所懂,装饰模式最大的特点就是把全部须要的功能都按正确的顺序串联起来进行控制。

这里须要强调的是“顺序”,也就是说这样的装饰是建立在一定的顺序之上的,并且这样的顺序是由人为控制的。不同于建造者模式,它的顺序是固定不变的。


**概念

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


**结构图

    Component是定义了一个对象接口。能够给这些对象动态地加入职责。

ConcreteComponent是定义了一个详细的对象,也能够给这个对象加入一些职责。Decorator,抽象装饰类,继承了Component,从外类来扩展Component类的功能。但对于Component来说。是无需知道Decorator的存在的。

至于ConcretrDecorator就是详细的装饰对象,起到给Component加入职责的功能。

**实例解析

     以下以小明(代码中的详细对象)的着装为例,进行详细解析:

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

namespace 装饰模式_终极版_
{
    class Program
    {
        //Person类
        class Person
        {
            public Person()
            { }

            private string name;
            public Person(string name)
            {
                this.name = name;
            }

            public virtual void show()
            {
                Console.WriteLine("装扮的{0}", name);
            }
        }

        //服饰类
        class Finery : Person
        {
            protected Person component;
            
            public void Decorate(Person component)
            {
                this.component = component;
            }

            public override void show()
            {
                if (component != null)
                {
                    component.show();
                }
            }
        }

        //详细服饰类
        class Tshirts : Finery
        {
            public override void show()
            {
                Console.Write("大体恤");
                base.show();
            }
        }

        class BigTrous : Finery
        {
            public override void show()
            {
                Console.Write("垮裤");
                base.show();
            }
        }

        class Sneakers : Finery
        {
            public override void show()
            {
                Console.Write("破球鞋");
                base.show();
            }
        }

        class Suit :Finery
        {
            public override void show()
               {
 	            Console.Write("西装");
                base.show();
                }
        } 
        class Tie : Finery
        { 
            public override  void show()
            {
            Console.Write("领带");
            base.show();
            }
            
        }

        class LeatherShoes:Finery 
        {
            public override void show()
            {
                Console.Write("皮鞋");
                base.show();
            }
        
        }
        static void Main(string[] args)
        {
            Person xc = new Person("小明");

            Console.WriteLine("\n第一种装扮:");

            Sneakers pqx = new Sneakers();
            BigTrous kk = new BigTrous();
            Tshirts dtx = new Tshirts();

            pqx.Decorate(xc);
            kk.Decorate(pqx);                                //装饰过程
            dtx.Decorate(kk);
            dtx.show();

            Console.WriteLine("\n另外一种装扮:");

            LeatherShoes px = new LeatherShoes();
            Tie ld = new Tie();
            Suit xz = new Suit();

            px.Decorate(xc);
            ld.Decorate(px);                                //装饰过程
            xz.Decorate(ld);
            xz.show();

            Console.Read();
        }
    }
}

    

在这个样例中并没有抽象的Component类,将其和ConcreteComponent类合并为“人”的类,详细结构图例如以下:



执行的详细结果为:

              

    在这里为什么会出现这种结果,为什么会是这种“打扮”,又是为什么出现了这种顺序。接下来将为您做具体的介绍(以第一种装扮为例):

    Sneakers实例化了pqx ,BigTrouser 实例化了kk,TShirts实例化了dtx。然后pqx包装xc(也就是这个样例中的小明)。kk包装pqx,dtx包装kk,最后运行dtx.Show(),依照实例化的相反顺序依次实现了对xc的包装,也就是以上截图中显示的结果。


**长处

    ①把类中的装饰功能从类中搬移去除。这样能够简化原有的类。

    ②有效地把类的核心职责和装饰功能区分开来。并且能够去除相关类中反复飞装饰逻辑;


**遗憾

    虽然对装饰模式有了些许了解,可是对于装饰过程部分还有诸多的不理解。希望大家多多不吝赐教。鄙人在此谢过了。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5120823.html,如需转载请自行联系原作者

相关文章
|
1月前
|
设计模式 PHP
php设计模式--装饰模式(七)装饰模式完成文章编辑
php设计模式--装饰模式(七)装饰模式完成文章编辑
12 0
|
4月前
|
设计模式 中间件 PHP
设计模式 | 装饰模式
设计模式 | 装饰模式
17 0
|
6月前
|
设计模式
设计模式系列教程(12) - 装饰模式
设计模式系列教程(12) - 装饰模式
16 0
|
7月前
|
设计模式 算法 uml
结构型设计模式01-装饰模式
结构型设计模式01-装饰模式
16 0
|
7月前
|
设计模式
设计模式13 - 装饰模式【Decorator Pattern】
设计模式13 - 装饰模式【Decorator Pattern】
15 0
|
18天前
|
设计模式 Go
[设计模式 Go实现] 结构型~装饰模式
[设计模式 Go实现] 结构型~装饰模式
|
10月前
|
设计模式
设计模式——装饰模式
设计模式——装饰模式
43 0
|
5月前
|
设计模式 容器
设计模式之装饰模式(2)--有意思的想法
设计模式之装饰模式(2)--有意思的想法
|
5月前
|
设计模式
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
设计模式之装饰模式--优雅的增强
|
7月前
|
设计模式 Java 数据库连接
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能
JAVA设计模式8:装饰模式,动态地将责任附加到对象上,扩展对象的功能