Skip to content

Commit 99a04fd

Browse files
authored
Update Readme.md
1 parent 7a60e80 commit 99a04fd

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

Abstract Factory/Readme.md

+153
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,156 @@
11
<p align="center"><img src="https://image.slidesharecdn.com/abstract-factory-pattern-150524162103-lva1-app6891/95/abstract-factory-pattern-example-implementation-in-java-17-638.jpg?cb=1432484689"></p></br>
22

33
The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.
4+
5+
6+
### C# example
7+
8+
9+
```c#
10+
using System;
11+
12+
namespace DoFactory.GangOfFour.Abstract.Structural
13+
{
14+
/// <summary>
15+
/// MainApp startup class for Structural
16+
/// Abstract Factory Design Pattern.
17+
/// </summary>
18+
class MainApp
19+
{
20+
/// <summary>
21+
/// Entry point into console application.
22+
/// </summary>
23+
public static void Main()
24+
{
25+
// Abstract factory #1
26+
AbstractFactory factory1 = new ConcreteFactory1();
27+
Client client1 = new Client(factory1);
28+
client1.Run();
29+
30+
// Abstract factory #2
31+
AbstractFactory factory2 = new ConcreteFactory2();
32+
Client client2 = new Client(factory2);
33+
client2.Run();
34+
35+
// Wait for user input
36+
Console.ReadKey();
37+
}
38+
}
39+
40+
/// <summary>
41+
/// The 'AbstractFactory' abstract class
42+
/// </summary>
43+
abstract class AbstractFactory
44+
{
45+
public abstract AbstractProductA CreateProductA();
46+
public abstract AbstractProductB CreateProductB();
47+
}
48+
49+
50+
/// <summary>
51+
/// The 'ConcreteFactory1' class
52+
/// </summary>
53+
class ConcreteFactory1 : AbstractFactory
54+
{
55+
public override AbstractProductA CreateProductA()
56+
{
57+
return new ProductA1();
58+
}
59+
public override AbstractProductB CreateProductB()
60+
{
61+
return new ProductB1();
62+
}
63+
}
64+
65+
/// <summary>
66+
/// The 'ConcreteFactory2' class
67+
/// </summary>
68+
class ConcreteFactory2 : AbstractFactory
69+
{
70+
public override AbstractProductA CreateProductA()
71+
{
72+
return new ProductA2();
73+
}
74+
public override AbstractProductB CreateProductB()
75+
{
76+
return new ProductB2();
77+
}
78+
}
79+
80+
/// <summary>
81+
/// The 'AbstractProductA' abstract class
82+
/// </summary>
83+
abstract class AbstractProductA
84+
{
85+
}
86+
87+
/// <summary>
88+
/// The 'AbstractProductB' abstract class
89+
/// </summary>
90+
abstract class AbstractProductB
91+
{
92+
public abstract void Interact(AbstractProductA a);
93+
}
94+
95+
96+
/// <summary>
97+
/// The 'ProductA1' class
98+
/// </summary>
99+
class ProductA1 : AbstractProductA
100+
{
101+
}
102+
103+
/// <summary>
104+
/// The 'ProductB1' class
105+
/// </summary>
106+
class ProductB1 : AbstractProductB
107+
{
108+
public override void Interact(AbstractProductA a)
109+
{
110+
Console.WriteLine(this.GetType().Name +
111+
" interacts with " + a.GetType().Name);
112+
}
113+
}
114+
115+
/// <summary>
116+
/// The 'ProductA2' class
117+
/// </summary>
118+
class ProductA2 : AbstractProductA
119+
{
120+
}
121+
122+
/// <summary>
123+
/// The 'ProductB2' class
124+
/// </summary>
125+
class ProductB2 : AbstractProductB
126+
{
127+
public override void Interact(AbstractProductA a)
128+
{
129+
Console.WriteLine(this.GetType().Name +
130+
" interacts with " + a.GetType().Name);
131+
}
132+
}
133+
134+
/// <summary>
135+
/// The 'Client' class. Interaction environment for the products.
136+
/// </summary>
137+
class Client
138+
{
139+
private AbstractProductA _abstractProductA;
140+
private AbstractProductB _abstractProductB;
141+
142+
// Constructor
143+
public Client(AbstractFactory factory)
144+
{
145+
_abstractProductB = factory.CreateProductB();
146+
_abstractProductA = factory.CreateProductA();
147+
}
148+
149+
public void Run()
150+
{
151+
_abstractProductB.Interact(_abstractProductA);
152+
}
153+
}
154+
}
155+
156+
```

0 commit comments

Comments
 (0)