Skip to content

Commit 89bd618

Browse files
authored
Fix/23 singleton snippet adjust (#27)
* adjust design pattern snippets with modern approach * adjusted version, changelog and readme
1 parent 2ba0470 commit 89bd618

File tree

5 files changed

+146
-153
lines changed

5 files changed

+146
-153
lines changed

.vscode/settings.json

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
// Place your settings in this file to overwrite default and user settings.
22
{
3-
"files.exclude": {
4-
"out": false // set this to true to hide the "out" folder with the compiled JS files
5-
},
6-
"search.exclude": {
7-
"out": true // set this to false to include "out" folder in search results
8-
},
9-
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
10-
"typescript.tsc.autoDetect": "off",
11-
"cSpell.ignoreWords": [
12-
"backreference",
13-
"documentxml",
14-
"extention",
15-
"group",
16-
"json",
17-
"multiline",
18-
"snippets",
19-
"strikethrough",
20-
"to",
21-
"zampieri"
22-
],
23-
"cSpell.words": [
24-
"Blazor",
25-
"blazorserver",
26-
"blazorwasm",
27-
"classlib",
28-
"designpattern",
29-
"forminline",
30-
"minwebapi",
31-
"mstest",
32-
"netcoreapp",
33-
"nunit",
34-
"paddding",
35-
"razorclasslib",
36-
"reactredux",
37-
"readlines",
38-
"richardzampieriprog",
39-
"submenu",
40-
"webapi",
41-
"xunit"
42-
]
3+
"files.exclude": {
4+
"out": false // set this to true to hide the "out" folder with the compiled JS files
5+
},
6+
"search.exclude": {
7+
"out": true // set this to false to include "out" folder in search results
8+
},
9+
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
10+
"typescript.tsc.autoDetect": "off",
11+
"cSpell.ignoreWords": [
12+
"backreference",
13+
"documentxml",
14+
"extention",
15+
"group",
16+
"json",
17+
"multiline",
18+
"snippets",
19+
"strikethrough",
20+
"to",
21+
"zampieri"
22+
],
23+
"cSpell.words": [
24+
"Blazor",
25+
"blazorserver",
26+
"blazorwasm",
27+
"classlib",
28+
"Creational",
29+
"designpattern",
30+
"forminline",
31+
"minwebapi",
32+
"mstest",
33+
"netcoreapp",
34+
"nunit",
35+
"paddding",
36+
"razorclasslib",
37+
"reactredux",
38+
"readlines",
39+
"richardzampieriprog",
40+
"submenu",
41+
"webapi",
42+
"xunit"
43+
]
4344
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
55

66
## [Released]
77

8+
## [2.0.1] - [2024-01-14]
9+
10+
## What's new in 2.0.1
11+
12+
> - **_Fix_**: Fixed issues related to design patterns snippets. Added a more modern code approach to the snippets.
13+
814
## [2.0.0] - [2024-01-14]
915

1016
## What's new in 2.0.0

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ C# Snippet Productivity
2626

2727
> [Click here](https://github.com/rsaz/csharp-snippet-productivity/blob/main/CHANGELOG.md)
2828
29+
## What's new in 2.0.1
30+
31+
> - **_Fix_**: Fixed issues related to design patterns snippets. Added a more modern code approach to the snippets.
32+
2933
## What's new in 2.0.0
3034

3135
> - **_All Project Types_**: Added support for all project types and templates under project creation.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "csharp-snippet-productivity",
33
"displayName": "C# Toolbox of Productivity",
44
"description": "The complete set of tools for C# development",
5-
"version": "2.0.0",
5+
"version": "2.0.1",
66
"icon": "icon.png",
77
"publisher": "richardzampieriprog",
88
"license": "SEE LICENSE IN LICENSE.md",

snippets/designpattern.json

Lines changed: 94 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,118 @@
22
"-> Creational::Singleton": {
33
"prefix": "singleton",
44
"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+
"}"
1310
],
1411
"description": "Creational design pattern singleton"
1512
},
1613
"-> Creational::Factory Method": {
1714
"prefix": "factoryMethod",
1815
"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+
"}"
5043
],
5144
"description": "Creational design pattern factory method"
5245
},
5346
"-> Structural::Adapter": {
5447
"prefix": "adapter",
5548
"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+
"}"
7669
],
7770
"description": "Structural design pattern adapter"
7871
},
7972
"-> Behavioral::Observer": {
8073
"prefix": "observer",
8174
"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+
"}"
135117
],
136118
"description": "Behavioral design pattern observer"
137119
}

0 commit comments

Comments
 (0)