zoukankan      html  css  js  c++  java
  • C#代理设计模式

    用简单的例子,说明代理(Proxy)设计模式。

    Insus.NET在家忙写程序没有时间,手机没钱了,叫儿子给一百元去超市(此超市有手机充值的服务)帮Insus.NET手机充值(话费)。
    手机充值的事,Insus.NET会做,儿子也会做,但是Insus.NET因某些情况,不能亲自办,叫儿子去代理(Proxy)。

    定义一个抽象类[Work],让自己或是儿子类别都能实现的充值方法:

    Work
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for Work
    /// </summary>
    namespace Insus.NET
    {
        public abstract class Work
        {
            public abstract void Suppliement();
        }
    }
    复制代码


    当自己没有特殊情况,或是有时间的情况之,自己去超市对手机充值:

    Self
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for Self
    /// </summary>
    namespace Insus.NET
    {
        public class Self : Work 
        {
            public Self()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public override void Suppliement()
            {
               HttpContext.Current.Response.Write ("手机充值人民币100元");
            }
        }
    }
    复制代码


    事情代办,叫儿子去帮Insus.NET的手机充值:

    Son
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for Son
    /// </summary>
    namespace Insus.NET
    {
        public class Son: Work 
        {
            Self objSelf;
            
            public Son()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public override void Suppliement()
            {
                if (objSelf == null)
                {
                    objSelf = new Self();
                }
    
                objSelf.Suppliement();
            }
        }
    }
    复制代码


    asp.net运行程序,在站点建立一个aspx网页:

    ProxyDemo.aspx.cs

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;
    
    public partial class ProxyDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //自己为Insus.NET手机充值
            Self objSelf = new Self();
            objSelf.Suppliement();
    
    
            //儿子(代理)为Insus.NET手机充值。
            Son objSon = new Son();
            objSon.Suppliement();
        }
    }
  • 相关阅读:
    [error]The command could not be located because '/usr/bin' is not included
    hadoop伪分布式
    ssh免密码登录
    移动端中的陀螺仪,摇一摇
    利用百度地图做的定位,获取位置和经纬度
    租房短租发布场地,工作中遇到的复杂日期插件功能
    深入理解定时器系列第三篇——定时器应用(时钟、倒计时、秒表和闹钟)
    BOM之navigator对象和用户代理检测
    jq css3实现跑马灯+大转盘
    Vue小事例
  • 原文地址:https://www.cnblogs.com/zpc870921/p/2935678.html
Copyright © 2011-2022 走看看