oo-70-245-1

oo-70-245-1 Commit Details


Date:2015-11-02 20:44:30 (9 years 5 months ago)
Author:Natalie Adams
Branch:master
Commit:7268bfc02009f62f77f8b3d30d546aaf3a2822d4
Parents: ca71467ff7e4d77b7e84f379e038b474f5bd7b71
Message:adding flyweight example

Changes:

File differences

Design Pattern Examples/Flyweight/Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace flyweightexample
{
    public interface IShape
    {
        void Print();
    }
    public class Rectangle : IShape
    {
        public void Print()
        {
            Console.WriteLine("Printing Rectangle");
        }
    }
    public class Cricle : IShape
    {
        public void Print()
        {
            Console.WriteLine("Printing Circle");
        }
    }
    public class ShapeObjectFactory
    {
        Dictionary<string, IShape> shapes = new Dictionary<string, IShape>();
        public int TotalObjectsCreated()
        {
            return shapes.Count;
        }
        public IShape GetShape(string ShapeName)
        {
            IShape shape = null;
            if (shapes.ContainsKey(ShapeName))
            {
                shape = shapes[ShapeName];
            }
            else
            {
                switch (ShapeName)
                {
                    case "Rectangle":
                        shape = new Rectangle();
                        shapes.Add("Rectangle", shape);
                        break;
                    case "Circle":
                        shape = new Cricle();
                        shapes.Add("Circle", shape);
                        break;
                    default:
                        throw new Exception("Factory cannot create the object specified");
                }
            }
            return shape;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ShapeObjectFactory sof = new ShapeObjectFactory();
            IShape shape = sof.GetShape("Rectangle");
            shape.Print();
            shape = sof.GetShape("Rectangle");
            shape.Print();
            shape = sof.GetShape("Circle");
            shape.Print();
            shape = sof.GetShape("Circle");
            shape.Print();
            Console.WriteLine("\nTotal Number of objects created = {0}", sof.TotalObjectsCreated());
        }
    }
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.07706s using 14 queries.