zoukankan      html  css  js  c++  java
  • 装饰模式个人的一些理解

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

    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/>往水中加糖....");
            }
        }
    }

     最终运行效果图如下

  • 相关阅读:
    [转]addEventListener() 方法,事件监听
    JavaScrpit判断横竖屏
    无法获得锁 /var/lib/dpkg/lock
    配置Meld为git的默认比较工具
    C#多线程之旅(7)——终止线程
    【SQL进阶】03.执行计划之旅1
    单问号和双问号
    聚集索引VS非聚集索引
    【T-SQL进阶】02.理解SQL查询的底层原理
    【T-SQL】系列文章全文目录(2017-06-26更新)
  • 原文地址:https://www.cnblogs.com/huangzelin/p/2830369.html
Copyright © 2011-2022 走看看