• CSharp:Flyweight Patterns


      /// <summary>
        /// Summary description for Positioner.
        /// Flyweight Patterns享元 模式
        ///20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class Positioner
        {
            private const int pLeft = 30;
            private const int pTop = 30;
            private const int HSpace = 70;
            private const int VSpace = 80;
            private const int rowMax = 2;
            private int x, y, cnt;
            /// <summary>
            /// 
            /// </summary>
            public Positioner()
            {
                reset();
            }
            /// <summary>
            /// 
            /// </summary>
            public void reset()
            {
                x = pLeft;
                y = pTop;
                cnt = 0;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public int nextX()
            {
                return x;
            }
            /// <summary>
            /// 
            /// </summary>
            public void incr()
            {
                cnt++;
                if (cnt > rowMax)
                {	//reset to start new row
                    cnt = 0;
                    x = pLeft;
                    y += VSpace;
                }
                else
                {
                    x += HSpace;
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public int nextY()
            {
                return y;
            }
        }
    

      

    /// <summary>
        /// Flyweight Patterns享元 模式
        ///20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class Rectangle
        {
            private int x1, x2, y1, y2;
            private int w, h;
            public Rectangle() { }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            public void init(int x, int y)
            {
                x1 = x;
                y1 = y;
                x2 = x1 + w;
                y2 = y1 + h;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="w_"></param>
            /// <param name="h_"></param>
            public void setSize(int w_, int h_)
            {
                w = w_;
                h = h_;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="xp"></param>
            /// <param name="yp"></param>
            /// <returns></returns>
            public bool contains(int xp, int yp)
            {
                return (x1 <= xp) && (xp <= x2) &&
                        (y1 <= yp) && (yp <= y2);
            }
        }
    

      

       /// <summary>
        /// Summary description for Folder.
        /// Flyweight Patterns享元 模式
        ///20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class Folder
        {
            //Draws a folder at the specified coordinates
            private const int w = 50;
            private const int h = 30;
            private Pen blackPen, whitePen;
            private Pen grayPen;
            private SolidBrush backBrush, blackBrush;
            private Font fnt;
            /// <summary>
            /// 
            /// </summary>
            /// <param name="col"></param>
            public Folder(Color col)
            {
                backBrush = new SolidBrush(col);
                blackBrush = new SolidBrush(Color.Black);
                blackPen = new Pen(Color.Black);
                whitePen = new Pen(Color.White);
                grayPen = new Pen(Color.Gray);
                fnt = new Font("Arial", 12);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="g"></param>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <param name="title"></param>
            public void draw(Graphics g, int x, int y, string title)
            {
                g.FillRectangle(backBrush, x, y, w, h);
                g.DrawRectangle(blackPen, x, y, w, h);
                g.DrawLine(whitePen, x + 1, y + 1, x + w - 1, y + 1);
                g.DrawLine(whitePen, x + 1, y, x + 1, y + h);
    
                g.DrawRectangle(blackPen, x + 5, y - 5, 15, 5);
                g.FillRectangle(backBrush, x + 6, y - 4, 13, 6);
    
                g.DrawLine(grayPen, x, y + h - 1, x + w, y + h - 1);
                g.DrawLine(grayPen, x + w - 1, y, x + w - 1, y + h - 1);
                g.DrawString(title, fnt, blackBrush, x, y + h + 5);
            }
        }
    

      

    /// <summary>
        /// Summary description for FolderFactory.
        /// Flyweight Patterns享元 模式
        ///20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public class FolderFactory
        {
            private Folder selFolder, unselFolder;
            /// <summary>
            /// 
            /// </summary>
            public FolderFactory()
            {
                //create the two folders
                selFolder = new Folder(Color.Brown);
                unselFolder = new Folder(Color.Bisque);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="selected"></param>
            /// <returns></returns>
            public Folder getFolder(bool selected)
            {
                if (selected)
                    return selFolder;
                else
                    return unselFolder;
            }
        }
    

      

    窗体调用:

      /// <summary>
        /// Flyweight Patterns享元 模式
        ///20220918
        /// geovindu,Geovin Du,涂聚文
        /// </summary>
        public partial class FlyweightPatternsForm : Form
        {
            private ArrayList names;
            private Folder fol;
            private const int pLeft = 30;
            private const int pTop = 30;
            private const int HSpace = 70;
            private const int VSpace = 80;
            private const int rowMax = 2;
            private string selectedName;
            private FlyweightPatterns.Rectangle rect;
            private FolderFactory folFact;
            private Positioner posn;
            /// <summary>
            /// 
            /// </summary>
            private void init()
            {
                names = new ArrayList();
                names.Add("Adam");
                names.Add("Bill");
                names.Add("Charlie");
                names.Add("Dave");
                names.Add("Edward");
                names.Add("Fred");
                names.Add("George");
                fol = new Folder(Color.Bisque);
                selectedName = (string)names[0];
                Pic.Paint += new PaintEventHandler(picPaint);
                rect = new FlyweightPatterns.Rectangle();
                rect.setSize(50, 30);
                folFact = new FolderFactory();
                posn = new Positioner();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void picPaint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                posn.reset();
                for (int i = 0; i < names.Count; i++)
                {
                    fol = folFact.getFolder(selectedName.Equals((string)names[i]));
                    fol.draw(g, posn.nextX(), posn.nextY(), (string)names[i]);
                    posn.incr();
                }
            }
            /// <summary>
            /// 
            /// </summary>
            public FlyweightPatternsForm()
            {
                InitializeComponent();
                init();
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void FlyweightPatternsForm_Load(object sender, EventArgs e)
            {
    
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Pic_Click(object sender, EventArgs e)
            {
    
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Pic_MouseMove(object sender, MouseEventArgs e)
            {
                string oldname = selectedName;  //save old name
                bool found = false;
                posn.reset();
                int i = 0;
                selectedName = "";
                while (i < names.Count && !found)
                {
                    rect.init(posn.nextX(), posn.nextY());
                    //see if a rectangle contains the mouse
                    if (rect.contains(e.X, e.Y))
                    {
                        selectedName = (string)names[i];
                        found = true;
                    }
                    posn.incr();
                    i++;
                }
                //only refresh if mouse in new rectangle
                if (!oldname.Equals(selectedName))
                {
                    Pic.Refresh();
                }
            }
        }
    

      

    输出:

  • 相关阅读:
    损失函数及经验风险和结构风险
    ML面试题网站及ML模型网站
    数据归一化
    逻辑回归
    什么是凸函数及如何判断一个函数是否是凸函数
    Feature Selection Can Reduce Overfitting And RF Show Feature Importance
    Python Machine Learning-Chapter4
    skywalking与pinpoint全链路追踪方案对比
    全)Java从单体到微服务打造房产销售平台 2018年慕课网 高清视频+源码
    CentOS7 通过YUM安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/geovindu/p/16729155.html
Copyright © 2020-2023  润新知