设计模式-装饰者模式

装饰者模式用于串起来各个部分

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

namespace DecorateSample
{
    //虚拟构件
    abstract class VisualCompont
    {
        public abstract void DisPlay();
    }
    //窗体具体构件类
    class Window:VisualCompont
    {
        public override void DisPlay()
        {
            Console.WriteLine("窗体显示");
        }
    }
    //文本框具体构件类
    class TexBox : VisualCompont
    {
        public override void DisPlay()
        {
            Console.WriteLine("文本框显示");
        }
    }
//抽象装饰类
    class CompontDecorator:VisualCompont
    {
        private VisualCompont compont;
        public CompontDecorator (VisualCompont compont)
        {
            this.compont = compont;
        }
        public override void DisPlay()
        {
           
            compont.DisPlay();
        }
    }
    class BarDecorator : CompontDecorator
    {
        public BarDecorator(VisualCompont compont):base(compont)
        {
           
        }
        public override void DisPlay()
        {
            Console.WriteLine("增加条装饰");
            base.DisPlay();
        }
        
    }
    class BlackDecorator : CompontDecorator
    {
        public BlackDecorator(VisualCompont compont) : base(compont)
        {

        }
        public override void DisPlay()
        {
            Console.WriteLine("增加黑色装饰");
            base.DisPlay();
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            VisualCompont window, windowPro;
            window = new Window();         
            windowPro = new BarDecorator(window);
            windowPro = new BlackDecorator(windowPro);
            windowPro.DisPlay();
            Console.Read();

        }
    }
}

你可能感兴趣的:(设计模式)