当前位置:编程学习 > 网站相关 >>

大话设计模式之装饰模式(小菜扮靓)

需求:
 
         装扮小菜,保证小菜想穿什么就穿什么(需要保证装饰类之间彼此独立)
 
定义:
 
         动态的为对象添加更多的功能
 
什么时候使用装饰模式?
 
       当系统需要添加更多的功能的时候,向旧的类中添加新的代码.这些新加的代码通常装饰了原有 类的核心职责或主
要行为.,就像下面的例子中提到的用西装或大T恤装饰小菜的问题,设计模式采取的办法是把每个装饰的功能放在单独
的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选
择的,按顺序使用装饰功能包装对象
 
 
类图:
       
 
说明:
 
        Component是定义一个对象接口,可以给这些对象动态添加职责。ConcreteComponent是定义了一个具体体的
对象,也乐意给这个对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类
的功能,但对于Component来说,是无需知道Decorator的存在的,至于ConcreteDecorator就是具体的装饰对象,起
到给Component添加职责的功能
 
        如果只有一个ConcreteComponent类而没有抽象的类Component类,那么Decorator类就可以是
ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要 建立一个单独的
Decorator类,而可以吧Decorator和ConcreteDecorator的责任合并成一个类
 
例子:  
 
类图:
         
  
代码:
 
[html] 
<span style="font-size:18px;">using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace 小菜扮靓3  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Person xc = new Person("小菜");  
              
            Console.WriteLine("\n第一种装扮");  
  
            Sneaker pqx = new Sneaker();  
            BigTrouser kk = new BigTrouser();  
            TShirts dtx = new TShirts();  
  
            <span style="color:#ff0000;">//装饰过程</span>  
            pqx.Decorate(xc);<span style="color:#ff0000;">//先给xc这个人穿上pqx</span>  
            kk.Decorate(pqx);<span style="color:#ff0000;">//再给穿着pqx的xc穿上kk</span>  
            dtx.Decorate(kk);<span style="color:#ff0000;">//再给穿着pqx和kk的xc穿上dtx</span>  
            dtx.Show();<span style="color:#ff0000;">//最后将穿着pqx kk和 dtx的xc 显示出来  
</span>  
            Console.Read();  
  
            Console.WriteLine("\n 第二种装扮");  
  
            LeatherShoes px = new LeatherShoes();  
            Tie id = new Tie();  
            Suit xz = new Suit ();  
  
            px.Decorate(xc);  
            id.Decorate(px);  
            xz.Decorate(id);  
            xz.Show();  
  
  
        }  
  
    }  
    <span style="color:#ff0000;">//Person类</span>  
    class Person  
    {  
        public Person()  
        { }  
        private string name;  
        public Person(string name)  
        {  
            this.name = name;  
        }  
  
        public virtual void Show()  
        {  
            Console.WriteLine("装扮的{0}", name);  
        }  
  
    }  
  
   <span style="color:#ff0000;"> //服饰类</span>  
    class Finery : Person  
    {  
        protected Person component;  
        public void Decorate(Person component)  
        {  
            this.component = component;  
        }  
        public override void Show()  
        {  
            if (component != null)  
            {  
                component.Show();  
            }  
  
        }  
    }    
      
    <span style="color:#ff0000;">//具体服饰类</span>  
    class TShirts : Finery  
    {  
        public override void Show()  
        {  
            Console.Write("大T恤");  
            base.Show();  
        }  
    }  
  
    class BigTrouser : Finery  
    {  
        public override void Show()  
        {  
            Console.Write("垮裤");  
            base.Show();  
        }  
    }  
  
    class Sneaker : Finery  
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,