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

  

輸出:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章