-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTileDemo.cs
More file actions
75 lines (62 loc) · 1.88 KB
/
TileDemo.cs
File metadata and controls
75 lines (62 loc) · 1.88 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SoftwarePatterns.Core.Flyweight;
namespace SoftwarePatterns.Forms
{
public partial class TileDemo : Form
{
private readonly Random random = new Random();
public TileDemo()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//DrawTiles(e);
DrawTilesFlyweight(e);
}
private void DrawTilesFlyweight(PaintEventArgs e)
{
//with flyweight pattern applied
var factory = new TileFactory();
for (int i = 0; i < 20; i++)
{
var ceramicTile = factory.GetTile("Ceramic");
ceramicTile.Draw(e.Graphics, GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
}
for (int i = 0; i < 20; i++)
{
var stoneTile = factory.GetTile("Stone");
stoneTile.Draw(e.Graphics,GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
}
toolStripStatusLabel1.Text = "Total Objects Created: " + Convert.ToString(StoneFlyweightTile.ObjectCounter + CeramicFlyweightTile.ObjectCounter);
}
private void DrawTiles(PaintEventArgs e)
{
//without the flyweight pattern applied
for (int i = 0; i < 20; i++)
{
ITile ceramicTile = new CeramicTile(GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
ceramicTile.Draw(e.Graphics);
}
for (int i = 0; i < 20; i++)
{
ITile stoneTile = new StoneTile(GetRandomNumber(), GetRandomNumber(), GetRandomNumber(), GetRandomNumber());
stoneTile.Draw(e.Graphics);
}
toolStripStatusLabel1.Text = "Total Objects Created: " + Convert.ToString(CeramicTile.ObjectCounter + StoneTile.ObjectCounter);
}
private int GetRandomNumber()
{
return random.Next(100);
}
}
}