-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenClosedPrinciple.cs
More file actions
194 lines (160 loc) · 5.53 KB
/
Copy pathOpenClosedPrinciple.cs
File metadata and controls
194 lines (160 loc) · 5.53 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace DesignPrinciples
{
public enum Color
{
Red, Green, Blue
}
public enum Size
{
Small,Medium, Large, Huge
}
public class Product
{
//remember it is illegal to have public fields in .Net but for the sake of example these will be public
public string Name;
public Color Color;
public Size Size;
public Product(string name, Color color, Size size)
{
if (name == null)
{
throw new ArgumentNullException(paramName: nameof(name));
}
Name = name;
Color = color;
Size = size;
}
}
//Filter only specific criteria
public class ProductFilter
{
//Filtering by just Size
public IEnumerable<Product> FilterBySize(IEnumerable<Product> products, Size size)
{
foreach (var p in products)
if (p.Size == size)
yield return p;
}
//Filtering by just color
public IEnumerable<Product> FilterByColor(IEnumerable<Product> products, Color color)
{
foreach (var p in products)
if (p.Color == color)
yield return p;
}
//Oh no! Filitering by color and size. Ew. Gross. This violates the Open/Close principle because you want to be able to
//Extend the product filter class but closed for modification so you don't have to keep adding new specifications
public IEnumerable<Product> FilterBySizeAndColor(IEnumerable<Product> products, Size size, Color color)
{
//don't do this see note above and below
foreach (var p in products)
if (p.Size == size && p.Color == color)
yield return p;
}
}
//use inheritance instead! We will also implement a pattern called the Specification pattern
public interface ISpecification<T>
{
//Checks to see if the specification of type t is satisfied
bool IsSatisfied(T t);
}
public interface IFilter<T>
{
//filters based on any type T under the ISpecification
IEnumerable<T> Filter(IEnumerable<T> items, ISpecification<T> spec);
}
public class ColorSpecification: ISpecification<Product>
{
private Color color;
public ColorSpecification(Color color)
{
this.color = color;
}
//here is the inherited member of the ISpecification
public bool IsSatisfied(Product t)
{
return t.Color == color;
}
}
public class SizeSpecification: ISpecification<Product>
{
private Size size;
public SizeSpecification(Size size)
{
this.size = size;
}
public bool IsSatisfied(Product t)
{
return t.Size == size;
}
}
//Now to create a Combinator to check multiple specifications
public class AndSpecification<T>: ISpecification<T>
{
ISpecification<T> first, second;
//Constructor
public AndSpecification(ISpecification <T> first, ISpecification <T> second)
{
this.first = first ?? throw new ArgumentNullException(paramName: nameof(first));
this.second = second ?? throw new ArgumentNullException(paramName: nameof(second));
}
public bool IsSatisfied(T t)
{
return first.IsSatisfied(t) && second.IsSatisfied(t);
}
}
//Now we will implement an even better filter
public class BetterFilter: IFilter<Product>
{
public IEnumerable<Product> Filter(IEnumerable<Product> items, ISpecification<Product> spec)
{
foreach (var i in items)
if(spec.IsSatisfied(i))
yield return i;
}
}
public class OpenClosedPrinciple
{
static void Main(String[] args)
{
var apple = new Product("Apple", Color.Green, Size.Small);
var tree = new Product("Tree", Color.Green, Size.Large);
var house = new Product("House", Color.Blue, Size.Large);
Product[] products = { apple, tree, house };
//Implementing the wrong way to implement filtering
var pf = new ProductFilter();
WriteLine("Green products (old):");
foreach (var p in pf.FilterByColor(products, Color.Green))
{
WriteLine($"- {p.Name} is green");
}
//Implementing the correct way to filter with the Open Closed Principle
var bf = new BetterFilter();
WriteLine("Green products (new):");
foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
{
WriteLine($"- {p.Name} is green");
}
//They both produce identical results
//Below you can see how you can create an iSpecification to filter out multiple requirements
WriteLine("Large Blue items");
foreach (var p in bf.Filter(
products,
new AndSpecification<Product>(
new ColorSpecification(Color.Blue),
new SizeSpecification(Size.Large)
)))
{
WriteLine($" - {p.Name} is big and blue");
}
}
}
}