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();

        }
    }

  

输出:

 

posted @ 2022-09-27 00:14  ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报