Java: Proxy Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Searcher.java
 *
 * */
 
 
package com.javapatterns.proxy;
 
 
/**
 * 抽象主题
 * @author geovindu
 * */
public interface Searcher
{
    /**
     * 声明一个抽象方法
     * */
    String doSearch(String userId, String searchType);
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc RealSearcher.java
 *
 * */
 
 
package com.javapatterns.proxy;
 
/**
 *真实主题
 * @author geovindu
 *
 * */
public class RealSearcher  implements Searcher{
 
 
    /**
     *
     *构造子
     * */
    public RealSearcher()
    {
    }
    /**
     *
     *真实的查询的工作在这里发生
     * @param userId
     * @param keyValue
     * @return
     * */
    public String doSearch(String userId, String keyValue)
    {
        String sql = "SELECT * FROM data_table WHERE key_col = '" + keyValue + "'";
 
        //execute this SQL Statement and concatenate a result string
        return "result set 涂聚文";
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc AccessValidator.java
 *
 * */
 
 
package com.javapatterns.proxy;
/**
 *用户权限检查对象
 * @author geovindu
 * */
public class AccessValidator {
    /**
     *
     *用户权限检查
     * */
    public boolean vaidateUser(String userId)
    {
        if (userId.equals("geovindu"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc UsageLogger.java
 *
 * */
 
 
package com.javapatterns.proxy;
/**
 *
 *
 * */
public class UsageLogger {
 
 
    private String userId;
 
    /**
     *用户Id 赋值方法
     *
     * */
    public void setUserId(String userId)
    {
        this.userId = userId;
    }
    /**
     *保存至日志中
     *
     * */
    public void save()
    {
        String sql = "INSERT INTO USAGE_TABLE (user_id) " +
                " VALUES(" + userId + ")";
         System.out.println("已在保存日志!操作用户 "+userId);
        //execute this SQL statement
    }
    /**
     *
     *保存至日志中
     * */
    public void save(String userId)
    {
        this.userId = userId;
 
        save();
    }
 
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Proxy.java
 *
 * */
 
package com.javapatterns.proxy;
 
/**
 *代理角色
 * @author geovindu
 * */
public class Proxy implements Searcher {
 
 
    /**
     * @link aggregation
     * @directed
     */
    private RealSearcher searcher;
 
    /**
     * @link aggregation
     * @directed
     */
    private UsageLogger usageLogger;
 
    /**
     * @link aggregation
     * @directed
     */
    private AccessValidator accessValidator;
 
    /**
     *构造子
     *
     * */
    public Proxy()
    {
        searcher = new RealSearcher();
    }
 
    /**
     *实现查询操作
     * @param userId  用户ID
     * @param keyValue  搜索的关键字
     * @return  返回字符串
     * */
    public String doSearch(String userId, String keyValue)
    {
        if (checkAccess(userId))
        {
            String result = searcher.doSearch(null, keyValue);
            logUsage(userId);
 
            return result;
        }
        else
        {
            return null;
        }
    }
 
    /**
     *查询前的权限操作
     * @param userId  用户ID
     * @return  逻辑真假
     * */
    private boolean checkAccess(String userId)
    {
        accessValidator = new AccessValidator();
 
        return accessValidator.vaidateUser(userId);
    }
 
    /**
     *查询后的日日操作(登记)
     * @param userId  用户ID
     *
     * */
    private void logUsage(String userId)
    {
        UsageLogger logger = new UsageLogger();
 
        logger.setUserId(userId);
 
        logger.save();
    }
 
}

  

调用测试:、

1
2
3
4
5
6
//代理模式
searcher = new Proxy();
String userId = "geovindu";
String searchType = "SEARCH_BY_ACCOUNT_NUMBER";
String result = searcher.doSearch(userId, searchType);
System.out.println(result);

  

输出:

1
2
已在保存日志!操作用户 geovindu
result set 涂聚文

  

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 代理模式 Proxy Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc ImageIconProxy.java
 *
 * */
 
 
package com.javapatterns.proxy;
 
import java.awt.Graphics;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.SwingUtilities;
 
/**
 *代理角色
 * @author geovindu
 *
 * */
public class ImageIconProxy implements Icon
{
    private ImageIcon realIcon = null;
    private String imageName;
    private int width;
    private int height;
    boolean isIconCreated = false;
    /**
     *构造子
     * @param imageName
     * @param width
     * @param height
     * */
    public ImageIconProxy(String imageName, int width, int height)
    {
        this.imageName = imageName;
        this.width = width;
        this.height = height;
    }
    /**
     *图像高度取值方法
     * @return
     * */
    public int getIconHeight()
    {
        return realIcon.getIconHeight();
    }
    /**
     *图像宽度取值
     * @return
     * */
    public int getIconWidth()
    {
        return realIcon.getIconWidth();
    }
 
    // The proxy's paint() method is overloaded to draw a border
    // and a message "Loading author's photo.." while the image
    // loads. After the image has loaded, it is drawn. Notice
    // that the proxy does not load the image until it is
    // actually needed.
    /**
     *加载图像
     * @param c
     * @param g
     * @param x
     * @param y
     * */
    public void paintIcon(final Component c, Graphics g, int x, int y)
    {
        if(isIconCreated)
        {
            realIcon.paintIcon(c, g, x, y);
            g.drawString("Java Design Patterns by Geovin Du(涂聚文)", x+20, y+370);
        }
        else
        {
            g.drawRect(x, y, width-1, height-1);
            g.drawString("Loading author's photo...", x+20, y+20);
 
            // The image is being loaded on another thread.
            synchronized(this)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            // Slow down the image-loading process.
                            Thread.currentThread().sleep(2000);
 
                            // ImageIcon constructor creates the image.
                            realIcon = new ImageIcon(imageName);
                            isIconCreated = true;
                        }
                        catch(InterruptedException ex)
                        {
                            ex.printStackTrace();
                        }
                        // Repaint the icon's component after the
                        // icon has been created.
                        c.repaint();
                    }
                });
            }
        }
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.javapatterns.proxy;
 
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.Icon;
 
/**
 *客户端代码实现
 * @author geovindu
 *
 * */
public class ImageClient extends JFrame{
 
    private static int IMAGE_WIDTH = 270;
    private static int IMAGE_HEIGHT = 380;
 
    private Icon imageIconProxy = null;
    /**
     *构造子
     * */
    public ImageClient()
    {
        super("图像画像Virtual Proxy Client ");
        imageIconProxy = new ImageIconProxy("resource/geovindu.jpg", IMAGE_WIDTH, IMAGE_HEIGHT);
 
        setBounds(200, 200, IMAGE_WIDTH + 10, IMAGE_HEIGHT + 30);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    /**
     *实理画像
     * @param g
     *
     * */
    public void paint(Graphics g)
    {
        super.paint(g);
        Insets insets = getInsets();
        imageIconProxy.paintIcon(this, g, insets.left , insets.top);
    }
}

  

调用测试:

1
2
3
//代理模式
ImageClient app = new ImageClient();
app.show();

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2021-09-18 sql server: Parent/Child hierarchy tree view
2016-09-18 csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
2015-09-18 sql:Oracle11g 表,视图,存储过程结构查询
2015-09-18 sql:MySQL 6.7 表,视图,存储过程结构查询
2010-09-18 csharp:DataRelation 对象访问相关数据表中的记录
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示