Vous êtes sur la page 1sur 13

Abstract Factory Pattern

Abstract Factory method pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is
used to create a set of related objects, or dependent objects. Internally, Abstract Factory use Factory design pattern
for creating objects. It may also use Builder design pattern and prototype design pattern for creating objects. It
completely depends upon your implementation for creating objects. In this article, I would like share what is abstract
factory pattern and how is it work?
What is Abstract Factory Pattern?
Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of
factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent
objects without specifying their concrete classes.
Abstract Factory Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the abstract factory design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:
1.

AbstractFactory
This is an interface which is used to create abstract product

2.

ConcreteFactory
This is a class which implements the AbstractFactory interface to create concrete products.

3.

AbstractProduct
This is an interface which declares a type of product.

4.

ConcreteProduct
This is a class which implements the AbstractProduct interface to create product.

5.

Client
This is a class which use AbstractFactory and AbstractProduct interfaces to create a family of related
objects.

C# - Implementation Code
1.

public interface AbstractFactory

2.

3.

AbstractProductA CreateProductA();

4.
5.

AbstractProductB CreateProductB();

6.

7.
8.

public class ConcreteFactoryA : AbstractFactory

9.

10. public AbstractProductA CreateProductA()


11. {
12. return new ProductA1();
13. }
14.
15. public AbstractProductB CreateProductB()
16. {
17. return new ProductB1();
18. }
19. }
20.

21. public class ConcreteFactoryB : AbstractFactory


22. {
23. public AbstractProductA CreateProductA()
24. {
25. return new ProductA2();
26. }
27.
28. public AbstractProductB CreateProductB()
29. {
30. return new ProductB2();
31. }
32. }
33. public interface AbstractProductA { }
34. public class ProductA1 : AbstractProductA { }
35. public class ProductA2 : AbstractProductA { }
36. public interface AbstractProductB { }
37. public class ProductB1 : AbstractProductB { }
38. public class ProductB2 : AbstractProductB { }
39. public class Client
40. {
41. private AbstractProductA _productA;
42. private AbstractProductB _productB;
43.

public Client(AbstractFactory factory)

44. {
45. _productA = factory.CreateProductA();

46. _productB = factory.CreateProductB();


47. }
48. }
Abstract Factory Pattern - Example

Who is what?
The classes, interfaces and objects in the above class diagram can be identified as follows:
1.

VehicleFactory - AbstractFactory interface

2.

HondaFactory & HeroFactory- Concrete Factories

3.

Bike & Scooter - AbstractProduct interface

4.

Regular Bike, Sports Bike, Regular Scooter & Scooty - Concreate Products

5.

VehicleClient - Client

C# - Sample Code
1.

/// <summary>

2.

/// The 'AbstractFactory' interface.

3.

/// </summary>

4.

interface VehicleFactory

5.

6.

Bike GetBike(string Bike);

7.

Scooter GetScooter(string Scooter);

8.

9.
10. /// <summary>
11. /// The 'ConcreteFactory1' class.
12. /// </summary>
13. class HondaFactory : VehicleFactory
14. {
15. public Bike GetBike(string Bike)
16. {
17. switch (Bike)
18. {
19. case "Sports":
20. return new SportsBike();
21. case "Regular":
22. return new RegularBike();
23. default:
24. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));

25. }
26.
27. }
28.
29. public Scooter GetScooter(string Scooter)
30. {
31. switch (Scooter)
32. {
33. case "Sports":
34. return new Scooty();
35. case "Regular":
36. return new RegularScooter();
37. default:
38. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
39. }
40.
41. }
42. }
43.
44. /// <summary>
45. /// The 'ConcreteFactory2' class.
46. /// </summary>
47. class HeroFactory : VehicleFactory
48. {
49. public Bike GetBike(string Bike)

50. {
51. switch (Bike)
52. {
53. case "Sports":
54. return new SportsBike();
55. case "Regular":
56. return new RegularBike();
57. default:
58. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));
59. }
60.
61. }
62.
63. public Scooter GetScooter(string Scooter)
64. {
65. switch (Scooter)
66. {
67. case "Sports":
68. return new Scooty();
69. case "Regular":
70. return new RegularScooter();
71. default:
72. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
73. }
74.

75. }
76. }
77.
78. /// <summary>
79. /// The 'AbstractProductA' interface
80. /// </summary>
81. interface Bike
82. {
83. string Name();
84. }
85.
86. /// <summary>
87. /// The 'AbstractProductB' interface
88. /// </summary>
89. interface Scooter
90. {
91. string Name();
92. }
93.
94. /// <summary>
95. /// The 'ProductA1' class
96. /// </summary>
97. class RegularBike : Bike
98. {
99. public string Name()

100. {
101. return "Regular Bike- Name";
102. }
103.}
104.
105./// <summary>
106./// The 'ProductA2' class
107./// </summary>
108.class SportsBike : Bike
109.{
110. public string Name()
111. {
112. return "Sports Bike- Name";
113. }
114.}
115.
116./// <summary>
117./// The 'ProductB1' class
118./// </summary>
119.class RegularScooter : Scooter
120.{
121. public string Name()
122. {
123. return "Regular Scooter- Name";
124. }

125.}
126.
127./// <summary>
128./// The 'ProductB2' class
129./// </summary>
130.class Scooty : Scooter
131.{
132. public string Name()
133. {
134. return "Scooty- Name";
135. }
136.}
137.
138./// <summary>
139./// The 'Client' class
140./// </summary>
141.class VehicleClient
142.{
143. Bike bike;
144. Scooter scooter;
145.
146. public VehicleClient(VehicleFactory factory, string type)
147. {
148. bike = factory.GetBike(type);
149. scooter = factory.GetScooter(type);

10

150. }
151.
152. public string GetBikeName()
153. {
154. return bike.Name();
155. }
156.
157. public string GetScooterName()
158. {
159. return scooter.Name();
160. }
161.
162.}
163.
164./// <summary>
165./// Abstract Factory Pattern Demo
166./// </summary>
167.class Program
168.{
169. static void Main(string[] args)
170. {
171. VehicleFactory honda = new HondaFactory();
172. VehicleClient hondaclient = new VehicleClient(honda, "Regular");
173.
174. Console.WriteLine("******* Honda **********");

11

175. Console.WriteLine(hondaclient.GetBikeName());
176. Console.WriteLine(hondaclient.GetScooterName());
177.
178. hondaclient = new VehicleClient(honda, "Sports");
179. Console.WriteLine(hondaclient.GetBikeName());
180. Console.WriteLine(hondaclient.GetScooterName());
181.
182. VehicleFactory hero = new HeroFactory();
183. VehicleClient heroclient = new VehicleClient(hero, "Regular");
184.
185. Console.WriteLine("******* Hero **********");
186. Console.WriteLine(heroclient.GetBikeName());
187. Console.WriteLine(heroclient.GetScooterName());
188.
189. heroclient = new VehicleClient(hero, "Sports");
190. Console.WriteLine(heroclient.GetBikeName());
191. Console.WriteLine(heroclient.GetScooterName());
192.
193. Console.ReadKey();
194. }
195.}
Abstract Factory Pattern Demo - Output

12

When to use it?


1.

Create a set of related objects, or dependent objects which must be used together.

2.

System should be configured to work with multiple families of products.

3.

The creation of objects should be independent from the utilizing system.

4.

Concrete classes should be decoupled from clients.

13

Vous aimerez peut-être aussi