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