zoukankan      html  css  js  c++  java
  • C#装饰模式

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

    namespace 装饰模式
    {
        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-Shirt ");
                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 Program
        {
            static void Main(string[] args)
            {
                Person ps = new Person("小江");

                Sneakers sn = new Sneakers();
                BigTrouser bt = new BigTrouser();
                TShirts ts = new TShirts();

                sn.Decorate(ps);
                bt.Decorate(sn);
                ts.Decorate(bt);

                ts.Show();
            }
        }
    }

    明确个目标,一直走下去
  • 相关阅读:
    第3章 Python的数据结构、函数和文件
    字符与编码
    第2章 IPython和Jupyter
    第1章 准备工作
    (转)详解Python的装饰器
    (转)Python中的split()函数
    5.5 用户定义的可调用类型
    2.6 序列的增量赋值
    Zookeeper简析
    Dubbo-服务引入源码分析
  • 原文地址:https://www.cnblogs.com/fhlj/p/3613684.html
Copyright © 2011-2022 走看看