- 浏览: 533010 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (231)
- 一个操作系统的实现 (20)
- 汇编(NASM) (12)
- Linux编程 (11)
- 项目管理 (4)
- 计算机网络 (8)
- 设计模式(抽象&封装) (17)
- 数据结构和算法 (32)
- java基础 (6)
- UML细节 (2)
- C/C++ (31)
- Windows (2)
- 乱七八糟 (13)
- MyLaB (6)
- 系统程序员-成长计划 (8)
- POJ部分题目 (10)
- 数学 (6)
- 分布式 & 云计算 (2)
- python (13)
- 面试 (1)
- 链接、装载与库 (11)
- java并行编程 (3)
- 数据库 (0)
- 体系结构 (3)
- C++ template / STL (4)
- Linux环境和脚本 (6)
最新评论
-
chuanwang66:
默默水塘 写道typedef void(*Fun)(void) ...
C++虚函数表(转) -
默默水塘:
typedef void(*Fun)(void);
C++虚函数表(转) -
lishaoqingmn:
写的很好,例子简单明了,将观察者模式都表达了出来。
这里是ja ...
观察者模式——Observer
A Classic Example: Image Proxies
以下是最朴素的代理模式实现,其中只有“代理”的思想,但并不是真正的代理。
一个ImageIconProxy proxy实例被封装在JFrame frame中,且他们都是ShowProxy属性。
点击“Load按钮”触发调用proxy.load(frame),该方法重新设置proxy内部current对象为ImageIcon LOADING,立即repaint窗口frame,将短暂显示Loading…。然后,该方法启动独立线程加载最终图片,这个独立线程也是通过重新设置proxy内部current对象,然后重绘(调用callbackFrame.pack())的方式。
重绘通过ImageIconProxy实现以下三个方法:
getIconHeight()、getIconWidth()和paintIcon(...),实现时只需将ImageIconProxy中current指示的图片绘制出来。注意到,ImageIconProxy继承了ImageIcon,并实现了该父类的此三种方法后就可以被调用者当作一个ImageIcon实例使用了。
代码:
文件一:ShowProxy.java
package app.proxy;
public class ShowProxy implements ActionListener {
private ImageIconProxy proxy = new ImageIconProxy("images/fest.jpg");
private JFrame frame;
private JButton loadButton;
/*********************************Load按钮*******************************/
protected JButton loadButton() {
if (loadButton == null) {
loadButton = new JButton("Load");
loadButton.addActionListener(this);//ShowProxy实现监听按钮
loadButton.setFont(SwingFacade.getStandardFont());
}
return loadButton;
}
/**
* 当用户点击按钮时,触发监听器执行这个函数
* Start loading the image and disable the Load button.
*/
public void actionPerformed(ActionEvent e) {
/*
* 理解P117这句话:
* The proxy does its work by judiciously forwarding requests to
* the underlying object that the proxy controls access to.
*/
proxy.load(frame); //这里是关键!!!
loadButton().setEnabled(false);
}
/*********************************JPanel**********************************/
protected JPanel mainPanel() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
/*
* ImageIconProxy被封装在JLabel内部-->
* JLabel(Icon);
* ImageIconProxy extends ImageIcon;
* ImageIcon implements javax.swing.Icon
*/
p.add("Center", new JLabel(proxy));
/*
* 添加按钮到JPanel,点击按钮后调用方法:
* proxy.load(frame);
*/
p.add("South", loadButton());
return p;
}
/*********************************main()**********************************/
public static void main(String[] args) {
ShowProxy sp = new ShowProxy();
sp.frame = SwingFacade.launch(sp.mainPanel(), " Proxy");
/*
* 1. com.oozinoz.ui.SwingFacade:
* This utility class provides an interface that makes
* the Swing subsystem easy to use.
*
* 2. JFrame com.oozinoz.ui.SwingFacade.launch(Component c, String title):
* Display the given component within a frame.
*/
}
}
文件二:ImageIconProxy.java --> 可以看做是代理
public class ImageIconProxy extends ImageIcon implements Runnable {
static final ImageIcon ABSENT = new ImageIcon(ClassLoader
.getSystemResource("images/absent.jpg"));
static final ImageIcon LOADING = new ImageIcon(ClassLoader
.getSystemResource("images/loading.jpg"));
ImageIcon current = ABSENT;//只会显示current指示的图片
protected String filename;
protected JFrame callbackFrame;
public ImageIconProxy(String filename) {
super(ABSENT.getImage());// Image javax.swing.ImageIcon.getImage()
this.filename = filename;
}
public void load(JFrame callbackFrame) {
//1、将图片设置为LOADING
this.callbackFrame = callbackFrame;
current = LOADING;
callbackFrame.repaint();
//2、利用一个独立线程,加载最终图片
/*
* 仅当点击Load按钮时,触发proxy.load(frame);(在类ShowProxy中);
* 初始时,ImageIconProxy被实例化,但并没有启动它的线程
*/
new Thread(this).start();
}
public void run() {
current = new ImageIcon(ClassLoader.getSystemResource(filename));
/*
* void java.awt.Window.pack():
* Causes this Window to be sized to fit the preferred size and
* layouts of its subcomponents...
*/
callbackFrame.pack();
}
public int getIconHeight() {
return current.getIconHeight();
}
public int getIconWidth() {
return current.getIconWidth();
}
/**
* Paint the Icon
*/
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
current.paintIcon(c, g, x, y);
}
}
Q&A:
The ImageIconProxy class is not a well-designed, reusable component. Point out two problems with the design.
1. Forwarding only a subset of calls to an underlying ImageIcon object is dangerous. The ImageIconProxy class inherits a dozen fields and at least 25 methods from the ImageIcon class. To be a true proxy, the ImageIconProxy object needs to forward most or all of these calls. Thorough forwarding would require many potentially erroneous methods, and this code would require maintenance as the ImageIcon class and its superclasses change over time.
(ImageIconProxy对象)仅向底层的ImageIcon对象转发部分调用是危险的。ImageIconProxy对象从ImageIcon类集成了一打域和至少25个方法。作为一个真正的代理,ImageIconProxy必须转发大多数或全部调用。“完全转发”可能导致潜在错误,而且这样的代码要求随ImageIcon类及其父类改变而改变。
2. You might question whether the “Absent” image and the desired image are in the right places in the design. It might make more sense to have the images passed in rather than making the class responsible for finding them.
你或许对"Absent"图片和最终图片的位置争取性存在疑问。或许让这些图片被传入,而非让ImageIconProxy来负责他们,更加合理。
见附件中使用oozinoz提供的包简单实现窗口的代码
发表评论
-
(第十章)一个xml解析器和构造器
2013-03-10 16:40 897本章的前两节“10.1 状态机”、“10.2 ... -
享元模式——Flyweight
2012-02-17 13:10 1014享元模式——Flyweig ... -
工厂方法和抽象工厂——Factory Method & Abstract Factory
2012-01-04 17:14 2049一、使用抽象工厂和工厂方法 Factory Me ... -
单例模式——Singleton
2012-01-04 17:08 975public class Singleton { ... -
观察者模式——Observer
2012-01-04 16:25 1280观察者模式—— Observer ... -
适配器模式——Adaptor(Adapter)
2012-01-01 18:23 1368适配器模式 —— Adapto ... -
状态模式——State (更好的实现状态机)
2011-12-28 14:10 655801. 概述 The intent o ... -
装饰者模式——Decorator
2011-12-25 11:11 1141装饰者模式—— Decorator ... -
组合模式——Composite
2011-12-24 14:27 9741. Composite 定义 : ... -
构造者模式——Builder
2011-08-10 13:59 1030构造者模式——Builder 本文是《Java设计模 ... -
责任链模式——Chain of Responsibility
2011-08-10 11:26 893一、总结《Java设计模式》Chapter12 Chain o ... -
代理模式Dynamic Proxies(四、Struts2.0拦截器Interceptor)
2011-08-01 11:31 1379一、概念和注意点: Once you write a d ... -
代理模式Remote Proxies(三、RMI)
2011-08-01 09:51 1665因为我本科毕业设计中大量采用RMI实现分布式,且使用了Ecli ... -
代理模式Image Proxies(二、最朴素实现)
2011-07-31 11:55 955在前面《 代理模式Image Proxies(一、最朴素实现) ... -
命令模式——Command
2011-06-10 10:31 912偷懒一下,直接用JavaEye上chjavach老兄的文章了, ... -
策略模式——strategy
2011-06-02 12:36 847Strategy Pattern ...
相关推荐
NULL 博文链接:https://chuanwang66.iteye.com/blog/1136589
NULL 博文链接:https://chuanwang66.iteye.com/blog/1137037
如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求: import requests proxies = { "http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080", } requests.get(...
Laravel开发-laravel-valid-proxies Laravel有效代理
用Proxies实现对象撤消,使重做和更改历史记录
代理ip池(自动采集ip地址 从网站获取ip地址,检测是否可用,并实时更新,存入数据库中) 代理ip池(自动采集ip地址 从网站获取ip地址,检测是否可用,并实时更新,存入数据库中)
利用 MySQL proxies_priv(模拟角色)实现类似用户组管理 角色(Role)可以用来批量管理用户,同一个角色下的用户,拥有相同的权限。 MySQL5.7.X以后可以模拟角色(Role)的功能,通过mysql.proxies_priv模拟实现 1、...
资源分类:Python库 所属语言:Python 资源全名:proxies_l-0.0.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Koa代理 middlware HTTP代理 由提供支持。 安装 $ npm install koa-proxies --save 选件 http-proxy活动 options . events = { error ( err , req , res ) { } , proxyReq ( proxyReq , req , res ) { } , ...
代理人 Tokio堆栈的代理协议。 用法 查看服务器和客户端示例的examples/目录。 地位 http 服务器端 客户端 袜子5 服务器端 客户端
Trusted Proxies GeoSet工具栏允许您快速连接到Trusted Proxies帐户上的任何代理服务器。 使用“受信任的代理” GeoSet工具栏,您可以在“受信任的代理”帐户上的所有地理位置代理之间无缝切换。 还没有Trusted ...
LIST_PATH选项,以及一个带有代理的文件的路径,每行一个: ROTATING_PROXY_LIST_PATH = '/my/path/proxies.txt'如果同时存在两个选项,则ROTATING_PROXY_LIST_PATH优先于ROTATING_PROXY_LIST 。
A simple source code to explain how to scrape proxies in Delphi.
另一个代理客户端源码,基于c#,.net框架,代码相交简单。比较实用
* OutputFilecheck proxies.json valid_proxies.jsonfilter valid_proxies.json <HTTP> output.json例如: 搜寻所有受支持的网站并将代理保存到proxies.json crawl proxies.json爬网并将代理保存到proxies.json: ...
要判断客户端是否使用代理服务器,可以从客户端所发送的环境变量信息来判断。 具体来说,就是看HTTP_VIA字段,如果这个字段设置了,...二、使用透明代理服务器的情况:Transparent Proxies REMOTE_ADDR = 代理服务器
weird_proxies:反向代理备忘单
java动态代理,j-dynproxies-source