-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.cs
More file actions
76 lines (67 loc) · 2.11 KB
/
FactoryMethod.cs
File metadata and controls
76 lines (67 loc) · 2.11 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
68
69
70
71
72
73
74
75
76
namespace DesignPatterns.Creational.FactoryMethod;
/// <summary>
/// Factory Method Pattern
///
/// Intent: Define an interface for creating an object, but let subclasses decide
/// which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
///
/// When to use:
/// - When a class can't anticipate the class of objects it must create
/// - When a class wants its subclasses to specify the objects it creates
///
/// Real-world analogy: A coffee shop franchise where each branch specializes
/// in a different drink — you walk into an EspressoBar and get an espresso,
/// or a LatteBar and get a latte. Each branch (subclass) decides which specific
/// drink to create, while the ordering process stays the same everywhere.
/// </summary>
// TODO: Implement Factory Method
// 1. Product interface
public interface ICoffee
{
string GetName();
void Brew();
}
// 2. Concrete products
public class Espresso : ICoffee
{
public string GetName() => "Espresso";
public void Brew() => Console.WriteLine("Brewing an Espresso...");
}
public class Latte : ICoffee
{
public string GetName() => "Latte";
public void Brew() => Console.WriteLine("Brewing a Latte...");
}
// 3. Creator abstract class with FactoryMethod()
public abstract class CoffeeFactory
{
public abstract ICoffee CreateCoffee();
public void OrderCoffee()
{
var coffee = CreateCoffee();
Console.WriteLine($"Ordered a {coffee.GetName()}");
coffee.Brew();
Console.WriteLine($"{coffee.GetName()} is ready!");
}
}
// 4. Concrete creators overriding FactoryMethod()
public class EspressoFactory : CoffeeFactory
{
public override ICoffee CreateCoffee() => new Espresso();
}
public class LatteFactory : CoffeeFactory
{
public override ICoffee CreateCoffee() => new Latte();
}
// Example usage
public class FactoryMethod
{
public static void Run()
{
CoffeeFactory espressoFactory = new EspressoFactory();
espressoFactory.OrderCoffee();
Console.WriteLine();
CoffeeFactory latteFactory = new LatteFactory();
latteFactory.OrderCoffee();
}
}