zoukankan      html  css  js  c++  java
  • DOTA版设计模式——代理

    代理模式分为好多中,什么透明代理啦,远程代理啦,安全代理啦,本文介绍的是最基本的代理。
    UML图:

    真正的对象和代理对象均继承相同的接口,实例化代理对象时将真实对象传入,当操作代理对象时感觉像是在操作真正对象一样。像是代理对象把真正的对象包装后再在客户端处理。
       internal interface ISubject
        {
            
    void Deal();
        }

    关键在代理类的构造函数中实现对真正对象的实例化,在类的成员方法中操作该真正对象的实例,具体见完整代码。注意真正的类是采用单件模式哦~~~~
    完整代码:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using DotaCommon;

    namespace DotaPatternLibrary.Proxy
    {
        
    internal interface ISubject
        {
            
    void Deal();
        }

        
    public class Proxy : ISubject
        {
            
    #region ISubject 成员

            
    public void Deal()
            {
                PrepareDeal();
                subject.Deal();
                CompleteDeal();
            }

            
    #endregion

            
    private void PrepareDeal()
            {
                LandpyForm.Form.OutputResult(
    "Proxy PrepareDeal");
            }

            
    private void CompleteDeal()
            {
                LandpyForm.Form.OutputResult(
    "Proxy CompleteDeal");
            }

            
    private ISubject subject;
            
    public Proxy(string input)
            {
                subject 
    = RealSubject.Instance(input);
            }
        }

        
    internal class RealSubject : ISubject
        {
            
    #region ISubject 成员

            
    public void Deal()
            {
                LandpyForm.Form.OutputResult(
    "RealSubject Deal" + "[" + input + "]");
            }

            
    #endregion

            
    private static string input;
            
    private static RealSubject realSubject;
            
    private static object lockObj = new object();

            
    public static RealSubject Instance(string inputValue)
            {
                input 
    = inputValue;
                
    lock (lockObj)
                {
                    
    if (realSubject == null)
                    {
                        realSubject 
    = new RealSubject();
                    }
                    
    return realSubject;
                }
            }
        }
    }
  • 相关阅读:
    Python 学习 —— 进阶篇(装饰器、类的特殊方法)
    Python 基础学习的几个小例子
    MyBatis——特殊传参问题小结
    为什么要有分布式事务 分布式事务解决的什么问题 一次解答
    2pc事务和3pc事务区别详解
    SPEL语法
    分布式事务框架 TX-LCN 使用
    分布式事务解决方案
    excel 使用总结
    nginx 常用配置
  • 原文地址:https://www.cnblogs.com/pangxiaoliang/p/1531580.html
Copyright © 2011-2022 走看看