-> In Abstract Factory Pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory Pattern.
-> Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories.
Implementation
1) Create an interface for Shapes
1 2 3 4 |
public interface IShape { void draw(); } |
2) Create concrete classes implementing the same interface.
1 2 3 4 5 6 7 |
public class Circle : IShape { public void draw() { Console.WriteLine("Inside Circle : Draw() method."); } } |
1 2 3 4 5 6 7 |
public class Rectangle : IShape { public void draw() { Console.WriteLine("Inside Rectangle : Draw() method."); } } |
1 2 3 4 5 6 7 |
public class Square : IShape { public void draw() { Console.WriteLine("Inside Square : Draw() method."); } } |
3) Create an interface for Colors
1 2 3 4 |
public interface IColor { void fill(); } |
4) Create concrete classes implementing IColor Interface.
1 2 3 4 5 6 7 |
public class Blue : IColor { public void fill() { Console.WriteLine("Inside Blue : fill() method."); } } |
1 2 3 4 5 6 7 |
public class Green : IColor { public void fill() { Console.WriteLine("Inside Green : fill() method."); } } |
1 2 3 4 5 6 7 |
public class Red : IColor { public void fill() { Console.WriteLine("Inside Red : fill() method."); } } |
5) Create an Abstract class to get factories for Color and Shape Objects.
1 2 3 4 5 |
public abstract class AbstractFactory { public abstract IColor getColor(String color); public abstract IShape getShape(String shape); } |
6) Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.
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 |
public class ShapeFactory : AbstractFactory { public override IShape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.Equals("CIRCLE")) { return new Circle(); } else if (shapeType.Equals("RECTANGLE")) { return new Rectangle(); } else if (shapeType.Equals("SQUARE")) { return new Square(); } return null; } public override IColor getColor(String color) { return null; } } |
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 |
public class ColorFactory : AbstractFactory { public override IShape getShape(String shapeType) { return null; } public override IColor getColor(String color) { if (color == null) { return null; } if (color.Equals("RED")) { return new Red(); } else if (color.Equals("GREEN")) { return new Green(); } else if (color.Equals("BLUE")) { return new Blue(); } return null; } } |
7) Create a Factory generator/producer class to get factories by passing an information such as Shape or Color.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class FactoryProducer { public static AbstractFactory getFactory(String choice) { if (choice.Equals("SHAPE")) { return new ShapeFactory(); } else if (choice.Equals("COLOR")) { return new ColorFactory(); } return null; } } |
8) Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.
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 |
class Program { static void Main(string[] args) { //get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); //get an object of Shape Circle IShape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Shape Circle shape1.draw(); //get an object of Shape Rectangle IShape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape2.draw(); //get an object of Shape Square IShape shape3 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape3.draw(); //get color factory AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); //get an object of Color Red IColor color1 = colorFactory.getColor("RED"); //call fill method of Red color1.fill(); //get an object of Color Green IColor color2 = colorFactory.getColor("GREEN"); //call fill method of Green color2.fill(); //get an object of Color Blue IColor color3 = colorFactory.getColor("BLUE"); //call fill method of Color Blue color3.fill(); Console.ReadKey(); } } |