diff --git a/Design Pattern Examples/Flyweight/Program.cs b/Design Pattern Examples/Flyweight/Program.cs new file mode 100644 index 0000000..a389402 --- /dev/null +++ b/Design Pattern Examples/Flyweight/Program.cs @@ -0,0 +1,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 shapes = new Dictionary(); + + 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()); + } + } +}