CSharp: Proxy Pattern
/// <summary> /// Proxy Patterns 代理模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface DuImager { Image getImage(); }
/// <summary> /// Proxy Patterns 代理模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class QuickImage:DuImager { /// <summary> /// /// </summary> public QuickImage() { } /// <summary> /// Box.gif /// </summary> /// <returns></returns> public Image getImage() { return new Bitmap("flowrtree.jpg"); //flowrtree.jpg } }
/// <summary> /// Proxy Patterns 代理模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class FinalImage:DuImager { /// <summary> /// /// </summary> public FinalImage() { } /// <summary> /// /// </summary> /// <returns></returns> public Image getImage() { return new Bitmap("flowrtree.jpg"); } }
/// <summary> /// Proxy Patterns 代理模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class ImageProxy { /// <summary> /// /// </summary> private bool done; /// <summary> /// /// </summary> private Timer timer; /// <summary> /// /// </summary> public ImageProxy() { timer = new Timer(new TimerCallback(timerCall), this, 5000, 0); } /// <summary> /// /// </summary> /// <param name="obj"></param> private void timerCall(object obj) { done = true; timer.Dispose(); } /// <summary> /// /// </summary> /// <returns></returns> public Image getImage() { DuImager img; if (done) img = new FinalImage(); else img = new QuickImage(); return img.getImage(); } }
调用试试:
/// <summary> /// Proxy Patterns 代理模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ProxyPatternsForm : Form { private ImageProxy imgProxy; /// <summary> /// /// </summary> private void init() { imgProxy = new ImageProxy(); } /// <summary> /// /// </summary> public ProxyPatternsForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ProxyPatternsForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Pic.Image = imgProxy.getImage(); } }
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)