zoukankan      html  css  js  c++  java
  • 二十四种设计模式:代理模式(Proxy Pattern)

    代理模式(Proxy Pattern)

    介绍
    为其他对象提供一个代理以控制对这个对象的访问。


    示例
    有一个Message实体类,某对象对它的操作有Insert()和Get()方法,用一个代理来控制对这个对象的访问。

      MessageModel

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Pattern.Proxy
    {
        /// <summary>
        /// Message实体类
        /// </summary>
        public class MessageModel
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="msg">Message内容</param>
            /// <param name="pt">Message发布时间</param>
            public MessageModel(string msg, DateTime pt)
            {
                this._message = msg;
                this._publishTime = pt;
            }
    
            private string _message;
            /// <summary>
            /// Message内容
            /// </summary>
            public string Message
            {
                get { return _message; }
                set { _message = value; }
            }
    
            private DateTime _publishTime;
            /// <summary>
            /// Message发布时间
            /// </summary>
            public DateTime PublishTime
            {
                get { return _publishTime; }
                set { _publishTime = value; }
            }
        }
    }
    

      IMessage

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Pattern.Proxy
    {
        /// <summary>
        /// 对Message操作的接口
        /// </summary>
        public interface IMessage
        {
            /// <summary>
            /// 获取Message
            /// </summary>
            /// <returns></returns>
            List<MessageModel> Get();
    
            /// <summary>
            /// 插入Message
            /// </summary>
            /// <param name="mm">Message实体对象</param>
            /// <returns></returns>
            bool Insert(MessageModel mm);
        }
    }
    

      SqlMessage

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Pattern.Proxy
    {
        /// <summary>
        /// Sql方式操作Message
        /// </summary>
        public class SqlMessage : IMessage
        {
            /// <summary>
            /// 获取Message
            /// </summary>
            /// <returns></returns>
            public List<MessageModel> Get()
            {
                List<MessageModel> l = new List<MessageModel>();
                l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
    
                return l;
            }
    
            /// <summary>
            /// 插入Message
            /// </summary>
            /// <param name="mm">Message实体对象</param>
            /// <returns></returns>
            public bool Insert(MessageModel mm)
            {
                // 代码略
                return true;
            }
        }
    }
    

      MessageProxy

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Pattern.Proxy
    {
        /// <summary>
        /// 代理类
        /// </summary>
        public class MessageProxy : IMessage
        {
            SqlMessage _sqlMessage;
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public MessageProxy()
            {
                _sqlMessage = new SqlMessage();
            }
    
            /// <summary>
            /// 获取Message
            /// </summary>
            /// <returns></returns>
            public List<MessageModel> Get()
            {
                return _sqlMessage.Get();
            }
    
            /// <summary>
            /// 插入Message
            /// </summary>
            /// <param name="mm">Message实体对象</param>
            /// <returns></returns>
            public bool Insert(MessageModel mm)
            {
                return _sqlMessage.Insert(mm);
            }
        }
    }
    

      Client

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    using Pattern.Proxy;
    
    public partial class Proxy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MessageProxy m = new MessageProxy();
            Response.Write(m.Insert(new MessageModel("插入", DateTime.Now)));
            Response.Write("<br />");
            Response.Write(m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString());
        }
    }
    

      运行结果
      True
      SQL方式获取Message 2007-4-1 16:31:24

  • 相关阅读:
    Permession denied error when use supervisorctl
    alembic 迁移数据库
    Linux VIM常用命令
    AWS EC2 install supervisor
    第一次编程作业
    (转)ubuntu 12 04下安装JDK7
    (转)Java程序利用main函数中args参数实现参数的传递
    Nvidia CUDA 6 Installed In Ubuntu 12.04
    (转)Java程序利用main函数中args参数实现参数的传递
    (转)JAVA路径问题及命令行编译运行基础(linux下)
  • 原文地址:https://www.cnblogs.com/taotaodetuer/p/6182367.html
Copyright © 2011-2022 走看看