首页 > 其他分享 >13) Proxy Pattern

13) Proxy Pattern

时间:2023-06-09 13:44:35浏览次数:31  
标签:13 loading String Pattern image filename public Proxy displayImage

类别:

 Structural Pattern

问题:

操纵一个对象时碍手碍脚,与装饰者模式不同之处:

装饰者是接口方法,授权小

代理则是整个类,授权大

方案:

 

 

 

示例:

 

public class ProxyPatternDemo {
    
    public static void main(final String[] arguments) {
        final Image image1 = new ProxyImage("HiRes_10MB_Photo1");
        final Image image2 = new ProxyImage("HiRes_10MB_Photo2");

        image1.displayImage(); // loading necessary
        image1.displayImage(); // loading unnecessary
        image2.displayImage(); // loading necessary
        image2.displayImage(); // loading unnecessary
        image1.displayImage(); // loading unnecessary
    }
}

interface Image {
    public void displayImage();
}

// On System A
class RealImage implements Image {

    private String filename = null;

    /**
     * Constructor
     * 
     * @param filename
     */
    public RealImage(final String filename) {
        this.filename = filename;
        loadImageFromDisk();
    }

    /**
     * Loads the image from the disk
     */
    private void loadImageFromDisk() {
        System.out.println("Loading   " + filename);
    }

    /**
     * Displays the image
     */
    public void displayImage() {
        System.out.println("Displaying " + filename);
    }

}

// On System B
class ProxyImage implements Image {

    private RealImage image = null;
    private String filename = null;

    /**
     * Constructor
     * 
     * @param filename
     */
    public ProxyImage(final String filename) {
        this.filename = filename;
    }

    /**
     * Displays the image
     */
    public void displayImage() {
        if (image == null) {
            image = new RealImage(filename);
        }
        image.displayImage();
    }

}

 

应用:

 

不足:(

 

优化:)

 

标签:13,loading,String,Pattern,image,filename,public,Proxy,displayImage
From: https://www.cnblogs.com/zno2/p/6694843.html

相关文章

  • 12) Flyweight Pattern
    类别: StructuralPattern问题/动机: 假若绿色是相同部分,占用1M内存,如果提取出来,众对象共享其内容,只占1M内存,否则占10M,且随着对象增多,占用越来越多内存,无疑是浪费资源Aflyweightisanobjectthatminimizesmemoryusagebysharingasmuchdataaspossiblewithot......
  • 11) Facade Pattern
    类别: StructuralPattern问题/动机:系统非常复杂隐藏复杂细节,提供简单界面方案:  示例: /*Complexparts*/publicclassFacadePatternDemo{publicstaticvoidmain(String[]args){CarFacadefacade=newCarFacade();facade.CreateC......
  • haproxy 2.8 发布
    最近haproxy2.8发布了,提供了不少新特性,以及新能上的提升新特性lua的邮件通知新的lua事件框架lua队列支持更多新的可以lua访问的server函数lua执行超时指令http客户端可以禁用默认dns直接request以及response的http压缩tls签名算法支持配置alpn默认值支持......
  • 【每日一题】Problem 313B - Ilya and Queries
    原题解决思路使用后缀和计算到i处共有多少对\(s_i=s_{i+1}\),计算时相减以下就可以#include<bits/stdc++.h>intmain(){std::strings;intm;std::cin>>s>>m;std::vector<std::vector<int>>vec(m,std::vector<int>(2,0));......
  • ARC132E
    https://www.luogu.com.cn/problem/AT_arc132_e由于一旦走到头那么这一个后缀/前缀就一定是对应的颜色,所以最终答案形如一段左脚印,一段保留原来的,一段右脚印。保留原来的段一定是在两个洞之间的一段完整段,考虑枚举这个段,左脚印的数量是确定的,转化成算概率的问题。这实际上等价......
  • CF1338 Div.1 做题记录
    ACF题面假定用到的最大的数是\(x\),那么一个数最大可以增大\(2^x-1\)。题目只要求不降,所以求出将\(a_i<a_{i-1}\)变成\(a_i=a_{i-1}\)时需要增大的最大值。求出这个数的二进制位数即可。点击查看代码#include<bits/stdc++.h>#defineullunsignedlonglong#definell......
  • LightOJ - 1374 Confusion in the Problemset (模拟)
    TimeLimit: 2000MSMemoryLimit: 32768KB64bitIOFormat: %lld&%lluLightOJ-1374ConfusionintheProblemsetSubmit StatusDescriptionAsmallconfusioninaproblemsetmayruinthewholecontest.So,mostoftheproblemsetterstrytheirbesttorem......
  • 1113.DRF
    一、路由器1.SimpleRouter该路由器包括标准集合:list,create,retrieve,update,partial_update和destroy动作的路由。视图集中还可以使用@detail_route或@list_route装饰器标记要被路由的其他方法。2.DefaultRouter这个路由器类似于上面的SimpleRouter,但是还包括一个默认返回所有......
  • [LeetCode] 1351. Count Negative Numbers in a Sorted Matrix
    Givena mxn matrix grid whichissortedinnon-increasingorderbothrow-wiseandcolumn-wise,return thenumberof negative numbersin grid.Example1:Input:grid=[[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]Output:8Explanation:Thereare......
  • 2013年工作中遇到的20个问题:121-140
     121.Springz中,根据实现类找不到bean。UserImplimplementsUser{}XmlWebApplicationContextcontext;context.getBean(User.class);√javcontext.getBean(UserImpl.class);获取不到  没有使用Cgilib库!  --------貌似也不行------------ 因为spring的......