装饰模式学习

来源:互联网 发布:上海海事大学网络教育 编辑:程序博客网 时间:2024/05/20 00:15
今天看大话设计模式一书,发现真的写的不错,之前一直有点疑惑的装饰模式,今晚看懂了,现笔记之。具体见该书相关部分,
主要是解析下。
 

using System;
using System.Collections.Generic;
using System.Text;

namespace 装饰模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Person xc = new Person("小菜");

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

            Sneakers pqx = new Sneakers();
            BigTrouser kk = new BigTrouser();
            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.WriteLine("\n第三种装扮:");
            Sneakers pqx2 = new Sneakers();
            LeatherShoes px2 = new LeatherShoes();
            BigTrouser kk2 = new BigTrouser();
            Tie ld2 = new Tie();

            pqx2.Decorate(xc);
            px2.Decorate(pqx);
            kk2.Decorate(px2);
            ld2.Decorate(kk2);

            ld2.Show();

            Console.Read();
        }
    }

    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("大T恤 ");
            base.Show();
        }
    }

    class BigTrouser : 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();
        }
    }
}

   其实,在这个穿衣服的过程中,要注意的是顺序可以随意搭配的。所以我们先看看首先用Person xc = new Person("小菜");
创建了人的对象实例;之后分别初始化了破球鞋,垮裤,Tshirt的对象。
   再看其执行流程。
   先看dtx.show(),首先输出“大T shirt"了,之后要调用base.show(),这时会跑去Finery类中去调用show()方法,
但这个是componet不为NULL,原因是dtx.decorate(kk)起作用了,用KK去装饰dtx了。这时实际上要接着运行kk.show(),
之后输出"垮裤 ",然后又要调用base.show()了,同样道理,KK被pqx装饰了,---》运行pqx.show(),
输出破球鞋,同样道理,最后输出“装扮的小菜”

原创粉丝点击