Csharp: Abstract Factory Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// <summary>
   /// Summary description for Garden.
   /// Abstract Factory Patterns
   /// geovindu,Geovin Du,涂聚文
   /// </summary>
   public class Garden
   {
       protected Plant center, shade, border;
       protected bool showCenter, showShade, showBorder;
       //select which ones to display
       public void setCenter() { showCenter = true; }
       public void setBorder() { showBorder = true; }
       public void setShade() { showShade = true; }
       /// <summary>
       /// draw each plant
       /// </summary>
       /// <param name="g"></param>
       public void draw(Graphics g)
       {
           if (showCenter) center.draw(g, 100, 100);
           if (showShade) shade.draw(g, 10, 50);
           if (showBorder) border.draw(g, 50, 150);
       }
   }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
   /// Summary description for Plant.
   /// Abstract Factory Patterns
   /// geovindu,Geovin Du,涂聚文
   /// </summary>
   public class Plant
   {
       private string name;
       private Brush br;
       private Font font;
       /// <summary>
       ///
       /// </summary>
       /// <param name="pname"></param>
       public Plant(string pname)
       {
           name = pname;     //save name
           font = new Font("Arial", 12);
           br = new SolidBrush(Color.Black);
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="g"></param>
       /// <param name="x"></param>
       /// <param name="y"></param>
       public void draw(Graphics g, int x, int y)
       {
           g.DrawString(name, font, br, x, y);
       }
   }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <summary>
/// Summary description for AnnualGarden.
/// Abstract Factory Patterns
/// geovindu,Geovin Du,涂聚文
/// </summary>
public class AnnualGarden : Garden
{
 
    /// <summary>
    ///
    /// </summary>
    public AnnualGarden()
    {
        shade = new Plant("Coleus");
        border = new Plant("Alyssum");
        center = new Plant("Marigold");
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// Summary description for VeggieGarden.
/// Abstract Factory Patterns
/// geovindu,Geovin Du,涂聚文
/// </summary>
public class VeggieGarden : Garden
{
    public VeggieGarden()
    {
        shade = new Plant("Broccoli");
        border = new Plant("Peas");
        center = new Plant("Corn");
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// Summary description for PerennialGarden.
/// Abstract Factory Patterns
/// geovindu,Geovin Du,涂聚文
/// </summary>
public class PerennialGarden : Garden
{
    public PerennialGarden()
    {
        shade = new Plant("Astilbe");
        border = new Plant("Dicentrum");
        center = new Plant("Sedum");
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// <summary>
    /// Abstract Factory Patterns
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public partial class GdPic : System.Windows.Forms.PictureBox
    {
 
  
        private Brush br;
        private Garden gden;
 
        /// <summary>
        ///
        /// </summary>
        private void init()
        {
            br = new SolidBrush(Color.LightGray);
        }
        /// <summary>
        ///
        /// </summary>
        public GdPic()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="garden"></param>
        public void setGarden(Garden garden)
        {
            gden = garden;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            g.FillEllipse(br, 5, 5, 100, 100);
            if (gden != null)
                gden.draw(g);
        }
 
   
    }

 窗體調用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/// <summary>
   /// Abstract Factory Patterns
   /// geovindu,Geovin Du,涂聚文
   /// </summary>
   public partial class AbstractFactoryForm : Form
   {
 
       private Garden garden;
       /// <summary>
       ///
       /// </summary>
       public AbstractFactoryForm()
       {
           InitializeComponent();
          
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void AbstractFactoryForm_Load(object sender, EventArgs e)
       {
 
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void ckCenter_CheckedChanged(object sender, EventArgs e)
       {
           garden.setCenter();
           gdPic1.Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void ckBorder_CheckedChanged(object sender, EventArgs e)
       {
           garden.setBorder();
           gdPic1.Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void ckShade_CheckedChanged(object sender, EventArgs e)
       {
           garden.setShade();
           gdPic1.Refresh();
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void opAnnual_CheckedChanged(object sender, EventArgs e)
       {
           setGarden(new AnnualGarden());
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void opVegetable_CheckedChanged(object sender, EventArgs e)
       {
           setGarden(new VeggieGarden());
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void opPerennial_CheckedChanged(object sender, EventArgs e)
       {
           setGarden(new PerennialGarden());
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="gd"></param>
       private void setGarden(Garden gd)
       {
           garden = gd;            //save current garden
           gdPic1.setGarden(gd);   //tell picture bos
           gdPic1.Refresh();       //repaint it
           ckCenter.Checked = false;   //clear all
           ckBorder.Checked = false;   //check
           ckShade.Checked = false;    //boxes
       }
   }

  

 

posted @   ®Geovin Du Dream Park™  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2012-09-17 SQL 生成公曆和農曆對照數據,公曆查找農曆和農曆查找公曆函數
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示