zoukankan      html  css  js  c++  java
  • (六)代理模式-代码实现

    介绍

    概念:为其他对象提供一种代理以控制对这个对象的访问。

    使用场景:增加一个对象操作的访问控制,如根据权限判断用户是否能执行某个方法. 另一个应用是延迟加载,比如图片的对象,直到真正改变图片文件时才真正加载.

     

    一个带访问控制的代理例子:

    代码:

    //真正的执行对象

    package note.com.proxy;
    
    /**
     * 可执行动作
     * @author lxz
     *
     */
    public class Action {
        
        public void doOne(){
            System.out.println("One");
        }
        public void doTwo(){
            System.out.println("Two");
        }
        public void doThree(){
            System.out.println("Three");
        }
    
    }

    //权限控制类

    package note.com.proxy;
    
    /**
     * 权限类
     * 
     * @author lxz
     *
     */
    public class Permission {
    
        private boolean one = false;
        private boolean two = false;
        private boolean three = false;
        
        public Permission(boolean one,boolean two,boolean three){
            this.one = one;
            this.two = two;
            this.three = three;
        }
        
        public boolean isOne() {
            return one;
        }
        public void setOne(boolean one) {
            this.one = one;
        }
        public boolean isTwo() {
            return two;
        }
        public void setTwo(boolean two) {
            this.two = two;
        }
        public boolean isThree() {
            return three;
        }
        public void setThree(boolean three) {
            this.three = three;
        }
        
        
    }

    //对Action的代理类

    package note.com.proxy;
    
    /**
     * 代理Action类
     * @author lxz
     *
     */
    public class Proxy {
    
        private Action action = null;
        private Permission permission = null;
        
        public Proxy(Permission permission){
            this.action = new Action();
            this.permission = permission;
        }
        
        public void doOne(){
            if(permission.isOne()){
                action.doOne();
            }
        }
        public void doTwo(){
            if(permission.isTwo()){
                action.doTwo();
            }
        }
        public void doThree(){
            if(permission.isThree()){
                action.doThree();
            }
        }
        public Action getAction() {
            return action;
        }
        public void setAction(Action action) {
            this.action = action;
        }
        public Permission getPermission() {
            return permission;
        }
        public void setPermission(Permission permission) {
            this.permission = permission;
        }
        
        
        
    }

    //测试类

    package note.com.proxy;
    
    public class ProxyTest {
        public static void main(String[] args) {
            Permission permission = new Permission(true,false,true);
            Proxy proxy = new Proxy(permission);
            proxy.doOne();
            proxy.doTwo();
            proxy.doThree();
        }
    }

    结果:

    One
    Three

    真正的对象由代理对象去掉用,并且在代理对象中增加的访问控制

  • 相关阅读:
    进行C# 编写发送邮箱,报错Error: need EHLO and AUTH first !
    vue使jsZip和FileSaver.js打包下载
    基于js或vue项目实现一次批量文件下载功能
    模块
    now 与 down 中的 ow 发音是否一样?
    __time64_t 解决了 2038 年问题,可是没解决 1969年问题
    MagickSetOption(mw, "jpeg:extent", "...kb"); 这个函数有时结果出乎意料
    解决Idea启动Spring Boot很慢的问题
    CAP原理和BASE思想和ACID模型
    java并发编程之Condition
  • 原文地址:https://www.cnblogs.com/qinggege/p/5231768.html
Copyright © 2011-2022 走看看