Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0019068

Browse files
New Design Patterns (Chain of Responsibility, Decorator, Facade)
1 parent b3045b0 commit 0019068

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

‎CSharp Code Samples/CodeSamples/CodeSamples.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<Compile Include="MultiThreading\BackgroundWorkerSample.cs" />
8787
<Compile Include="MultiThreading\MultithreadingSample.cs" />
8888
<Compile Include="MultiThreading\ThreadSample.cs" />
89+
<Compile Include="Patterns\Behavioral\ChainOfResponsibilityPattern.cs" />
8990
<Compile Include="Patterns\Behavioral\ObserverPattern.cs" />
9091
<Compile Include="Patterns\Behavioral\StatePattern.cs" />
9192
<Compile Include="Patterns\Creational\AbstractFactoryPattern.cs" />
@@ -95,6 +96,8 @@
9596
<Compile Include="Patterns\PatternsSample.cs" />
9697
<Compile Include="Patterns\Creational\SingletonPattern.cs" />
9798
<Compile Include="Patterns\Behavioral\StrategyPattern.cs" />
99+
<Compile Include="Patterns\Structural\DecoratorPattern.cs" />
100+
<Compile Include="Patterns\Structural\FacadePattern.cs" />
98101
<Compile Include="Program.cs" />
99102
<Compile Include="Properties\AssemblyInfo.cs" />
100103
<Compile Include="Properties\OtherSettings.Designer.cs">
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CodeSamples.Patterns.Behavioral
8+
{
9+
public class ChainOfResponsibilityPatternSample : SampleExecute
10+
{
11+
internal abstract class Handler
12+
{
13+
protected Handler Successor { get; private set; }
14+
15+
public void SetSuccessor(Handler successor)
16+
{
17+
this.Successor = successor;
18+
}
19+
20+
public abstract void HandleRequest(string deviceName, int request);
21+
}
22+
23+
internal class HandlerDeviceKeyboard : Handler
24+
{
25+
public override void HandleRequest(string deviceName, int request)
26+
{
27+
if (deviceName.Equals("keyboard", StringComparison.OrdinalIgnoreCase))
28+
{
29+
Console.WriteLine($"Handled Request {request} for device {deviceName} by {this.GetType().Name}");
30+
}
31+
else Successor?.HandleRequest(deviceName, request);
32+
}
33+
}
34+
35+
internal class HandlerDeviceMouse : Handler
36+
{
37+
public override void HandleRequest(string deviceName, int request)
38+
{
39+
if (deviceName.Equals("mouse", StringComparison.OrdinalIgnoreCase))
40+
{
41+
Console.WriteLine($"Handled Request {request} for device {deviceName} by {this.GetType().Name}");
42+
}
43+
else Successor?.HandleRequest(deviceName, request);
44+
}
45+
}
46+
47+
internal class HandlerDeviceNetwork : Handler
48+
{
49+
public override void HandleRequest(string deviceName, int request)
50+
{
51+
if (deviceName.Equals("network", StringComparison.OrdinalIgnoreCase))
52+
{
53+
Console.WriteLine($"Handled Request {request} for device {deviceName} by {this.GetType().Name}");
54+
}
55+
else Successor?.HandleRequest(deviceName, request);
56+
}
57+
}
58+
59+
public override void Execute()
60+
{
61+
Section("Chain of Responsibility Pattern");
62+
var keyboard = new HandlerDeviceKeyboard();
63+
var mouse = new HandlerDeviceMouse();
64+
var network = new HandlerDeviceNetwork();
65+
66+
keyboard.SetSuccessor(mouse);
67+
mouse.SetSuccessor(network);
68+
69+
keyboard.HandleRequest("mouse", 10);
70+
keyboard.HandleRequest("network", 12);
71+
keyboard.HandleRequest("keyboard", 102);
72+
keyboard.HandleRequest("something", 11);
73+
}
74+
}
75+
}

‎CSharp Code Samples/CodeSamples/Patterns/PatternsSample.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CodeSamples.Patterns.Behavioral;
22
using CodeSamples.Patterns.Creational;
3+
using CodeSamples.Patterns.Structural;
34

45
namespace CodeSamples.Patterns
56
{
@@ -8,6 +9,7 @@ public class PatternsSample : SampleExecute
89
public override void Execute()
910
{
1011
Title("PatternsSampleExecute");
12+
1113
Section("Creational Patterns");
1214
var singleton = new SingletonPatternSample();
1315
singleton.Execute();
@@ -16,13 +18,24 @@ public override void Execute()
1618
var abstractFactory = new AbstractFactoryPatternSample();
1719
abstractFactory.Execute();
1820
LineBreak();
21+
1922
Section("Behavioral Patterns");
2023
var strategy = new StrategyPatternSample();
2124
strategy.Execute();
2225
var observer = new ObserverPatternSample();
2326
observer.Execute();
2427
var state = new StatePatternSample();
2528
state.Execute();
29+
var chainOfResponsibility = new ChainOfResponsibilityPatternSample();
30+
chainOfResponsibility.Execute();
31+
LineBreak();
32+
33+
Section("Structural Patterns");
34+
var decorator = new DecoratorPatternSample();
35+
decorator.Execute();
36+
var facade = new FacadePatternSample();
37+
facade.Execute();
38+
2639
Finish();
2740
}
2841
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
3+
namespace CodeSamples.Patterns.Structural
4+
{
5+
public class DecoratorPatternSample : SampleExecute
6+
{
7+
8+
internal interface IBird
9+
{
10+
string Fly();
11+
}
12+
13+
internal class Bird : IBird
14+
{
15+
public string Fly()
16+
{
17+
return "Flying";
18+
}
19+
}
20+
21+
//decorator classes
22+
internal class FasterBird : IBird
23+
{
24+
protected readonly IBird _bird;
25+
26+
public FasterBird(IBird bird)
27+
{
28+
_bird = bird;
29+
}
30+
31+
public string Fly()
32+
{
33+
return $"{_bird.Fly()} faster";
34+
}
35+
}
36+
37+
internal class FastestBird : IBird
38+
{
39+
protected readonly IBird _bird;
40+
41+
public FastestBird(IBird bird)
42+
{
43+
_bird = bird;
44+
}
45+
46+
public string Fly()
47+
{
48+
return $"{_bird.Fly()} like tail is on fire";
49+
}
50+
}
51+
52+
public override void Execute()
53+
{
54+
Section("Decorator Pattern");
55+
var bird = new Bird();
56+
Console.WriteLine(bird.Fly());
57+
58+
var fasterBird = new FasterBird(bird);
59+
Console.WriteLine(fasterBird.Fly());
60+
61+
var fastestBird = new FastestBird(fasterBird);
62+
Console.WriteLine(fastestBird.Fly());
63+
}
64+
}
65+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
3+
namespace CodeSamples.Patterns.Structural
4+
{
5+
public class FacadePatternSample : SampleExecute
6+
{
7+
8+
internal class SubSystemOne
9+
{
10+
public void DoSomething()
11+
{
12+
Console.WriteLine("Doing Something");
13+
}
14+
}
15+
16+
internal class SubSystemTwo
17+
{
18+
public void DoSomethingElse()
19+
{
20+
Console.WriteLine("Doing Something Else");
21+
}
22+
}
23+
24+
internal class SubSystemThree
25+
{
26+
public void WriteABook()
27+
{
28+
Console.WriteLine("Writing a book");
29+
}
30+
}
31+
32+
internal class SubSystemFour
33+
{
34+
public void Swim()
35+
{
36+
Console.WriteLine("Swimming");
37+
}
38+
}
39+
40+
internal class FacadeClass
41+
{
42+
private readonly SubSystemOne _one = new SubSystemOne();
43+
private readonly SubSystemTwo _two = new SubSystemTwo();
44+
private readonly SubSystemThree _three = new SubSystemThree();
45+
private readonly SubSystemFour _four = new SubSystemFour();
46+
47+
public void GoLeisureTimeMethodOne()
48+
{
49+
_one.DoSomething();
50+
_three.WriteABook();
51+
_four.Swim();
52+
}
53+
54+
public void GoLeisureTimeAll()
55+
{
56+
_one.DoSomething();
57+
_two.DoSomethingElse();
58+
_three.WriteABook();
59+
_four.Swim();
60+
}
61+
}
62+
63+
public override void Execute()
64+
{
65+
Section("Facade Pattern");
66+
var facade = new FacadeClass();
67+
68+
facade.GoLeisureTimeMethodOne();
69+
Console.WriteLine();
70+
facade.GoLeisureTimeAll();
71+
}
72+
}
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /