Skip to content

Commit 5b0ddcc

Browse files
committed
initial commit
0 parents  commit 5b0ddcc

18 files changed

+583
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.vs
2+
bin
3+
obj
4+
Patterns.suo

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Wouter Van Schandevijl
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Patterns.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Patterns", "Patterns\Patterns.csproj", "{DA5C651E-59F1-4207-B9FC-5B13AFD189E6}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{DA5C651E-59F1-4207-B9FC-5B13AFD189E6}.Debug|x86.ActiveCfg = Debug|x86
13+
{DA5C651E-59F1-4207-B9FC-5B13AFD189E6}.Debug|x86.Build.0 = Debug|x86
14+
{DA5C651E-59F1-4207-B9FC-5B13AFD189E6}.Release|x86.ActiveCfg = Release|x86
15+
{DA5C651E-59F1-4207-B9FC-5B13AFD189E6}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace SOLID.OpenClosedPrinciple.Setup
7+
{
8+
public class Rectangle
9+
{
10+
public double Width { get; set; }
11+
public double Height { get; set; }
12+
}
13+
14+
public class AreaCalculator
15+
{
16+
public double Area(Rectangle[] shapes)
17+
{
18+
double area = 0;
19+
foreach (var shape in shapes)
20+
{
21+
area += shape.Width * shape.Height;
22+
}
23+
return area;
24+
}
25+
}
26+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using SOLID.OpenClosedPrinciple.Setup;
6+
7+
namespace SOLID.OpenClosedPrinciple.Violation
8+
{
9+
public class Circle
10+
{
11+
public double Radius { get; set; }
12+
}
13+
14+
public class AdvancedAreaCalculator
15+
{
16+
public double Area(object[] shapes)
17+
{
18+
double area = 0;
19+
foreach (var shape in shapes)
20+
{
21+
if (shape is Rectangle)
22+
{
23+
Rectangle rectangle = (Rectangle)shape;
24+
area += rectangle.Width * rectangle.Height;
25+
}
26+
else
27+
{
28+
Circle circle = (Circle)shape;
29+
area += circle.Radius * circle.Radius * Math.PI;
30+
}
31+
// Fool me once, shame on you. Fool me twice, shame on me.
32+
// Triangles, Elipses, ...
33+
}
34+
35+
return area;
36+
}
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace SOLID.OpenClosedPrinciple.Solution
7+
{
8+
public interface IAreaCalculator
9+
{
10+
double CalculateArea();
11+
}
12+
13+
public class Rectangle : IAreaCalculator
14+
{
15+
public double Width { get; set; }
16+
public double Height { get; set; }
17+
public double CalculateArea()
18+
{
19+
return Width * Height;
20+
}
21+
}
22+
23+
public class Circle : IAreaCalculator
24+
{
25+
public double Radius { get; set; }
26+
public double CalculateArea()
27+
{
28+
return Radius * Radius * Math.PI;
29+
}
30+
}
31+
32+
public class OpenClosedAreaCalculator
33+
{
34+
public double Area(IAreaCalculator[] shapes)
35+
{
36+
double area = 0;
37+
foreach (var shape in shapes)
38+
{
39+
area += shape.CalculateArea();
40+
}
41+
42+
return area;
43+
}
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Patterns.TemplateMethod.Problem
7+
{
8+
class FileSystemUploader
9+
{
10+
public virtual void UploadFile(byte[] fileContent)
11+
{
12+
if (fileContent == null)
13+
throw new ArgumentException("Kan niet null zijn", "fileContent");
14+
15+
// Put file on network share
16+
}
17+
}
18+
19+
class FtpUploader : FileSystemUploader
20+
{
21+
public override void UploadFile(byte[] fileContent)
22+
{
23+
// DRY!
24+
if (fileContent == null)
25+
throw new ArgumentException("Kan niet null zijn", "fileContent");
26+
27+
// Upload the file
28+
}
29+
}
30+
31+
class DropBoxUploader : FileSystemUploader
32+
{
33+
public override void UploadFile(byte[] fileContent)
34+
{
35+
// Forced to call the base!
36+
base.UploadFile(fileContent);
37+
38+
Console.WriteLine("_dropBox.GetPublicShareUrl();");
39+
}
40+
}
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
7+
namespace Patterns.TemplateMethod
8+
{
9+
abstract class UploaderBase
10+
{
11+
public void UploadFile(byte[] fileContent) {
12+
if (fileContent == null)
13+
throw new ArgumentException("Kan niet null zijn", "fileContent");
14+
15+
SetUpHook();
16+
UploadFileCore(fileContent);
17+
BreakDownHook();
18+
}
19+
20+
protected virtual void SetUpHook() { }
21+
22+
protected abstract void UploadFileCore(byte[] fileContent);
23+
24+
protected virtual void BreakDownHook() { }
25+
}
26+
27+
class FtpUploader : UploaderBase
28+
{
29+
protected override void SetUpHook() {
30+
// Connect to FTP server
31+
}
32+
33+
protected override void UploadFileCore(byte[] fileContent) {
34+
// _ftp.PutFreeFile(fileContent);
35+
}
36+
37+
protected override void BreakDownHook() {
38+
// Close FTP connection
39+
}
40+
}
41+
42+
class DropBoxUploader : UploaderBase
43+
{
44+
public const string RootDropBoxFolder = @"c:\dropbox\";
45+
46+
protected override void UploadFileCore(byte[] fileContent) {
47+
File.WriteAllBytes(RootDropBoxFolder + "file.txt", fileContent);
48+
Console.WriteLine("_dropBox.GetPublicShareUrl();");
49+
}
50+
}
51+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
7+
namespace Patterns.FactoryMethod
8+
{
9+
abstract class UploaderBase
10+
{
11+
public UploadResult UploadFile(byte[] fileContent) {
12+
if (fileContent == null)
13+
throw new ArgumentException("Kan niet null zijn", "fileContent");
14+
15+
SetUpHook();
16+
UploadResult result = UploadFileCore(fileContent);
17+
BreakDownHook();
18+
return result;
19+
}
20+
21+
protected virtual void SetUpHook() { }
22+
23+
protected abstract UploadResult UploadFileCore(byte[] fileContent);
24+
25+
protected virtual void BreakDownHook() { }
26+
}
27+
28+
class UploadResult
29+
{
30+
public UploadResult(string location) { Location = location; }
31+
public string Location { private set; get; }
32+
}
33+
34+
class FtpUploader : UploaderBase
35+
{
36+
protected override void SetUpHook() {
37+
// Connect to FTP server
38+
}
39+
40+
protected override UploadResult UploadFileCore(byte[] fileContent) {
41+
string location = ""; // _ftp.PutFreeFile(fileContent);
42+
return new UploadResult(location);
43+
}
44+
45+
protected override void BreakDownHook() {
46+
// Close FTP connection
47+
}
48+
}
49+
50+
class DropBoxUploader : UploaderBase
51+
{
52+
public const string RootDropBoxFolder = @"c:\dropbox\";
53+
54+
protected override UploadResult UploadFileCore(byte[] fileContent) {
55+
string location = RootDropBoxFolder + "file.txt";
56+
File.WriteAllBytes(location, fileContent);
57+
return new DropBoxUploadResult(location, "_dropBox.GetPublicShare(RootDropBoxFolder)");
58+
}
59+
60+
class DropBoxUploadResult : UploadResult
61+
{
62+
public string PublicShareUrl { private set; get; }
63+
public DropBoxUploadResult(string location, string publicShareUrl)
64+
: base(location)
65+
{
66+
PublicShareUrl = publicShareUrl;
67+
}
68+
}
69+
}
70+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
7+
namespace Patterns.Strategy
8+
{
9+
class Uploader
10+
{
11+
public void UploadFiles(params string[] files)
12+
{
13+
if (files == null || files.Length == 0)
14+
throw new ArgumentException("Kan niet null of leeg zijn", "files");
15+
16+
_strategy.SetUpHook();
17+
foreach (string file in files) {
18+
_strategy.UploadFile(File.ReadAllBytes(file));
19+
}
20+
_strategy.BreakDownHook();
21+
}
22+
23+
private UploadStrategy _strategy;
24+
public Uploader(UploadStrategy strategy) {
25+
// Constructor Dependency Injection
26+
_strategy = strategy;
27+
}
28+
}
29+
30+
abstract class UploadStrategy
31+
{
32+
public virtual void SetUpHook() { }
33+
34+
public abstract void UploadFile(byte[] fileContent);
35+
36+
public virtual void BreakDownHook() { }
37+
}
38+
39+
class DropBoxUploader : UploadStrategy
40+
{
41+
public string RootDropBoxFolder { get; private set; }
42+
43+
public DropBoxUploader()
44+
: this(@"c:\dropbox\") {
45+
// Backwards compatibility
46+
}
47+
48+
public DropBoxUploader(string dropBoxPath) {
49+
RootDropBoxFolder = dropBoxPath;
50+
}
51+
52+
public override void UploadFile(byte[] fileContent) {
53+
string location = RootDropBoxFolder + "file.txt";
54+
File.WriteAllBytes(location, fileContent);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)