-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory.cs
More file actions
67 lines (50 loc) · 1.45 KB
/
AbstractFactory.cs
File metadata and controls
67 lines (50 loc) · 1.45 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void Main()
{
var regularCustomer = new Customer(new RegularClothingFactory());
$"{nameof(regularCustomer)} buying clothes ...".Dump();
regularCustomer.BuyClothes();
var funkyCustomer = new Customer(new FunkyClothingFactory());
$"{nameof(funkyCustomer)} buying clothes ...".Dump();
funkyCustomer.BuyClothes();
}
// Client, using the abstract factory to hide the creation details
public class Customer
{
ClothingFactory _factory;
public Customer(ClothingFactory factory)
=> _factory = factory;
public void BuyClothes()
{
_factory.CreateSweater().Dump("Bought:");
_factory.CreateTrouser().Dump("Bought:");
}
}
// Products' abstractions
public interface Sweater { }
public interface Trouser { }
// Concrete products
public record FunkySweater() : Sweater;
public record FunkyTrouser() : Trouser;
public record RegularSweater() : Sweater;
public record RegularTrouser() : Trouser;
// Abstract factory
public interface ClothingFactory
{
Sweater CreateSweater();
Trouser CreateTrouser();
}
// Concrete factories
public class FunkyClothingFactory : ClothingFactory
{
public Sweater CreateSweater()
=> new FunkySweater();
public Trouser CreateTrouser()
=> new FunkyTrouser();
}
public class RegularClothingFactory : ClothingFactory
{
public Sweater CreateSweater()
=> new RegularSweater();
public Trouser CreateTrouser()
=> new RegularTrouser();
}