Java: Proxy Pattern
/**
* 版权所有 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);
}
/**
* 版权所有 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 涂聚文";
}
}
/**
* 版权所有 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;
}
}
}
/**
* 版权所有 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();
}
}
/**
* 版权所有 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();
}
}
调用测试:、
//代理模式
searcher = new Proxy();
String userId = "geovindu";
String searchType = "SEARCH_BY_ACCOUNT_NUMBER";
String result = searcher.doSearch(userId, searchType);
System.out.println(result);
输出:
已在保存日志!操作用户 geovindu result set 涂聚文
/**
* 版权所有 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();
}
});
}
}
}
}
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);
}
}
调用测试:
//代理模式
ImageClient app = new ImageClient();
app.show();
输出:

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号