|
2 | 2 | "-> Creational::Singleton": { |
3 | 3 | "prefix": "singleton", |
4 | 4 | "body": [ |
5 | | - "// class Singleton {", |
6 | | - "//\t\tprivate static Singleton _instance = null;", |
7 | | - "//\t\tpublic static Singleton GetInstance() {", |
8 | | - "//\t\t\tif (_instance == null) {", |
9 | | - "//\t\t\t\t_instance = new Singleton();", |
10 | | - "//\t\treturn _instance;", |
11 | | - "//\t\t}", |
12 | | - "//}" |
| 5 | + "class Singleton {", |
| 6 | + "\tprivate static readonly Lazy<Singleton> _instance = new Lazy<Singleton>(() => new Singleton());", |
| 7 | + "\tpublic static Singleton Instance => _instance.Value;", |
| 8 | + "\tprivate Singleton() { }", |
| 9 | + "}" |
13 | 10 | ], |
14 | 11 | "description": "Creational design pattern singleton" |
15 | 12 | }, |
16 | 13 | "-> Creational::Factory Method": { |
17 | 14 | "prefix": "factoryMethod", |
18 | 15 | "body": [ |
19 | | - "// abstract class Creator {", |
20 | | - "//\t\tpublic abstract IProduct FactoryMethod();", |
21 | | - "//\t\tpublic string SomeOperation() {", |
22 | | - "//\t\t\tvar product = FactoryMethod();", |
23 | | - "//\t\t\tvar result = \"Creator: The same creator's code has just worked with + product.Operation();", |
24 | | - "//\t\t\treturn result;", |
25 | | - "// }", |
26 | | - " ", |
27 | | - "// class ConcreteCreator1 : Creator {", |
28 | | - "//\t\tpublic override IProduct FactoryMethod() {", |
29 | | - "// return new ConcreteProduct1();", |
30 | | - "// }", |
31 | | - " ", |
32 | | - "// class ConcreteCreator2 : Creator {", |
33 | | - "//\t\tpublic override IProduct FactoryMethod() {", |
34 | | - "// return new ConcreteProduct2();", |
35 | | - "// }", |
36 | | - " ", |
37 | | - "// public interface IProduct {", |
38 | | - "//\t\tstring Operation()", |
39 | | - "// }", |
40 | | - " ", |
41 | | - "// class ConcreteProduct1 : IProduct {", |
42 | | - "//\t\tpublic string Operation() {", |
43 | | - "// return {Result of concreteProduct1};", |
44 | | - "// }", |
45 | | - " ", |
46 | | - "// class ConcreteProduct2 : IProduct {", |
47 | | - "//\t\tpublic string Operation() {", |
48 | | - "// return {Result of concreteProduct2};", |
49 | | - "// }" |
| 16 | + "public abstract class Creator {", |
| 17 | + "\tpublic abstract IProduct FactoryMethod();", |
| 18 | + "\tpublic string SomeOperation() {", |
| 19 | + "\t\tvar product = FactoryMethod();", |
| 20 | + "\t\treturn $\"Creator: The same creator's code has just worked with {product.Operation()}\";", |
| 21 | + "\t}", |
| 22 | + "}", |
| 23 | + "", |
| 24 | + "public class ConcreteCreator1 : Creator {", |
| 25 | + "\tpublic override IProduct FactoryMethod() => new ConcreteProduct1();", |
| 26 | + "}", |
| 27 | + "", |
| 28 | + "public class ConcreteCreator2 : Creator {", |
| 29 | + "\tpublic override IProduct FactoryMethod() => new ConcreteProduct2();", |
| 30 | + "}", |
| 31 | + "", |
| 32 | + "public interface IProduct {", |
| 33 | + "\tstring Operation();", |
| 34 | + "}", |
| 35 | + "", |
| 36 | + "public class ConcreteProduct1 : IProduct {", |
| 37 | + "\tpublic string Operation() => \"Result of ConcreteProduct1\";", |
| 38 | + "}", |
| 39 | + "", |
| 40 | + "public class ConcreteProduct2 : IProduct {", |
| 41 | + "\tpublic string Operation() => \"Result of ConcreteProduct2\";", |
| 42 | + "}" |
50 | 43 | ], |
51 | 44 | "description": "Creational design pattern factory method" |
52 | 45 | }, |
53 | 46 | "-> Structural::Adapter": { |
54 | 47 | "prefix": "adapter", |
55 | 48 | "body": [ |
56 | | - "// public interface ITarget {", |
57 | | - "//\t\tstring GetRequest();", |
58 | | - "//}", |
59 | | - " ", |
60 | | - "// public class Adaptee {", |
61 | | - "//\t\tpublic string GetSpecificRequest() {", |
62 | | - "//\t\t\treturn 'Specific Request.'", |
63 | | - "//\t\t}", |
64 | | - "//}", |
65 | | - " ", |
66 | | - "// public class Adapter : ITarget {", |
67 | | - "//\t\tprivate readonly Adaptee _adaptee;", |
68 | | - "//\t\tpublic Adapter(Adaptee adaptee) {", |
69 | | - "//\t\t\tthis._adaptee = adaptee;", |
70 | | - "//\t\t}", |
71 | | - " ", |
72 | | - "//\t\tpublic string GetRequest() {", |
73 | | - "//\t\t\treturn $'This is {this._adaptee.GetSpecificRequest()};'", |
74 | | - "//\t\t}", |
75 | | - "//}" |
| 49 | + "public interface ITarget {", |
| 50 | + "\tstring GetRequest();", |
| 51 | + "}", |
| 52 | + "", |
| 53 | + "public class Adaptee {", |
| 54 | + "\tpublic string GetSpecificRequest() {", |
| 55 | + "\t\treturn 'Specific Request.';", |
| 56 | + "\t}", |
| 57 | + "}", |
| 58 | + "", |
| 59 | + "public class Adapter : ITarget {", |
| 60 | + "\tprivate readonly Adaptee _adaptee;", |
| 61 | + "\tpublic Adapter(Adaptee adaptee) {", |
| 62 | + "\t\tthis._adaptee = adaptee;", |
| 63 | + "\t}", |
| 64 | + "", |
| 65 | + "\tpublic string GetRequest() {", |
| 66 | + "\t\treturn $'This is {this._adaptee.GetSpecificRequest()}';", |
| 67 | + "\t}", |
| 68 | + "}" |
76 | 69 | ], |
77 | 70 | "description": "Structural design pattern adapter" |
78 | 71 | }, |
79 | 72 | "-> Behavioral::Observer": { |
80 | 73 | "prefix": "observer", |
81 | 74 | "body": [ |
82 | | - "// public interface IObserver {", |
83 | | - "//\t\tvoid Update(ISubject subject);", |
84 | | - "//}", |
85 | | - " ", |
86 | | - "// public interface ISubject {", |
87 | | - "//\t\tvoid Attach(IObserver observer);", |
88 | | - "//\t\tvoid Detach(IObserver observer);", |
89 | | - "//\t\tvoid Notify();", |
90 | | - "//}", |
91 | | - " ", |
92 | | - "// public class Subject : ISubject {", |
93 | | - "//\t\tpublic int State { get; set; } = -0;", |
94 | | - "//\t\tprivate List<IObserver> _observers = new List<IObserver>();", |
95 | | - "// public void Attach(IObserver observer) {", |
96 | | - "//\t\tConsole.WriteLine('Subject: Attached an observer.');", |
97 | | - "//\t\tthis._observers.Add(observer);", |
98 | | - "//}", |
99 | | - " ", |
100 | | - "// public void Detach(IObserver observer) {", |
101 | | - "//\t\tthis._observers.Remove(observer);", |
102 | | - "//\t\tConsole.WriteLine('Subject: Detached an observer.');", |
103 | | - "//}", |
104 | | - " ", |
105 | | - "// public void Notify() {", |
106 | | - "//\t\tConsole.WriteLine('Subject: Notifying observers...');", |
107 | | - "//\t\tforeach (var observer in _observers) {", |
108 | | - "//\t\t\tobserver.Update(this);", |
109 | | - "//\t\t}", |
110 | | - "// }", |
111 | | - " ", |
112 | | - "// public void SomeBusinessLogic() {", |
113 | | - "//\t\tConsole.WriteLine('Subject: I'm doing something important.');", |
114 | | - "//\t\tthis.State = new Random().Next(0,10);", |
115 | | - "//\t\tThread.Sleep(15);", |
116 | | - "//\t\tConsole.WriteLine('Subject: My state has just changed to: ' + this.State);", |
117 | | - "//\t\tthis.Notify();", |
118 | | - "// }", |
119 | | - "//}", |
120 | | - " ", |
121 | | - "// class ConcreteObserverA : IObserver {", |
122 | | - "//\t\tpublic void Update(ISubject subject) {", |
123 | | - "//\t\t\tif ((subject as Subject).State < 3) {", |
124 | | - "//\t\t\t\tConsole.WriteLine('ConcreteObserverA: Reacted to the event.');", |
125 | | - "//\t\t\t}", |
126 | | - "//\t\t}", |
127 | | - "// }", |
128 | | - "// class ConcreteObserverB : IObserver {", |
129 | | - "//\t\tpublic void Update(ISubject subject) {", |
130 | | - "//\t\t\tif ((subject as Subject).State == 0 || (subject as Subject).State >= 2) {", |
131 | | - "//\t\t\t\tConsole.WriteLine('ConcreteObserverB: Reacted to the event.');", |
132 | | - "//\t\t\t}", |
133 | | - "//\t\t}", |
134 | | - "// }" |
| 75 | + "public interface IObserver {", |
| 76 | + "\tvoid Update(Subject subject);", |
| 77 | + "}", |
| 78 | + "", |
| 79 | + "public interface ISubject {", |
| 80 | + "\tevent Action<Subject> OnChange;", |
| 81 | + "\tvoid Notify();", |
| 82 | + "}", |
| 83 | + "", |
| 84 | + "public class Subject : ISubject {", |
| 85 | + "\tpublic int State { get; private set; } = -1;", |
| 86 | + "\tpublic event Action<Subject>? OnChange;", |
| 87 | + "", |
| 88 | + "\tpublic void Notify() {", |
| 89 | + "\t\tConsole.WriteLine(\"Subject: Notifying observers...\");", |
| 90 | + "\t\tOnChange?.Invoke(this);", |
| 91 | + "\t}", |
| 92 | + "", |
| 93 | + "\tpublic void SomeBusinessLogic() {", |
| 94 | + "\t\tConsole.WriteLine(\"Subject: I'm doing something important.\");", |
| 95 | + "\t\tState = new Random().Next(0, 10);", |
| 96 | + "\t\tThread.Sleep(15);", |
| 97 | + "\t\tConsole.WriteLine($\"Subject: My state has just changed to: {State}\");", |
| 98 | + "\t\tNotify();", |
| 99 | + "\t}", |
| 100 | + "}", |
| 101 | + "", |
| 102 | + "public class ConcreteObserverA : IObserver {", |
| 103 | + "\tpublic void Update(Subject subject) {", |
| 104 | + "\t\tif (subject.State < 3) {", |
| 105 | + "\t\t\tConsole.WriteLine(\"ConcreteObserverA: Reacted to the event.\");", |
| 106 | + "\t\t}", |
| 107 | + "\t}", |
| 108 | + "}", |
| 109 | + "", |
| 110 | + "public class ConcreteObserverB : IObserver {", |
| 111 | + "\tpublic void Update(Subject subject) {", |
| 112 | + "\t\tif (subject.State == 0 || subject.State >= 2) {", |
| 113 | + "\t\t\tConsole.WriteLine(\"ConcreteObserverB: Reacted to the event.\");", |
| 114 | + "\t\t}", |
| 115 | + "\t}", |
| 116 | + "}" |
135 | 117 | ], |
136 | 118 | "description": "Behavioral design pattern observer" |
137 | 119 | } |
|
0 commit comments