装饰模式个人的一些理解

以下代码是个人对装饰模式的理解,备忘

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestApp.装饰
{
    public partial class 装饰2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Water water = new Water();
            JiaGong jiagong = new JiaGong();
            纯净水加工厂 chunjingshui = new 纯净水加工厂();
            糖水加工厂 tangshui = new 糖水加工厂();

            jiagong.SetComponent(water);
            chunjingshui.SetComponent(jiagong);
            tangshui.SetComponent(chunjingshui);

            tangshui.加工();
        }
    }


    public class Water
    {
        public virtual void 加工()
        {
            HttpContext.Current.Response.Write("<hr/>");
            HttpContext.Current.Response.Write("<br/>原始的河水,里面还有些杂质....");
        }
    }

    public class JiaGong : Water
    {
        private Water _waterResouce;

        public void SetComponent(Water water)
        {
            _waterResouce = water;
        }

        public override void 加工()
        {
            if (_waterResouce != null)
            {
                _waterResouce.加工();
            }
        }
    }


    public class 纯净水加工厂 : JiaGong
    {
        public override void 加工()
        {
            HttpContext.Current.Response.Write("<hr/>");
            过滤();
            HttpContext.Current.Response.Write("<br/>现在变成了纯净数....");
            base.加工();
        }

        public void 过滤()
        {
            HttpContext.Current.Response.Write("<br/>河水中的杂质已过滤完毕....");
        }
    }

    public class 糖水加工厂 : JiaGong
    {
        public override void 加工()
        {
            HttpContext.Current.Response.Write("<hr/>");
            加糖();
            HttpContext.Current.Response.Write("<br/>糖水制作完毕....");
            base.加工();
        }

        public void 加糖()
        {
            HttpContext.Current.Response.Write("<br/>往水中加糖....");
        }
    }
}

 最终运行效果图如下

装饰模式个人的一些理解_第1张图片

你可能感兴趣的:(装饰模式)