代理模式,proxy

以下用代理买火车票作为例子

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

namespace TestApp.ProxyDemo
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Proxy proxy = new Proxy();
            proxy.buy("lisi"); //给lisi代理买票
        }
    }


    public interface BuyTickt
    {
        void buy(string Name);
    }


    public class Proxy : BuyTickt
    {
        private TicktSaler ticktSaler;

        public Proxy()
        {
            ticktSaler = new TicktSaler("zhangsan");
        }

        public void buy(string Name)
        {
            ticktSaler.buy(Name);
        }
    }

    public class TicktSaler : BuyTickt
    {
        private string _name;
        public TicktSaler(string Name)
        {
            _name = Name;
        }

        public void buy(string Name)
        {
            HttpContext.Current.Response.Write(string.Format("<br/>{0}给{1}买了一张火车票...",_name,Name));
        }
    }
}

posted @ 2012-12-23 23:03  玻璃鱼儿  阅读(182)  评论(0编辑  收藏  举报