├── .gitignore
├── DesignPatterns.sln
├── DesignPatterns
├── DesignPatterns.csproj
├── GangOfFour
│ ├── Behavioral
│ │ ├── ChainOfResponsibility
│ │ │ ├── ConcreteHandler1.cs
│ │ │ ├── ConcreteHandler2.cs
│ │ │ ├── ConcreteHandler3.cs
│ │ │ ├── Handler.cs
│ │ │ └── Tests
│ │ │ │ └── ChainOfResponsibilityTest.cs
│ │ ├── Command
│ │ │ ├── CloseSwitchCommand.cs
│ │ │ ├── ICommand.cs
│ │ │ ├── ISwitchable.cs
│ │ │ ├── Light.cs
│ │ │ ├── OpenSwitchCommand .cs
│ │ │ ├── Switch.cs
│ │ │ └── Tests
│ │ │ │ └── CommandTest.cs
│ │ ├── Interpreter
│ │ │ ├── AddExpression.cs
│ │ │ ├── IExpression.cs
│ │ │ ├── NumberExpression.cs
│ │ │ ├── SubtractExpression.cs
│ │ │ ├── Tests
│ │ │ │ └── InterpreterTest.cs
│ │ │ └── TokenReader.cs
│ │ ├── Iterator
│ │ │ ├── Collection.cs
│ │ │ ├── ICollection.cs
│ │ │ ├── IIterator.cs
│ │ │ ├── Iterator.cs
│ │ │ └── Tests
│ │ │ │ └── IteratorTest.cs
│ │ ├── Mediator
│ │ │ ├── Colleague.cs
│ │ │ ├── IColleague.cs
│ │ │ ├── IMediator.cs
│ │ │ ├── Mediator.cs
│ │ │ └── Tests
│ │ │ │ └── MediatorTest.cs
│ │ ├── Memento
│ │ │ ├── CareTaker.cs
│ │ │ ├── Memento.cs
│ │ │ ├── Originator.cs
│ │ │ └── Tests
│ │ │ │ └── MementoTest.cs
│ │ ├── Observer
│ │ │ ├── IObserver.cs
│ │ │ ├── ISubject.cs
│ │ │ ├── Observer.cs
│ │ │ ├── Subject.cs
│ │ │ └── Tests
│ │ │ │ └── ObserverTest.cs
│ │ ├── State
│ │ │ ├── GreenLight.cs
│ │ │ ├── ITrafficLightState.cs
│ │ │ ├── RedLight.cs
│ │ │ ├── RedYellowLight.cs
│ │ │ ├── Tests
│ │ │ │ └── StateTest.cs
│ │ │ ├── TrafficLight.cs
│ │ │ └── YellowLight.cs
│ │ ├── Strategy
│ │ │ ├── ISortStrategy.cs
│ │ │ ├── MergeSort.cs
│ │ │ ├── QuickSort.cs
│ │ │ ├── ShellSort.cs
│ │ │ ├── SortedList.cs
│ │ │ └── Tests
│ │ │ │ └── StrategyTest.cs
│ │ ├── TemplateMethod
│ │ │ ├── DataExporter.cs
│ │ │ ├── ExcelExporter.cs
│ │ │ ├── PdfExporter.cs
│ │ │ └── Tests
│ │ │ │ └── TemplateMethodTest.cs
│ │ └── Visitor
│ │ │ ├── BoldText.cs
│ │ │ ├── Document.cs
│ │ │ ├── DocumentPart.cs
│ │ │ ├── HtmlVisitor.cs
│ │ │ ├── Hyperlink.cs
│ │ │ ├── IVisitor.cs
│ │ │ ├── LatexVisitor.cs
│ │ │ ├── PlainText.cs
│ │ │ └── Tests
│ │ │ └── VisitorTest.cs
│ ├── Creational
│ │ ├── AbstractFactory
│ │ │ ├── IButton.cs
│ │ │ ├── IContextMenu.cs
│ │ │ ├── IGuiFactory.cs
│ │ │ ├── IWindow.cs
│ │ │ ├── OSX
│ │ │ │ ├── Button.cs
│ │ │ │ ├── ContextMenu.cs
│ │ │ │ ├── GuiFactory.cs
│ │ │ │ └── Window.cs
│ │ │ ├── Tests
│ │ │ │ └── AbstractFactoryTest.cs
│ │ │ └── Windows
│ │ │ │ ├── Button.cs
│ │ │ │ ├── ContextMenu.cs
│ │ │ │ ├── GuiFactory.cs
│ │ │ │ └── Window.cs
│ │ ├── Builder
│ │ │ ├── AudiA8CarBuilder.cs
│ │ │ ├── Car.cs
│ │ │ ├── CarBuilderDirector.cs
│ │ │ ├── ICar.cs
│ │ │ ├── ICarBuilder.cs
│ │ │ └── Tests
│ │ │ │ └── BuilderTest.cs
│ │ ├── FactoryMethod
│ │ │ ├── Audi.cs
│ │ │ ├── AudiFactory.cs
│ │ │ ├── BMWFactory.cs
│ │ │ ├── Bmw.cs
│ │ │ ├── ICar.cs
│ │ │ ├── ICarFactory.cs
│ │ │ └── Tests
│ │ │ │ └── FactoryMethodTest.cs
│ │ ├── Prototype
│ │ │ ├── ConcretePrototype1.cs
│ │ │ ├── ConcretePrototype2.cs
│ │ │ ├── Prototype.cs
│ │ │ └── Tests
│ │ │ │ └── PrototypeTest.cs
│ │ └── Singleton
│ │ │ ├── Singleton.cs
│ │ │ ├── Tests
│ │ │ ├── SingletonTest.cs
│ │ │ └── ThreadSafeSingletonTest.cs
│ │ │ └── ThreadSafeSingleton.cs
│ └── Structural
│ │ ├── Adapter
│ │ ├── Book.cs
│ │ ├── EBookAdapter.cs
│ │ ├── IEBook.cs
│ │ ├── IPaperBook.cs
│ │ ├── Kindle.cs
│ │ └── Tests
│ │ │ └── AdapterTest.cs
│ │ ├── Bridge
│ │ ├── Circle.cs
│ │ ├── DrawingApi1.cs
│ │ ├── DrawingApi2.cs
│ │ ├── IDrawingApi.cs
│ │ ├── IShape.cs
│ │ └── Tests
│ │ │ └── BridgeTest.cs
│ │ ├── Composite
│ │ ├── Form.cs
│ │ ├── IFormElement.cs
│ │ ├── InputElement.cs
│ │ ├── Tests
│ │ │ └── CompositeTest.cs
│ │ └── TextElement.cs
│ │ ├── Decorator
│ │ ├── HorizontalScrollBarDecorator .cs
│ │ ├── IWindow.cs
│ │ ├── SimpleWindow.cs
│ │ ├── Tests
│ │ │ └── DecoratorTest.cs
│ │ ├── VerticalScrollBarDecorator.cs
│ │ └── WindowDecorator.cs
│ │ ├── Facade
│ │ ├── Computer.cs
│ │ ├── IBios.cs
│ │ ├── IOs.cs
│ │ └── Tests
│ │ │ └── FacadeTest.cs
│ │ ├── Flyweight
│ │ ├── Circle.cs
│ │ ├── CircleFactory.cs
│ │ ├── IShape.cs
│ │ └── Tests
│ │ │ └── FlyweightTest.cs
│ │ └── Proxy
│ │ ├── Car.cs
│ │ ├── CarProxy.cs
│ │ ├── Driver.cs
│ │ ├── ICar.cs
│ │ └── Tests
│ │ └── ProxyTest.cs
├── Other
│ ├── Behavioral
│ │ ├── NullObject
│ │ │ ├── ConsoleLogger.cs
│ │ │ ├── ILogger.cs
│ │ │ ├── NullLogger.cs
│ │ │ ├── Service.cs
│ │ │ └── Tests
│ │ │ │ └── NullLoggerTest.cs
│ │ └── Specification
│ │ │ ├── Base
│ │ │ ├── AndNotSpecification.cs
│ │ │ ├── AndSpecification.cs
│ │ │ ├── CompositeSpecification.cs
│ │ │ ├── ISpecification.cs
│ │ │ ├── NotSpecification.cs
│ │ │ ├── OrNotSpecification.cs
│ │ │ └── OrSpecification.cs
│ │ │ ├── InCollectionSpecification.cs
│ │ │ ├── Invoice.cs
│ │ │ ├── NoticeSentSpecification.cs
│ │ │ ├── OverDueSpecification.cs
│ │ │ └── Tests
│ │ │ └── SpecificationTest.cs
│ └── Creational
│ │ ├── ObjectPool
│ │ ├── Pool.cs
│ │ ├── PooledObject.cs
│ │ └── Tests
│ │ │ └── ObjectPoolTest.cs
│ │ └── SimpleFactory
│ │ ├── Audi.cs
│ │ ├── Bmw.cs
│ │ ├── CarFactory.cs
│ │ ├── ICar.cs
│ │ └── Tests
│ │ └── SimpleFactoryTest.cs
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | ################################################################################
2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3 | ################################################################################
4 |
5 | /obj/Debug
6 | /.vs/DesignPatterns/v14
7 | /packages/NUnit.3.2.1
8 | /bin/Debug
9 | /Tests/obj/Debug
10 | /Tests/bin/Debug
11 | /DesignPatterns/obj/Debug
12 | /DesignPatterns/bin/Debug
13 | /packages/Moq.4.2.1510.2205/lib
14 | /packages/Moq.4.2.1510.2205
15 |
--------------------------------------------------------------------------------
/DesignPatterns.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.24720.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns", "DesignPatterns\DesignPatterns.csproj", "{B75365E6-B18A-4B81-957E-0863DB236D69}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {B75365E6-B18A-4B81-957E-0863DB236D69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {B75365E6-B18A-4B81-957E-0863DB236D69}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {B75365E6-B18A-4B81-957E-0863DB236D69}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {B75365E6-B18A-4B81-957E-0863DB236D69}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/DesignPatterns/DesignPatterns.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B75365E6-B18A-4B81-957E-0863DB236D69}
8 | Library
9 | Properties
10 | DesignPatterns
11 | DesignPatterns
12 | v4.6.1
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 | ..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll
35 | True
36 |
37 |
38 | ..\packages\NUnit.3.2.1\lib\net45\nunit.framework.dll
39 | True
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 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
226 |
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/ChainOfResponsibility/ConcreteHandler1.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility
2 | {
3 | using System;
4 |
5 | public class ConcreteHandler1 : Handler
6 | {
7 | public override void HandleRequest(int request)
8 | {
9 | if (request >= 0 && request < 10)
10 | {
11 | Console.WriteLine("{0} handled request: {1}.", this.GetType().Name, request);
12 | }
13 | else if (this.Successor != null)
14 | {
15 | this.Successor.HandleRequest(request);
16 | }
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/ChainOfResponsibility/ConcreteHandler2.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility
2 | {
3 | using System;
4 |
5 | public class ConcreteHandler2 : Handler
6 | {
7 | public override void HandleRequest(int request)
8 | {
9 | if (request >= 10 && request < 20)
10 | {
11 | Console.WriteLine("{0} handled request: {1}.", this.GetType().Name, request);
12 | }
13 | else if (this.Successor != null)
14 | {
15 | this.Successor.HandleRequest(request);
16 | }
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/ChainOfResponsibility/ConcreteHandler3.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility
2 | {
3 | using System;
4 |
5 | public class ConcreteHandler3 : Handler
6 | {
7 | public override void HandleRequest(int request)
8 | {
9 | if (request >= 20 && request < 30)
10 | {
11 | Console.WriteLine("{0} handled request: {1}.", this.GetType().Name, request);
12 | }
13 | else if (this.Successor != null)
14 | {
15 | this.Successor.HandleRequest(request);
16 | }
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/ChainOfResponsibility/Handler.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility
2 | {
3 | public abstract class Handler
4 | {
5 | public Handler Successor { get; set; }
6 |
7 | public abstract void HandleRequest(int request);
8 | }
9 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/ChainOfResponsibility/Tests/ChainOfResponsibilityTest.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility.Tests
2 | {
3 | using DesignPatterns.GangOfFour.Behavioral.ChainOfResponsibility;
4 |
5 | using NUnit.Framework;
6 |
7 | [TestFixture]
8 | public class ChainOfResponsibilityTest
9 | {
10 | [TestCase(1)]
11 | [TestCase(12)]
12 | [TestCase(23)]
13 | public void TestChainOfResponsibility(int request)
14 | {
15 | var handler1 = new ConcreteHandler1();
16 | var handler2 = new ConcreteHandler2();
17 | var handler3 = new ConcreteHandler3();
18 |
19 | handler1.Successor = handler2;
20 | handler2.Successor = handler3;
21 |
22 | handler1.HandleRequest(request);
23 |
24 | // OUTPUT: ConcreteHandler1 handled request: 1.
25 |
26 | // OUTPUT: ConcreteHandler2 handled request: 12.
27 |
28 | // OUTPUT: ConcreteHandler3 handled request: 23.
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/CloseSwitchCommand.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | public class CloseSwitchCommand : ICommand
4 | {
5 | private readonly ISwitchable switchable;
6 |
7 | public CloseSwitchCommand(ISwitchable switchable)
8 | {
9 | this.switchable = switchable;
10 | }
11 |
12 | public void Execute()
13 | {
14 | this.switchable.PowerOff();
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/ICommand.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | public interface ICommand
4 | {
5 | void Execute();
6 | }
7 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/ISwitchable.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | public interface ISwitchable
4 | {
5 | void PowerOff();
6 |
7 | void PowerOn();
8 | }
9 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/Light.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | using System;
4 |
5 | public class Light : ISwitchable
6 | {
7 | public void PowerOff()
8 | {
9 | Console.WriteLine("The light is off.");
10 | }
11 |
12 | public void PowerOn()
13 | {
14 | Console.WriteLine("The light is on.");
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/OpenSwitchCommand .cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | public class OpenSwitchCommand : ICommand
4 | {
5 | private readonly ISwitchable switchable;
6 |
7 | public OpenSwitchCommand(ISwitchable switchable)
8 | {
9 | this.switchable = switchable;
10 | }
11 |
12 | public void Execute()
13 | {
14 | this.switchable.PowerOn();
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/Switch.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command
2 | {
3 | public class Switch
4 | {
5 | private readonly ICommand closeCommand;
6 |
7 | private readonly ICommand openCommand;
8 |
9 | public Switch(ICommand closeCommand, ICommand openCommand)
10 | {
11 | this.closeCommand = closeCommand;
12 | this.openCommand = openCommand;
13 | }
14 |
15 | public void Close()
16 | {
17 | this.closeCommand.Execute();
18 | }
19 |
20 | public void Open()
21 | {
22 | this.openCommand.Execute();
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Command/Tests/CommandTest.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Command.Tests
2 | {
3 | using DesignPatterns.GangOfFour.Behavioral.Command;
4 |
5 | using NUnit.Framework;
6 |
7 | [TestFixture]
8 | public class CommandTest
9 | {
10 | [Test]
11 | public void TestCommand()
12 | {
13 | var lamp = new Light();
14 | var closeCommand = new CloseSwitchCommand(lamp);
15 | var openCommand = new OpenSwitchCommand(lamp);
16 | var @switch = new Switch(closeCommand, openCommand);
17 |
18 | @switch.Open();
19 |
20 | // OUTPUT: The light is on.
21 |
22 | @switch.Close();
23 |
24 | // OUTPUT: The light is off.
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/AddExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter
2 | {
3 | public class AddExpression : IExpression
4 | {
5 | private readonly IExpression leftExpression;
6 |
7 | private readonly IExpression rightExpression;
8 |
9 | public AddExpression(IExpression leftExpression, IExpression rightExpression)
10 | {
11 | this.leftExpression = leftExpression;
12 | this.rightExpression = rightExpression;
13 | }
14 |
15 | public int Interpret()
16 | {
17 | return this.leftExpression.Interpret() + this.rightExpression.Interpret();
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/IExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter
2 | {
3 | public interface IExpression
4 | {
5 | int Interpret();
6 | }
7 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/NumberExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter
2 | {
3 | public class NumberExpression : IExpression
4 | {
5 | private readonly int number;
6 |
7 | public NumberExpression(int number)
8 | {
9 | this.number = number;
10 | }
11 |
12 | public int Interpret()
13 | {
14 | return this.number;
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/SubtractExpression.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter
2 | {
3 | public class SubtractExpression : IExpression
4 | {
5 | private readonly IExpression leftExpression;
6 |
7 | private readonly IExpression rightExpression;
8 |
9 | public SubtractExpression(IExpression leftExpression, IExpression rightExpression)
10 | {
11 | this.leftExpression = leftExpression;
12 | this.rightExpression = rightExpression;
13 | }
14 |
15 | public int Interpret()
16 | {
17 | return this.leftExpression.Interpret() - this.rightExpression.Interpret();
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/Tests/InterpreterTest.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter.Tests
2 | {
3 | using System.Collections.Generic;
4 |
5 | using DesignPatterns.GangOfFour.Behavioral.Interpreter;
6 |
7 | using NUnit.Framework;
8 |
9 | [TestFixture]
10 | public class InterpreterTest
11 | {
12 | [TestCase("+ 10 2", 12)]
13 | [TestCase("+ - 10 2 3", 11)]
14 | [TestCase("- + 10 5 - 8 2", 9)]
15 | public void TestInterpreter(string tokenString, int expectedResult)
16 | {
17 | var tokenList = new List(tokenString.Split(' '));
18 | var expression = new TokenReader().ReadToken(tokenList);
19 |
20 | Assert.That(expression.Interpret(), Is.EqualTo(expectedResult));
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Interpreter/TokenReader.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Interpreter
2 | {
3 | using System.Collections.Generic;
4 |
5 | public class TokenReader
6 | {
7 | public IExpression ReadToken(List tokenList)
8 | {
9 | return this.ReadNextToken(tokenList);
10 | }
11 |
12 | private IExpression ReadNextToken(List tokenList)
13 | {
14 | int i;
15 |
16 | if (int.TryParse(tokenList[0], out i))
17 | {
18 | tokenList.RemoveAt(0);
19 |
20 | return new NumberExpression(i);
21 | }
22 |
23 | return this.ReadNonTerminal(tokenList);
24 | }
25 |
26 | private IExpression ReadNonTerminal(List tokenList)
27 | {
28 | var token = tokenList[0];
29 | tokenList.RemoveAt(0);
30 |
31 | var left = this.ReadNextToken(tokenList);
32 | var right = this.ReadNextToken(tokenList);
33 |
34 | switch (token)
35 | {
36 | case "+":
37 | return new AddExpression(left, right);
38 | case "-":
39 | return new SubtractExpression(left, right);
40 | default:
41 | return null;
42 | }
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/DesignPatterns/GangOfFour/Behavioral/Iterator/Collection.cs:
--------------------------------------------------------------------------------
1 | namespace DesignPatterns.GangOfFour.Behavioral.Iterator
2 | {
3 | using System.Collections.Generic;
4 |
5 | public class Collection : ICollection
6 | {
7 | private readonly List