-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (69 loc) · 1.75 KB
/
Program.cs
File metadata and controls
85 lines (69 loc) · 1.75 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
77
78
79
80
81
82
83
84
85
using System;
namespace AbstractFactory;
public interface IButton
{
void Render();
}
public interface ICheckbox
{
void Toggle();
}
public interface IWidgetFactory
{
IButton CreateButton();
ICheckbox CreateCheckbox();
}
public class LightButton : IButton
{
public void Render() => Console.WriteLine("Rendering light button");
}
public class DarkButton : IButton
{
public void Render() => Console.WriteLine("Rendering dark button");
}
public class LightCheckbox : ICheckbox
{
public void Toggle() => Console.WriteLine("Light checkbox toggled");
}
public class DarkCheckbox : ICheckbox
{
public void Toggle() => Console.WriteLine("Dark checkbox toggled");
}
public class LightThemeFactory : IWidgetFactory
{
public IButton CreateButton() => new LightButton();
public ICheckbox CreateCheckbox() => new LightCheckbox();
}
public class DarkThemeFactory : IWidgetFactory
{
public IButton CreateButton() => new DarkButton();
public ICheckbox CreateCheckbox() => new DarkCheckbox();
}
public class SettingsPanel(IWidgetFactory factory)
{
private readonly IButton button = factory.CreateButton();
private readonly ICheckbox checkbox = factory.CreateCheckbox();
public void Draw()
{
button.Render();
checkbox.Toggle();
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Abstract Factory Pattern Demo ===");
Console.WriteLine();
Console.WriteLine("Light theme:");
var lightPanel = new SettingsPanel(new LightThemeFactory());
lightPanel.Draw();
Console.WriteLine();
Console.WriteLine("Dark theme:");
var darkPanel = new SettingsPanel(new DarkThemeFactory());
darkPanel.Draw();
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}