├── .github └── FUNDING.yml ├── .gitignore ├── Behavioral ├── ChainOfResponsibility │ ├── ChainOfResponsibility.csproj │ ├── Client.cs │ ├── ConcreteHandler1.cs │ ├── ConcreteHandler2.cs │ ├── Handler.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Request.cs ├── Command │ ├── Client.cs │ ├── Command.csproj │ ├── ConcreteCommand.cs │ ├── ICommand.cs │ ├── Invoker.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Receiver.cs ├── Interpreter │ ├── Client.cs │ ├── Evaluator.cs │ ├── IExpression.cs │ ├── Interpreter.csproj │ ├── Minus.cs │ ├── Number.cs │ ├── Plus.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Variable.cs ├── Iterator │ ├── Client.cs │ ├── ConcreateAggregate.cs │ ├── ConcreteIterator.cs │ ├── IAggregate.cs │ ├── IIterator.cs │ ├── Iterator.csproj │ └── Properties │ │ └── AssemblyInfo.cs ├── Mediator │ ├── BookButton.cs │ ├── DisplayLabel.cs │ ├── IColleague.cs │ ├── IMediator.cs │ ├── Mediator.cs │ ├── Mediator.csproj │ ├── MediatorDemo.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── SearchButton.cs │ └── ViewButton.cs ├── Memento │ ├── CareTaker.cs │ ├── Memento.csproj │ ├── Originator.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Observer │ ├── Client.cs │ ├── ConcreteObserver.cs │ ├── ConcreteSubject.cs │ ├── IObserver.cs │ ├── ISubject.cs │ ├── Observer.csproj │ └── Properties │ │ └── AssemblyInfo.cs ├── State │ ├── Client.cs │ ├── IState.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── State.csproj │ ├── StateA.cs │ ├── StateB.cs │ └── StateContext.cs ├── Strategy │ ├── AddStrategy.cs │ ├── Client.cs │ ├── Context.cs │ ├── IStrategy.cs │ ├── MultiplyStrategy.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Strategy.csproj │ └── SubstractStrategy.cs ├── TemplateMethod │ ├── Chess.cs │ ├── Client.cs │ ├── Game.cs │ ├── Monopoly.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── TemplateMethod.csproj └── Visitor │ ├── Body.cs │ ├── Car.cs │ ├── CarElementDoVisitor.cs │ ├── CarElementPrintVisitor.cs │ ├── Engine.cs │ ├── ICarElement.cs │ ├── ICarElementVisitor.cs │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Visitor.csproj │ └── Wheel.cs ├── Creational ├── AbstractFactory │ ├── AbstractFactory.csproj │ ├── Application.cs │ ├── ApplicationRunner.cs │ ├── IButton.cs │ ├── IGuiFactory.cs │ ├── OsxButton.cs │ ├── OsxFactory.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── WinButton.cs │ └── WinFactory.cs ├── Builder │ ├── Builder.csproj │ ├── Cook.cs │ ├── HawaiianPizzaBuilder.cs │ ├── Pizza.cs │ ├── PizzaBuilder.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── SpicyPizzaBuilder.cs ├── FactoryMethod │ ├── FactoryMethod.csproj │ ├── MagicMazeGame.cs │ ├── MagicRoom.cs │ ├── MazeGame.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Room.cs ├── NullObject │ ├── AbstractBook.cs │ ├── Book.cs │ ├── BookFactory.cs │ ├── NullBook.cs │ ├── NullObject.csproj │ ├── Program.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── ObjectPool │ ├── ObjectPool.csproj │ ├── Pool.cs │ ├── PooledObject.cs │ ├── Program.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Prototype │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Prototype.cs │ ├── Prototype.csproj │ └── PrototypeImpl.cs └── Singleton │ ├── DoublecheckSingleton.cs │ ├── LazySingleton.cs │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Singleton.csproj ├── DesignPatterns.sln ├── README.md └── Structural ├── Adapter ├── Adaptee.cs ├── Adapter.cs ├── Adapter.csproj ├── ITarget.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Bridge ├── Bridge.csproj ├── CircleShape.cs ├── DrawingApi1.cs ├── DrawingApi2.cs ├── IDrawingApi.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── Shape.cs ├── Composite ├── Composite.csproj ├── CompositeGraphic.cs ├── Ellipse.cs ├── IGraphic.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Decorator ├── Decorator.csproj ├── HorizontalScrollbarWindow.cs ├── IWindow.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── SimpleWindow.cs ├── VerticalScrollbarWindow.cs └── WindowDecorator.cs ├── Facade ├── Computer.cs ├── Cpu.cs ├── Facade.csproj ├── HardDrive.cs ├── Memory.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Flyweight ├── CoffeeFlavor.cs ├── CoffeeFlavorFactory.cs ├── CoffeeOrderContext.cs ├── Flyweight.csproj ├── ICoffeeOrder.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Proxy ├── IImage.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Proxy.csproj ├── ProxyImage.cs └── RealImage.cs └── Specification ├── AndSpecification.cs ├── CompositSpecification.cs ├── ExpressionSpecification.cs ├── ISpecification.cs ├── NotSpecification.cs ├── OrSpecification.cs ├── Program.cs ├── Properties └── AssemblyInfo.cs └── Specification.csproj /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['paypal.me/beginor'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.suo 2 | *.manifest 3 | *.user 4 | *.pidb 5 | *.DS_Store 6 | *.userprefs 7 | [Tt]est[Rr]esults 8 | [Oo]bj/ 9 | [Bb]in/ 10 | [Cc]lient[Bb]in/ 11 | .idea -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/ChainOfResponsibility.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0} 9 | Exe 10 | Properties 11 | ChainOfResponsibility 12 | ChainOfResponsibility 13 | Client 14 | 512 15 | 16 | 17 | ChainOfResponsibility.Client 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ChainOfResponsibility { 7 | 8 | class Client { 9 | 10 | static void Main(string[] args) { 11 | Handler handlerChain = new ConcreteHandler1(new ConcreteHandler2(null)); 12 | 13 | Request request1 = new ConcreteRequest1(); 14 | handlerChain.HandleRequest(request1); 15 | 16 | Request request2 = new ConcreteRequest2(); 17 | handlerChain.HandleRequest(request2); 18 | 19 | Console.ReadKey(); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/ConcreteHandler1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibility { 4 | 5 | public class ConcreteHandler1 : Handler { 6 | 7 | public ConcreteHandler1(Handler successor) : base(successor) { 8 | } 9 | 10 | public override void HandleRequest(Request request) { 11 | if (request is ConcreteRequest1) { 12 | Console.WriteLine("ConcreteRequest1 is handled by ConcreteHandler1"); 13 | } 14 | else { 15 | this.Successor.HandleRequest(request); 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/ConcreteHandler2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibility { 4 | 5 | public class ConcreteHandler2 : Handler { 6 | 7 | public ConcreteHandler2(Handler successor) : base(successor) { 8 | } 9 | 10 | public override void HandleRequest(Request request) { 11 | if (request is ConcreteRequest2) { 12 | Console.WriteLine("ConcreteRequest2 is handled by ConcreteHandler2"); 13 | } 14 | else { 15 | this.Successor.HandleRequest(request); 16 | } 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/Handler.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibility { 2 | 3 | public abstract class Handler { 4 | 5 | private readonly Handler _successor; 6 | 7 | protected Handler(Handler successor) { 8 | this._successor = successor; 9 | } 10 | 11 | protected Handler Successor { 12 | get { 13 | return this._successor; 14 | } 15 | } 16 | 17 | public abstract void HandleRequest(Request request); 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ChainOfResponsibility")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ChainOfResponsibility")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9e6bee19-8a0a-4b35-9b34-29556b0be99a")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/ChainOfResponsibility/Request.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibility { 2 | 3 | public abstract class Request { 4 | } 5 | 6 | public class ConcreteRequest1 : Request { 7 | } 8 | 9 | public class ConcreteRequest2 : Request { 10 | } 11 | } -------------------------------------------------------------------------------- /Behavioral/Command/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Command { 4 | 5 | class Client { 6 | 7 | static void Main(string[] args) { 8 | 9 | var invoker = new Invoker(); 10 | 11 | var receiver = new Receiver(); 12 | ICommand command = new ConcreteCommand(receiver); 13 | 14 | invoker.InvokeCommand(command); 15 | 16 | Console.ReadKey(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Behavioral/Command/Command.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {65459173-442A-4E0D-B758-3B9EDCE54D75} 9 | Exe 10 | Properties 11 | Command 12 | Command 13 | Client 14 | 512 15 | 16 | 17 | Command.Client 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /Behavioral/Command/ConcreteCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Command { 2 | 3 | public class ConcreteCommand : ICommand { 4 | 5 | private readonly Receiver _receiver; 6 | private object _state; 7 | 8 | public ConcreteCommand(Receiver receiver) { 9 | this._receiver = receiver; 10 | } 11 | 12 | public void Execute() { 13 | this._receiver.Action(); 14 | } 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /Behavioral/Command/ICommand.cs: -------------------------------------------------------------------------------- 1 | namespace Command { 2 | 3 | public interface ICommand { 4 | 5 | void Execute(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Behavioral/Command/Invoker.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Command { 4 | 5 | public class Invoker { 6 | 7 | private readonly IList _commandHistory = new List(); 8 | 9 | public void InvokeCommand(ICommand command) { 10 | command.Execute(); 11 | this._commandHistory.Add(command); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Behavioral/Command/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Command")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Command")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9cba8560-470e-4a0d-8ab6-5d58de813207")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Command/Receiver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Command { 4 | 5 | public class Receiver { 6 | 7 | public void Action() { 8 | Console.WriteLine("Receiver Action."); 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Interpreter { 5 | 6 | class Client { 7 | 8 | static void Main(string[] args) { 9 | const string expression = "w x z - +"; 10 | var evaluator = new Evaluator(expression); 11 | var sentence = new Dictionary(); 12 | sentence["w"] = new Number(5); 13 | sentence["x"] = new Number(10); 14 | sentence["z"] = new Number(42); 15 | var result = evaluator.Interpret(sentence); 16 | Console.WriteLine(result); 17 | 18 | Console.ReadKey(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Behavioral/Interpreter/Evaluator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public class Evaluator : IExpression { 6 | 7 | private readonly IExpression _expressionTree; 8 | 9 | public Evaluator(string expression) { 10 | var stack = new Stack(); 11 | foreach (var token in expression.Split(' ')) { 12 | if (token == "+") { 13 | stack.Push(new Plus(stack.Pop(), stack.Pop())); 14 | } 15 | else if (token == "-") { 16 | var right = stack.Pop(); 17 | var left = stack.Pop(); 18 | stack.Push(new Minus(left, right)); 19 | } 20 | else { 21 | stack.Push(new Variable(token)); 22 | } 23 | } 24 | this._expressionTree = stack.Pop(); 25 | } 26 | 27 | public int Interpret(Dictionary context) { 28 | return this._expressionTree.Interpret(context); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/IExpression.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public interface IExpression { 6 | 7 | int Interpret(Dictionary context); 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/Interpreter.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {6007D4E1-1676-4FBC-816D-1B19F6829F22} 9 | Exe 10 | Properties 11 | Interpreter 12 | Interpreter 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 53 | -------------------------------------------------------------------------------- /Behavioral/Interpreter/Minus.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public class Minus : IExpression { 6 | 7 | private readonly IExpression _leftOperand; 8 | private readonly IExpression _rightOperand; 9 | 10 | public Minus(IExpression leftOperand, IExpression rightOperand) { 11 | this._leftOperand = leftOperand; 12 | this._rightOperand = rightOperand; 13 | } 14 | 15 | public int Interpret(Dictionary context) { 16 | return this._leftOperand.Interpret(context) - this._rightOperand.Interpret(context); 17 | } 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/Number.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public class Number : IExpression { 6 | 7 | private readonly int _number; 8 | 9 | public Number(int number) { 10 | this._number = number; 11 | } 12 | 13 | public int Interpret(Dictionary context) { 14 | return this._number; 15 | } 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/Plus.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public class Plus : IExpression { 6 | 7 | private readonly IExpression _leftOperand; 8 | private readonly IExpression _rightOperand; 9 | 10 | public Plus(IExpression leftOperand, IExpression rightOperand) { 11 | this._leftOperand = leftOperand; 12 | this._rightOperand = rightOperand; 13 | } 14 | 15 | public int Interpret(Dictionary context) { 16 | return this._leftOperand.Interpret(context) + this._rightOperand.Interpret(context); 17 | } 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Behavioral/Interpreter/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Interpreter")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Interpreter")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("40580a9e-1023-437f-9c0b-510630db13c7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Interpreter/Variable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Interpreter { 4 | 5 | public class Variable : IExpression { 6 | 7 | private readonly string _name; 8 | 9 | public Variable(string name) { 10 | this._name = name; 11 | } 12 | 13 | public int Interpret(Dictionary context) { 14 | if (string.IsNullOrEmpty(this._name)) { 15 | return 0; 16 | } 17 | return context[this._name].Interpret(context); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Behavioral/Iterator/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Iterator { 4 | 5 | class Client { 6 | 7 | static void Main(string[] args) { 8 | IAggregate aggregate = new ConcreateAggregate("Item1", "Item2", "Item3", "Item4"); 9 | IIterator iterator = aggregate.CreateIterator(); 10 | 11 | if (iterator.IsDone()) { 12 | iterator.First(); 13 | } 14 | while (!iterator.IsDone()) { 15 | Console.WriteLine(iterator.CurrentItem()); 16 | iterator.Next(); 17 | } 18 | 19 | Console.ReadKey(); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Behavioral/Iterator/ConcreateAggregate.cs: -------------------------------------------------------------------------------- 1 | namespace Iterator { 2 | 3 | public class ConcreateAggregate : IAggregate { 4 | 5 | private readonly object[] _items; 6 | 7 | public ConcreateAggregate(params object[] items) { 8 | if (items != null) { 9 | this._items = new object[items.Length]; 10 | for (var i = 0; i < items.Length; i++) { 11 | this._items[i] = items[i]; 12 | } 13 | } 14 | } 15 | 16 | public IIterator CreateIterator() { 17 | return new ConcreteIterator(this._items); 18 | } 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Behavioral/Iterator/ConcreteIterator.cs: -------------------------------------------------------------------------------- 1 | namespace Iterator { 2 | 3 | public class ConcreteIterator : IIterator { 4 | 5 | private readonly object[] _aggregateItems; 6 | private int _index; 7 | 8 | public ConcreteIterator(object[] aggregateItems) { 9 | this._aggregateItems = aggregateItems; 10 | this._index = 0; 11 | } 12 | 13 | public object CurrentItem() { 14 | return this._aggregateItems[this._index]; 15 | } 16 | 17 | public void First() { 18 | this._index = 0; 19 | } 20 | 21 | public bool IsDone() { 22 | return this._index == this._aggregateItems.Length; 23 | } 24 | 25 | public void Next() { 26 | this._index += 1; 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Behavioral/Iterator/IAggregate.cs: -------------------------------------------------------------------------------- 1 | namespace Iterator { 2 | 3 | public interface IAggregate { 4 | 5 | IIterator CreateIterator(); 6 | } 7 | } -------------------------------------------------------------------------------- /Behavioral/Iterator/IIterator.cs: -------------------------------------------------------------------------------- 1 | namespace Iterator { 2 | 3 | public interface IIterator { 4 | 5 | object CurrentItem(); 6 | 7 | void First(); 8 | 9 | bool IsDone(); 10 | 11 | void Next(); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /Behavioral/Iterator/Iterator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43} 9 | Exe 10 | Properties 11 | Iterator 12 | Iterator 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | -------------------------------------------------------------------------------- /Behavioral/Iterator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Iterator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Iterator")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4b70f7dc-f4ef-4f22-b983-5a05e7dbd6c0")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Mediator/BookButton.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Controls; 2 | 3 | namespace Mediator { 4 | 5 | public class BookButton : Button, IColleague { 6 | 7 | private readonly IMediator _mediator; 8 | 9 | public BookButton(IMediator mediator) { 10 | this.Content = "Book"; 11 | this._mediator = mediator; 12 | this._mediator.RegisterBook(this); 13 | } 14 | 15 | public void Execute() { 16 | this._mediator.Book(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/DisplayLabel.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | using System.Windows.Controls; 3 | 4 | namespace Mediator { 5 | 6 | public class DisplayLabel : Label { 7 | 8 | private readonly IMediator _mediator; 9 | 10 | public DisplayLabel(IMediator mediator) { 11 | this.Content = "Starting ..."; 12 | this.FontSize = 24; 13 | //this.FontWeight = new FontWeight(); 14 | this._mediator = mediator; 15 | this._mediator.RegisterDisplay(this); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/IColleague.cs: -------------------------------------------------------------------------------- 1 | namespace Mediator { 2 | 3 | /// 4 | /// Colleague interface 5 | /// 6 | public interface IColleague { 7 | 8 | void Execute(); 9 | } 10 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/IMediator.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Controls; 2 | 3 | namespace Mediator { 4 | 5 | /// 6 | /// Mediator interface 7 | /// 8 | public interface IMediator { 9 | 10 | void Book(); 11 | 12 | void View(); 13 | 14 | void Search(); 15 | 16 | void RegisterView(ViewButton viewButton); 17 | 18 | void RegisterSearch(SearchButton searchButton); 19 | 20 | void RegisterBook(BookButton bookButton); 21 | 22 | void RegisterDisplay(DisplayLabel displayLabel); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/Mediator.cs: -------------------------------------------------------------------------------- 1 | namespace Mediator { 2 | 3 | class Mediator : IMediator { 4 | 5 | private ViewButton _viewButton; 6 | private SearchButton _searchButton; 7 | private BookButton _bookButton; 8 | private DisplayLabel _displayLabel; 9 | 10 | public void Book() { 11 | this._bookButton.IsEnabled = false; 12 | this._viewButton.IsEnabled = true; 13 | this._searchButton.IsEnabled = true; 14 | this._displayLabel.Content = "Booking ..."; 15 | } 16 | 17 | public void View() { 18 | this._bookButton.IsEnabled = true; 19 | this._viewButton.IsEnabled = false; 20 | this._searchButton.IsEnabled = true; 21 | this._displayLabel.Content = "Viewing ..."; 22 | } 23 | 24 | public void Search() { 25 | this._bookButton.IsEnabled = true; 26 | this._viewButton.IsEnabled = true; 27 | this._searchButton.IsEnabled = false; 28 | this._displayLabel.Content = "Searching ..."; 29 | } 30 | 31 | public void RegisterView(ViewButton viewButton) { 32 | this._viewButton = viewButton; 33 | } 34 | 35 | public void RegisterSearch(SearchButton searchButton) { 36 | this._searchButton = searchButton; 37 | } 38 | 39 | public void RegisterBook(BookButton bookButton) { 40 | this._bookButton = bookButton; 41 | } 42 | 43 | public void RegisterDisplay(DisplayLabel displayLabel) { 44 | this._displayLabel = displayLabel; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/Mediator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} 9 | Exe 10 | Properties 11 | Mediator 12 | Mediator 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 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 | 59 | -------------------------------------------------------------------------------- /Behavioral/Mediator/MediatorDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows; 3 | using System.Windows.Controls; 4 | 5 | namespace Mediator { 6 | 7 | class MediatorDemo : Window { 8 | 9 | private readonly IMediator _mediator = new Mediator(); 10 | 11 | public MediatorDemo() { 12 | 13 | var rootView = new Grid(); 14 | rootView.RowDefinitions.Add(new RowDefinition()); 15 | rootView.RowDefinitions.Add(new RowDefinition()); 16 | rootView.ColumnDefinitions.Add(new ColumnDefinition()); 17 | rootView.ColumnDefinitions.Add(new ColumnDefinition()); 18 | rootView.ColumnDefinitions.Add(new ColumnDefinition()); 19 | 20 | var viewButton = new ViewButton(this._mediator); 21 | viewButton.Click += this.OnViewClick; 22 | Grid.SetColumn(viewButton, 0); 23 | Grid.SetRow(viewButton, 0); 24 | rootView.Children.Add(viewButton); 25 | 26 | var bookButton = new BookButton(this._mediator); 27 | bookButton.Click += this.OnViewClick; 28 | Grid.SetColumn(bookButton, 1); 29 | Grid.SetRow(bookButton, 0); 30 | rootView.Children.Add(bookButton); 31 | 32 | var searchButton = new SearchButton(this._mediator); 33 | searchButton.Click += this.OnViewClick; 34 | Grid.SetColumn(searchButton, 2); 35 | Grid.SetRow(searchButton, 0); 36 | rootView.Children.Add(searchButton); 37 | 38 | 39 | var displayLabel = new DisplayLabel(this._mediator); 40 | Grid.SetColumn(displayLabel, 0); 41 | Grid.SetRow(displayLabel, 1); 42 | Grid.SetColumnSpan(displayLabel, 3); 43 | rootView.Children.Add(displayLabel); 44 | 45 | this.Content = rootView; 46 | 47 | this.Width = 400; 48 | this.Height = 200; 49 | this.Title = "Mediator Demo"; 50 | } 51 | 52 | private void OnViewClick(object sender, RoutedEventArgs routedEventArgs) { 53 | var colleague = (IColleague)sender; 54 | colleague.Execute(); 55 | } 56 | 57 | [STAThread] 58 | static void Main(string[] args) { 59 | var app = new Application(); 60 | app.Startup += AppOnStartup; 61 | app.Run(); 62 | } 63 | 64 | private static void AppOnStartup(object sender, StartupEventArgs startupEventArgs) { 65 | Application.Current.MainWindow = new MediatorDemo(); 66 | Application.Current.MainWindow.Show(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Behavioral/Mediator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Mediator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Mediator")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2bac36b6-5ac6-43c2-8785-4d351ba7792b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Mediator/SearchButton.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Controls; 2 | 3 | namespace Mediator { 4 | 5 | public class SearchButton : Button, IColleague { 6 | 7 | private readonly IMediator _mediator; 8 | 9 | public SearchButton(IMediator mediator) { 10 | this.Content = "Search"; 11 | this._mediator = mediator; 12 | this._mediator.RegisterSearch(this); 13 | } 14 | 15 | public void Execute() { 16 | this._mediator.Search(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/Mediator/ViewButton.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Controls; 2 | 3 | namespace Mediator { 4 | 5 | public class ViewButton : Button, IColleague { 6 | 7 | private readonly IMediator _mediator; 8 | 9 | public ViewButton(IMediator mediator) { 10 | this.Content = "View"; 11 | this._mediator = mediator; 12 | this._mediator.RegisterView(this); 13 | } 14 | 15 | public void Execute() { 16 | this._mediator.View(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/Memento/CareTaker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Memento { 5 | 6 | class CareTaker { 7 | 8 | static void Main(string[] args) { 9 | IList savedStates = new List(); 10 | 11 | var originator = new Originator(); 12 | originator.SetState("State1"); 13 | originator.SetState("State2"); 14 | savedStates.Add(originator.SaveToMomento()); 15 | originator.SetState("State3"); 16 | savedStates.Add(originator.SaveToMomento()); 17 | originator.SetState("State4"); 18 | originator.RestoreFromMomento(savedStates[0]); 19 | 20 | Console.ReadKey(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Behavioral/Memento/Memento.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {C404876C-B04B-451F-B159-2C0CF8CB861B} 9 | Exe 10 | Properties 11 | Memento 12 | Memento 13 | Client 14 | 512 15 | 16 | 17 | Memento.CareTaker 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | -------------------------------------------------------------------------------- /Behavioral/Memento/Originator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Memento { 4 | 5 | public class Originator { 6 | 7 | private string _state; 8 | 9 | public void SetState(string state) { 10 | Console.WriteLine("Originator: setting momento to {0}", state); 11 | this._state = state; 12 | } 13 | 14 | public Momento SaveToMomento() { 15 | Console.WriteLine("Originator: saving to momento..."); 16 | return new Originator.Momento(this._state); 17 | } 18 | 19 | public void RestoreFromMomento(Momento momento) { 20 | this._state = momento.State; 21 | Console.WriteLine("Originator: resotring from momento {0}", this._state); 22 | } 23 | 24 | public class Momento { 25 | 26 | private readonly string _state; 27 | 28 | internal Momento(string state) { 29 | this._state = state; 30 | } 31 | 32 | public string State { 33 | get { 34 | return this._state; 35 | } 36 | } 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /Behavioral/Memento/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Memento")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Memento")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("02b2940c-887c-4401-9b58-88a108d729b8")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Observer/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Observer { 7 | 8 | class Client { 9 | 10 | static void Main(string[] args) { 11 | var subject = new ConcreteSubject(); 12 | 13 | subject.Attach(new ConcreteObserver(subject)); 14 | subject.Attach(new ConcreteObserver(subject)); 15 | 16 | subject.State = "State1"; 17 | subject.Notify(); 18 | 19 | subject.State = "State2"; 20 | subject.Notify(); 21 | 22 | Console.ReadKey(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Behavioral/Observer/ConcreteObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Observer { 4 | 5 | public class ConcreteObserver : IObserver { 6 | 7 | private readonly ConcreteSubject _subject; 8 | 9 | public ConcreteObserver(ConcreteSubject subject) { 10 | this._subject = subject; 11 | } 12 | 13 | public void Update() { 14 | Console.WriteLine("Observer: subject state updated to {0} .", this._subject.State); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Behavioral/Observer/ConcreteSubject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Observer { 4 | 5 | public class ConcreteSubject : ISubject { 6 | 7 | private readonly IList _observers = new List(); 8 | 9 | public string State { 10 | get; 11 | set; 12 | } 13 | 14 | public void Attach(IObserver observer) { 15 | if (!this._observers.Contains(observer)) { 16 | this._observers.Add(observer); 17 | } 18 | } 19 | 20 | public void Detach(IObserver observer) { 21 | if (this._observers.Contains(observer)) { 22 | this._observers.Remove(observer); 23 | } 24 | } 25 | 26 | public void Notify() { 27 | foreach (var observer in _observers) { 28 | observer.Update(); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Behavioral/Observer/IObserver.cs: -------------------------------------------------------------------------------- 1 | namespace Observer { 2 | 3 | public interface IObserver { 4 | 5 | void Update(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Behavioral/Observer/ISubject.cs: -------------------------------------------------------------------------------- 1 | namespace Observer { 2 | 3 | public interface ISubject { 4 | 5 | void Attach(IObserver observer); 6 | 7 | void Detach(IObserver observer); 8 | 9 | void Notify(); 10 | } 11 | } -------------------------------------------------------------------------------- /Behavioral/Observer/Observer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {2823F21D-D474-447D-9C8B-74B29D0D9B35} 9 | Exe 10 | Properties 11 | Observer 12 | Observer 13 | Client 14 | 512 15 | 16 | 17 | Observer.Client 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /Behavioral/Observer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Observer")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Observer")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("6667d06c-0f67-48a7-88f6-14b5aa791005")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/State/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace State { 4 | 5 | class Client { 6 | 7 | static void Main(string[] args) { 8 | var sc = new StateContext(); 9 | sc.WriteName("Monday"); 10 | sc.WriteName("Tuesday"); 11 | sc.WriteName("Wednesday"); 12 | sc.WriteName("Thursday"); 13 | sc.WriteName("Saturday"); 14 | sc.WriteName("Sunday"); 15 | 16 | Console.ReadKey(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Behavioral/State/IState.cs: -------------------------------------------------------------------------------- 1 | namespace State { 2 | 3 | public interface IState { 4 | 5 | void WriteName(StateContext stateContext, string name); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Behavioral/State/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("State")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("State")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("61a69783-73c6-4634-9c1e-97dc1992b3b0")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/State/State.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {F214B483-E8BB-477D-807F-24F176B588D5} 9 | Exe 10 | Properties 11 | State 12 | State 13 | Client 14 | 512 15 | 16 | 17 | State.Client 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /Behavioral/State/StateA.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace State { 4 | 5 | public class StateA : IState { 6 | 7 | public void WriteName(StateContext stateContext, string name) { 8 | Console.WriteLine(name.ToLower()); 9 | stateContext.SetState(new StateB()); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Behavioral/State/StateB.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace State { 4 | 5 | public class StateB : IState { 6 | 7 | private int _count; 8 | 9 | public void WriteName(StateContext stateContext, string name) { 10 | Console.WriteLine(name.ToUpper()); 11 | if (++this._count > 1) { 12 | stateContext.SetState(new StateA()); 13 | } 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Behavioral/State/StateContext.cs: -------------------------------------------------------------------------------- 1 | namespace State { 2 | 3 | public class StateContext { 4 | 5 | private IState _state; 6 | 7 | public StateContext() { 8 | this._state = new StateA(); 9 | } 10 | 11 | public void SetState(IState state) { 12 | this._state = state; 13 | } 14 | 15 | public void WriteName(string name) { 16 | this._state.WriteName(this, name); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Behavioral/Strategy/AddStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy { 2 | 3 | public class AddStrategy : IStrategy { 4 | 5 | public int Execute(int a, int b) { 6 | return a + b; 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /Behavioral/Strategy/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Strategy { 4 | 5 | class Client { 6 | 7 | static void Main(string[] args) { 8 | 9 | Context context; 10 | 11 | // Three contexts following different strategies 12 | context = new Context(new AddStrategy()); 13 | var resultA = context.ExecuteStrategy(3, 4); 14 | Console.WriteLine(resultA); 15 | 16 | context = new Context(new SubstractStrategy()); 17 | var resultB = context.ExecuteStrategy(3, 4); 18 | Console.WriteLine(resultB); 19 | 20 | context = new Context(new MultiplyStrategy()); 21 | var resultC = context.ExecuteStrategy(3, 4); 22 | Console.WriteLine(resultC); 23 | 24 | Console.ReadKey(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Behavioral/Strategy/Context.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy { 2 | 3 | public class Context { 4 | 5 | private readonly IStrategy _strategy; 6 | 7 | public Context(IStrategy strategy) { 8 | this._strategy = strategy; 9 | } 10 | 11 | public int ExecuteStrategy(int a, int b) { 12 | return this._strategy.Execute(a, b); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Behavioral/Strategy/IStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy { 2 | 3 | public interface IStrategy { 4 | 5 | int Execute(int a, int b); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Behavioral/Strategy/MultiplyStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy { 2 | 3 | public class MultiplyStrategy : IStrategy { 4 | 5 | public int Execute(int a, int b) { 6 | return a * b; 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /Behavioral/Strategy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Strategy")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Strategy")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2b3f684e-ce46-4d48-822a-1ed0326f7aa0")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Strategy/Strategy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254} 9 | Exe 10 | Properties 11 | Strategy 12 | Strategy 13 | Client 14 | 512 15 | 16 | 17 | Strategy.Client 18 | 19 | 20 | false 21 | bin\Debug 22 | 4 23 | 24 | 25 | false 26 | bin\Release 27 | 4 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 55 | -------------------------------------------------------------------------------- /Behavioral/Strategy/SubstractStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy { 2 | 3 | public class SubstractStrategy : IStrategy { 4 | 5 | public int Execute(int a, int b) { 6 | return a - b; 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Chess.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplateMethod { 4 | 5 | public class Chess : Game { 6 | 7 | public override void InitializeGame() { 8 | throw new NotImplementedException(); 9 | } 10 | 11 | protected override void MakePlay(int player) { 12 | throw new NotImplementedException(); 13 | } 14 | 15 | protected override bool EndOfGame() { 16 | throw new NotImplementedException(); 17 | } 18 | 19 | protected override void PrintWinner() { 20 | throw new NotImplementedException(); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace TemplateMethod { 7 | 8 | class Client { 9 | 10 | static void Main(string[] args) { 11 | Game monopolyGame = new Monopoly(); 12 | monopolyGame.PlayOneGame(4); 13 | 14 | Console.ReadKey(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Game.cs: -------------------------------------------------------------------------------- 1 | namespace TemplateMethod { 2 | 3 | public abstract class Game { 4 | 5 | protected int PlayerCount; 6 | 7 | public abstract void InitializeGame(); 8 | 9 | protected abstract void MakePlay(int player); 10 | 11 | protected abstract bool EndOfGame(); 12 | 13 | protected abstract void PrintWinner(); 14 | 15 | public void PlayOneGame(int playerCount) { 16 | this.PlayerCount = playerCount; 17 | 18 | this.InitializeGame(); 19 | 20 | var j = 0; 21 | while (!this.EndOfGame()) { 22 | this.MakePlay(j); 23 | j = (j + 1) % this.PlayerCount; 24 | } 25 | this.PrintWinner(); 26 | } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Monopoly.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplateMethod { 4 | 5 | public class Monopoly : Game { 6 | 7 | public override void InitializeGame() { 8 | Console.WriteLine("Initialize players"); 9 | Console.WriteLine("Initialize money"); 10 | } 11 | 12 | protected override void MakePlay(int player) { 13 | Console.WriteLine("Process one turn of player"); 14 | } 15 | 16 | protected override bool EndOfGame() { 17 | Console.WriteLine("Return true if game is over, according to Monopoly rules."); 18 | return true; 19 | } 20 | 21 | protected override void PrintWinner() { 22 | Console.WriteLine("Whow won ?"); 23 | } 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TemplateMethod")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TemplateMethod")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e67506b6-7642-4531-ae0b-fec504643f74")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/TemplateMethod/TemplateMethod.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5} 9 | Exe 10 | Properties 11 | TemplateMethod 12 | TemplateMethod 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 50 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Body.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | class Body : ICarElement { 4 | 5 | public void Accept(ICarElementVisitor visitor) { 6 | visitor.Visit(this); 7 | } 8 | 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/Car.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | class Car : ICarElement { 4 | 5 | private readonly ICarElement[] _elements; 6 | 7 | public Car() { 8 | this._elements = new ICarElement[] { 9 | new Wheel("front left"), 10 | new Wheel("front right"), 11 | new Wheel("back left"), 12 | new Wheel("back right"), 13 | new Body(), 14 | new Engine() 15 | }; 16 | } 17 | 18 | public void Accept(ICarElementVisitor visitor) { 19 | foreach (var element in this._elements) { 20 | element.Accept(visitor); 21 | } 22 | visitor.Visit(this); 23 | } 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/CarElementDoVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Visitor { 4 | 5 | class CarElementDoVisitor : ICarElementVisitor { 6 | 7 | public void Visit(Wheel wheel) { 8 | Console.WriteLine("kicking my {0} wheel", wheel.Name); 9 | } 10 | 11 | public void Visit(Engine engine) { 12 | Console.WriteLine("starting my engine"); 13 | } 14 | 15 | public void Visit(Body body) { 16 | Console.WriteLine("moving my body"); 17 | } 18 | 19 | public void Visit(Car car) { 20 | Console.WriteLine("starting my car"); 21 | } 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/CarElementPrintVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Visitor { 4 | 5 | class CarElementPrintVisitor : ICarElementVisitor { 6 | 7 | public void Visit(Wheel wheel) { 8 | Console.WriteLine("visiting {0} wheel", wheel.Name); 9 | } 10 | 11 | public void Visit(Engine engine) { 12 | Console.WriteLine("visiting engine"); 13 | } 14 | 15 | public void Visit(Body body) { 16 | Console.WriteLine("visiting body"); 17 | } 18 | 19 | public void Visit(Car car) { 20 | Console.WriteLine("visitig car"); 21 | } 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/Engine.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | class Engine : ICarElement { 4 | 5 | public void Accept(ICarElementVisitor visitor) { 6 | visitor.Visit(this); 7 | } 8 | 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/ICarElement.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | interface ICarElement { 4 | 5 | void Accept(ICarElementVisitor visitor); 6 | 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/ICarElementVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | interface ICarElementVisitor { 4 | 5 | void Visit(Wheel wheel); 6 | 7 | void Visit(Engine engine); 8 | 9 | void Visit(Body body); 10 | 11 | void Visit(Car car); 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /Behavioral/Visitor/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Visitor { 4 | 5 | class Program { 6 | 7 | static void Main(string[] args) { 8 | 9 | var car = new Car(); 10 | car.Accept(new CarElementPrintVisitor()); 11 | car.Accept(new CarElementDoVisitor()); 12 | 13 | Console.ReadKey(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Visitor")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Visitor")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("70e3f33b-8ee0-4c32-9aef-6d4576a7d07d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Visitor.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32} 9 | Exe 10 | Properties 11 | Visitor 12 | Visitor 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 55 | -------------------------------------------------------------------------------- /Behavioral/Visitor/Wheel.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor { 2 | 3 | class Wheel : ICarElement { 4 | 5 | public string Name { 6 | get; 7 | private set; 8 | } 9 | 10 | public Wheel(string name) { 11 | this.Name = name; 12 | } 13 | 14 | public void Accept(ICarElementVisitor visitor) { 15 | visitor.Visit(this); 16 | } 17 | 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/AbstractFactory.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4} 9 | Exe 10 | Properties 11 | AbstractFactory 12 | AbstractFactory 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 55 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/Application.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory { 2 | 3 | public class Application { 4 | 5 | public Application(IGuiFactory factory) { 6 | var button = factory.CreateButton(); 7 | button.Paint(); 8 | } 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/ApplicationRunner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Configuration; 3 | 4 | namespace AbstractFactory { 5 | 6 | public class ApplicationRunner { 7 | 8 | static IGuiFactory CreateOsSpecificFactory() { 9 | var sysType = ConfigurationManager.AppSettings["OS_TYPE"] ?? "Win"; 10 | if (sysType == "Win") { 11 | return new WinFactory(); 12 | } 13 | return new OsxFactory(); 14 | } 15 | 16 | static void Main(string[] args) { 17 | new Application(CreateOsSpecificFactory()); 18 | Console.ReadKey(); 19 | } 20 | 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/IButton.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory { 2 | 3 | public interface IButton { 4 | 5 | void Paint(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/IGuiFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory { 2 | 3 | public interface IGuiFactory { 4 | 5 | IButton CreateButton(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/OsxButton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AbstractFactory { 4 | 5 | public class OsxButton : IButton { 6 | 7 | public void Paint() { 8 | Console.WriteLine("I'm an OSXButton"); 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/OsxFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory { 2 | 3 | public class OsxFactory : IGuiFactory { 4 | 5 | IButton IGuiFactory.CreateButton() { 6 | return new OsxButton(); 7 | } 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AbstractFactory")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AbstractFactory")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("50449864-76c1-4b30-8470-493802accc35")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Creational/AbstractFactory/WinButton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AbstractFactory { 4 | 5 | public class WinButton : IButton { 6 | 7 | public void Paint() { 8 | Console.WriteLine("I'm a WinButton"); 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Creational/AbstractFactory/WinFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory { 2 | 3 | public class WinFactory : IGuiFactory { 4 | 5 | IButton IGuiFactory.CreateButton() { 6 | return new WinButton(); 7 | } 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Creational/Builder/Builder.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC} 9 | Exe 10 | Properties 11 | Builder 12 | Builder 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 52 | -------------------------------------------------------------------------------- /Creational/Builder/Cook.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | class Cook { 4 | 5 | private PizzaBuilder _pizzaBuilder; 6 | 7 | public void SetPizzaBuilder(PizzaBuilder pb) { 8 | this._pizzaBuilder = pb; 9 | } 10 | 11 | public Pizza GetPizza() { 12 | return this._pizzaBuilder.GetPizza(); 13 | } 14 | 15 | public void ConstructPizza() { 16 | this._pizzaBuilder.CreateNewPizzaProduct(); 17 | this._pizzaBuilder.BuildDough(); 18 | this._pizzaBuilder.BuildSauce(); 19 | this._pizzaBuilder.BuildTopping(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Creational/Builder/HawaiianPizzaBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | class HawaiianPizzaBuilder : PizzaBuilder { 4 | 5 | public override void BuildDough() { 6 | this.Pizza.Dough = "cross"; 7 | } 8 | 9 | public override void BuildSauce() { 10 | this.Pizza.Sauce = "mild"; 11 | } 12 | 13 | public override void BuildTopping() { 14 | this.Pizza.Topping = "ham+pineapple"; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Creational/Builder/Pizza.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | public class Pizza { 4 | 5 | private string _dough = string.Empty; 6 | private string _sauce = string.Empty; 7 | private string _topping = string.Empty; 8 | 9 | public string Dough { 10 | get { 11 | return this._dough; 12 | } 13 | set { 14 | this._dough = value; 15 | } 16 | } 17 | 18 | public string Sauce { 19 | get { 20 | return this._sauce; 21 | } 22 | set { 23 | this._sauce = value; 24 | } 25 | } 26 | 27 | public string Topping { 28 | get { 29 | return this._topping; 30 | } 31 | set { 32 | this._topping = value; 33 | } 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Creational/Builder/PizzaBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | abstract class PizzaBuilder { 4 | 5 | protected Pizza Pizza; 6 | 7 | public Pizza GetPizza() { 8 | return this.Pizza; 9 | } 10 | 11 | public void CreateNewPizzaProduct() { 12 | this.Pizza = new Pizza(); 13 | } 14 | 15 | public abstract void BuildDough(); 16 | public abstract void BuildSauce(); 17 | public abstract void BuildTopping(); 18 | } 19 | } -------------------------------------------------------------------------------- /Creational/Builder/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | class Program { 4 | 5 | static void Main(string[] args) { 6 | PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder(); 7 | var cook = new Cook(); 8 | cook.SetPizzaBuilder(hawaiianPizzaBuilder); 9 | cook.ConstructPizza(); 10 | // create the product 11 | var hawaiian = cook.GetPizza(); 12 | 13 | PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder(); 14 | cook.SetPizzaBuilder(spicyPizzaBuilder); 15 | cook.ConstructPizza(); 16 | // create another product 17 | var spicy = cook.GetPizza(); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Creational/Builder/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Builder")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Builder")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fd240c1d-c55c-43de-95b7-c0eaf2abebab")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Creational/Builder/SpicyPizzaBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace Builder { 2 | 3 | class SpicyPizzaBuilder : PizzaBuilder { 4 | 5 | public override void BuildDough() { 6 | this.Pizza.Dough = "pan baked"; 7 | } 8 | 9 | public override void BuildSauce() { 10 | this.Pizza.Sauce = "hot"; 11 | } 12 | 13 | public override void BuildTopping() { 14 | this.Pizza.Topping = "pepperoni + salami"; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Creational/FactoryMethod/FactoryMethod.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4} 9 | Exe 10 | Properties 11 | FactoryMethod 12 | FactoryMethod 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/MagicMazeGame.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryMethod { 2 | 3 | class MagicMazeGame : MazeGame { 4 | 5 | protected override Room MakeRoom() { 6 | return new MagicRoom(); 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /Creational/FactoryMethod/MagicRoom.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryMethod { 2 | 3 | class MagicRoom : Room { 4 | 5 | } 6 | } -------------------------------------------------------------------------------- /Creational/FactoryMethod/MazeGame.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace FactoryMethod { 4 | 5 | abstract class MazeGame { 6 | 7 | private readonly IList _rooms = new List(); 8 | 9 | protected MazeGame() { 10 | var room1 = this.MakeRoom(); 11 | var room2 = this.MakeRoom(); 12 | room1.Connect(room2); 13 | this.AddRoom(room1); 14 | this.AddRoom(room2); 15 | } 16 | 17 | private void AddRoom(Room room) { 18 | this._rooms.Add(room); 19 | } 20 | 21 | protected abstract Room MakeRoom(); 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /Creational/FactoryMethod/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryMethod { 4 | 5 | class Program { 6 | 7 | static void Main(string[] args) { 8 | 9 | MazeGame game = new MagicMazeGame(); 10 | 11 | Console.ReadKey(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FactoryMethod")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("FactoryMethod")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e647fadd-b3c4-4480-81ab-06fc61ba2105")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Creational/FactoryMethod/Room.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryMethod { 2 | 3 | class Room { 4 | 5 | private Room _next; 6 | 7 | public void Connect(Room other) { 8 | this._next = other; 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Creational/NullObject/AbstractBook.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NullObject { 4 | 5 | public abstract class AbstractBook { 6 | 7 | protected string title; 8 | 9 | public abstract string GetTitle(); 10 | 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /Creational/NullObject/Book.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NullObject { 4 | 5 | public class Book : AbstractBook { 6 | 7 | public Book(string title) { 8 | this.title = title; 9 | } 10 | 11 | public override string GetTitle() { 12 | return title; 13 | } 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Creational/NullObject/BookFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NullObject { 4 | 5 | public class BookFactory { 6 | 7 | public static string[] books = { "Book1", "Book2", "Book3" }; 8 | 9 | public static AbstractBook GetBook(string title) { 10 | for (int i = 0; i < books.Length; i++) { 11 | if (books[i] == title) { 12 | return new Book(title); 13 | } 14 | } 15 | return new NullBook(); 16 | } 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Creational/NullObject/NullBook.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NullObject { 4 | 5 | class NullBook : AbstractBook { 6 | 7 | public override string GetTitle() { 8 | return "Not avaible in Customer Database"; 9 | } 10 | 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /Creational/NullObject/NullObject.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29} 9 | Exe 10 | NullObject 11 | NullObject 12 | v4.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG; 20 | prompt 21 | 4 22 | true 23 | 24 | 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Creational/NullObject/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NullObject { 4 | 5 | public class Program { 6 | 7 | static void Main(string[] args) { 8 | 9 | AbstractBook c1 = BookFactory.GetBook("Book1"); 10 | AbstractBook c2 = BookFactory.GetBook("Book5"); 11 | AbstractBook c3 = BookFactory.GetBook("Book2"); 12 | AbstractBook c4 = BookFactory.GetBook("Book3"); 13 | AbstractBook c5 = BookFactory.GetBook("Book9"); 14 | 15 | Console.WriteLine(c1.GetTitle()); 16 | Console.WriteLine(c2.GetTitle()); 17 | Console.WriteLine(c3.GetTitle()); 18 | Console.WriteLine(c4.GetTitle()); 19 | Console.WriteLine(c5.GetTitle()); 20 | } 21 | 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Creational/NullObject/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("NullObject")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("zhang")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | -------------------------------------------------------------------------------- /Creational/ObjectPool/ObjectPool.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698} 9 | Exe 10 | ObjectPool 11 | ObjectPool 12 | v4.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG; 20 | prompt 21 | 4 22 | true 23 | 24 | 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Creational/ObjectPool/Pool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace ObjectPool { 6 | 7 | public static class Pool { 8 | 9 | private static readonly IList InUsed = new List(); 10 | private static readonly IList Available = new List(); 11 | 12 | public static PooledObject GetObject() { 13 | lock (Available) { 14 | PooledObject obj; 15 | if (Available.Count == 0) { 16 | obj = new PooledObject(); 17 | InUsed.Add(obj); 18 | return obj; 19 | } 20 | obj = Available[0]; 21 | InUsed.Add(obj); 22 | Available.RemoveAt(0); 23 | return obj; 24 | } 25 | } 26 | 27 | public static void ReleaseObject(PooledObject obj) { 28 | CleanUp(obj); 29 | lock (Available) { 30 | InUsed.Remove(obj); 31 | Available.Add(obj); 32 | } 33 | } 34 | 35 | public static void CleanUp(PooledObject obj) { 36 | obj.TempData = null; 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Creational/ObjectPool/PooledObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace ObjectPool { 6 | 7 | public class PooledObject { 8 | 9 | public DateTime CreatedAt { get; } = DateTime.Now; 10 | 11 | public string TempData { get; set; } 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Creational/ObjectPool/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace ObjectPool { 6 | 7 | class Program { 8 | 9 | public static void Main(string[] args) { 10 | var po01 = Pool.GetObject(); 11 | po01.TempData = DateTime.Now.ToString("s"); 12 | var po02 = Pool.GetObject(); 13 | po02.TempData = Guid.NewGuid().ToString("N"); 14 | Thread.Sleep(100); 15 | Pool.ReleaseObject(po01); 16 | Pool.ReleaseObject(po02); 17 | Console.ReadLine(); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Creational/ObjectPool/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("ObjectPool")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("zhang")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | -------------------------------------------------------------------------------- /Creational/Prototype/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Prototype { 4 | 5 | class Program { 6 | 7 | static void Main(string[] args) { 8 | Prototype prototype = new PrototypeImpl(1000); 9 | 10 | for (int i = 1; i < 10; i++) { 11 | var tempotype = (Prototype)prototype.Clone(); 12 | 13 | // Usage of values in prototype to derive a new value. 14 | tempotype.X = tempotype.X * i; 15 | tempotype.PrintX(); 16 | } 17 | 18 | Console.ReadKey(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Creational/Prototype/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Prototype")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Prototype")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f44fa181-60ef-47c9-9b69-69d45b9c5f68")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Creational/Prototype/Prototype.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Prototype { 4 | 5 | abstract class Prototype : ICloneable { 6 | 7 | public abstract int X { 8 | get; 9 | set; 10 | } 11 | 12 | public abstract void PrintX(); 13 | 14 | public abstract object Clone(); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /Creational/Prototype/Prototype.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E} 9 | Exe 10 | Properties 11 | Prototype 12 | Prototype 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 49 | -------------------------------------------------------------------------------- /Creational/Prototype/PrototypeImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Prototype { 4 | 5 | class PrototypeImpl : Prototype { 6 | 7 | private int _x; 8 | 9 | public override int X { 10 | get { 11 | return this._x; 12 | } 13 | set { 14 | this._x = value; 15 | } 16 | } 17 | 18 | public PrototypeImpl(int x) { 19 | this._x = x; 20 | } 21 | 22 | public override void PrintX() { 23 | Console.WriteLine("value : {0}", this.X); 24 | } 25 | 26 | public override object Clone() { 27 | return new PrototypeImpl(this._x); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Creational/Singleton/DoublecheckSingleton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton { 4 | 5 | public class DoublecheckSingleton { 6 | 7 | private volatile static DoublecheckSingleton _instance; 8 | 9 | private DoublecheckSingleton() { 10 | } 11 | 12 | public static DoublecheckSingleton Instance { 13 | get { 14 | if (_instance == null) { 15 | lock (typeof(DoublecheckSingleton)) { 16 | if (_instance == null) { 17 | Console.WriteLine("Initialize a double check singleton."); 18 | _instance = new DoublecheckSingleton(); 19 | } 20 | } 21 | } 22 | return _instance; 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Creational/Singleton/LazySingleton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton { 4 | 5 | public class LazySingleton { 6 | 7 | private static readonly Lazy LazyInstance = new Lazy(() => { 8 | Console.WriteLine("Initialize a lazy singleton."); 9 | return new LazySingleton(); 10 | }); 11 | 12 | private LazySingleton() { 13 | } 14 | 15 | public static LazySingleton Instance { 16 | get { 17 | return LazyInstance.Value; 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Creational/Singleton/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace Singleton { 5 | 6 | class Program { 7 | 8 | static void Main(string[] args) { 9 | 10 | for (int i = 0; i < 100; i++) { 11 | Task.Factory.StartNew(() => Console.WriteLine(DoublecheckSingleton.Instance.ToString())); 12 | } 13 | 14 | for (int i = 0; i < 100; i++) { 15 | Task.Factory.StartNew(() => Console.WriteLine(LazySingleton.Instance.ToString())); 16 | } 17 | 18 | Console.ReadKey(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Creational/Singleton/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Singleton")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Singleton")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d14f4089-6605-4004-8400-853b36efe937")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Creational/Singleton/Singleton.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95} 9 | Exe 10 | Properties 11 | Singleton 12 | Singleton 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 49 | -------------------------------------------------------------------------------- /DesignPatterns.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Behavioral", "Behavioral", "{B3C3F0E2-50B6-450B-A161-F4500F4E681D}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "Behavioral\ChainOfResponsibility\ChainOfResponsibility.csproj", "{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Behavioral\Command\Command.csproj", "{65459173-442A-4E0D-B758-3B9EDCE54D75}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interpreter", "Behavioral\Interpreter\Interpreter.csproj", "{6007D4E1-1676-4FBC-816D-1B19F6829F22}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Behavioral\Iterator\Iterator.csproj", "{2A37AC43-5F17-4774-B9BE-8D5008F41F43}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Behavioral\Mediator\Mediator.csproj", "{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}" 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Behavioral\Memento\Memento.csproj", "{C404876C-B04B-451F-B159-2C0CF8CB861B}" 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Behavioral\Observer\Observer.csproj", "{2823F21D-D474-447D-9C8B-74B29D0D9B35}" 19 | EndProject 20 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "State", "Behavioral\State\State.csproj", "{F214B483-E8BB-477D-807F-24F176B588D5}" 21 | EndProject 22 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Behavioral\Strategy\Strategy.csproj", "{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}" 23 | EndProject 24 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateMethod", "Behavioral\TemplateMethod\TemplateMethod.csproj", "{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}" 25 | EndProject 26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Visitor", "Behavioral\Visitor\Visitor.csproj", "{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}" 27 | EndProject 28 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Creational", "Creational", "{B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}" 29 | EndProject 30 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactory", "Creational\AbstractFactory\AbstractFactory.csproj", "{FF8A73BD-7DCB-46C1-8F83-EE84D65320B4}" 31 | EndProject 32 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Creational\Builder\Builder.csproj", "{92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC}" 33 | EndProject 34 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "Creational\FactoryMethod\FactoryMethod.csproj", "{040AFA71-3175-4C6C-8DB2-98810D3E98F4}" 35 | EndProject 36 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Creational\Prototype\Prototype.csproj", "{598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}" 37 | EndProject 38 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Creational\Singleton\Singleton.csproj", "{14333E87-2DCC-42A7-91FF-2DFEABF2AA95}" 39 | EndProject 40 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Structural", "Structural", "{05993A61-BAEE-4BA2-81F0-25B789A15F6E}" 41 | EndProject 42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Structural\Adapter\Adapter.csproj", "{72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0}" 43 | EndProject 44 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Structural\Bridge\Bridge.csproj", "{FECD9206-68C5-4F31-A7AB-AD876E05ABDA}" 45 | EndProject 46 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Structural\Composite\Composite.csproj", "{F8ED6676-6701-4476-9606-4D150EF4A537}" 47 | EndProject 48 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Structural\Decorator\Decorator.csproj", "{7F3A9BF2-137D-41AA-9346-4E117326850D}" 49 | EndProject 50 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facade", "Structural\Facade\Facade.csproj", "{A3F748C6-13F4-4DE1-880B-73664F801EA7}" 51 | EndProject 52 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flyweight", "Structural\Flyweight\Flyweight.csproj", "{7E303E38-84E0-45B9-8580-3109479B1F49}" 53 | EndProject 54 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxy", "Structural\Proxy\Proxy.csproj", "{D859AF5D-34D0-4D0A-8264-0C2AB88B6612}" 55 | EndProject 56 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Specification", "Structural\Specification\Specification.csproj", "{7C8D50CF-566A-4CDE-ACC3-3D36A06790E9}" 57 | EndProject 58 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullObject", "Creational\NullObject\NullObject.csproj", "{FA5CB853-0E45-4A38-9936-D68BB88B3B29}" 59 | EndProject 60 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPool", "Creational\ObjectPool\ObjectPool.csproj", "{15F148FB-F4A1-4E0D-82B2-047F63ED5698}" 61 | EndProject 62 | Global 63 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 64 | Debug|Any CPU = Debug|Any CPU 65 | Release|Any CPU = Release|Any CPU 66 | EndGlobalSection 67 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 68 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 73 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Debug|Any CPU.Build.0 = Debug|Any CPU 74 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Release|Any CPU.ActiveCfg = Release|Any CPU 75 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95}.Release|Any CPU.Build.0 = Release|Any CPU 76 | {2823F21D-D474-447D-9C8B-74B29D0D9B35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {2823F21D-D474-447D-9C8B-74B29D0D9B35}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|Any CPU.ActiveCfg = Release|Any CPU 79 | {2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|Any CPU.Build.0 = Release|Any CPU 80 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 81 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Debug|Any CPU.Build.0 = Debug|Any CPU 82 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Release|Any CPU.ActiveCfg = Release|Any CPU 83 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Release|Any CPU.Build.0 = Release|Any CPU 84 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 85 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Debug|Any CPU.Build.0 = Debug|Any CPU 86 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Release|Any CPU.ActiveCfg = Release|Any CPU 87 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Release|Any CPU.Build.0 = Release|Any CPU 88 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 89 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Debug|Any CPU.Build.0 = Debug|Any CPU 90 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|Any CPU.ActiveCfg = Release|Any CPU 91 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|Any CPU.Build.0 = Release|Any CPU 92 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 93 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Debug|Any CPU.Build.0 = Debug|Any CPU 94 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|Any CPU.ActiveCfg = Release|Any CPU 95 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E}.Release|Any CPU.Build.0 = Release|Any CPU 96 | {6007D4E1-1676-4FBC-816D-1B19F6829F22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 97 | {6007D4E1-1676-4FBC-816D-1B19F6829F22}.Debug|Any CPU.Build.0 = Debug|Any CPU 98 | {6007D4E1-1676-4FBC-816D-1B19F6829F22}.Release|Any CPU.ActiveCfg = Release|Any CPU 99 | {6007D4E1-1676-4FBC-816D-1B19F6829F22}.Release|Any CPU.Build.0 = Release|Any CPU 100 | {65459173-442A-4E0D-B758-3B9EDCE54D75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 101 | {65459173-442A-4E0D-B758-3B9EDCE54D75}.Debug|Any CPU.Build.0 = Debug|Any CPU 102 | {65459173-442A-4E0D-B758-3B9EDCE54D75}.Release|Any CPU.ActiveCfg = Release|Any CPU 103 | {65459173-442A-4E0D-B758-3B9EDCE54D75}.Release|Any CPU.Build.0 = Release|Any CPU 104 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 105 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0}.Debug|Any CPU.Build.0 = Debug|Any CPU 106 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0}.Release|Any CPU.ActiveCfg = Release|Any CPU 107 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0}.Release|Any CPU.Build.0 = Release|Any CPU 108 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 109 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9}.Debug|Any CPU.Build.0 = Debug|Any CPU 110 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9}.Release|Any CPU.ActiveCfg = Release|Any CPU 111 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9}.Release|Any CPU.Build.0 = Release|Any CPU 112 | {7E303E38-84E0-45B9-8580-3109479B1F49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 113 | {7E303E38-84E0-45B9-8580-3109479B1F49}.Debug|Any CPU.Build.0 = Debug|Any CPU 114 | {7E303E38-84E0-45B9-8580-3109479B1F49}.Release|Any CPU.ActiveCfg = Release|Any CPU 115 | {7E303E38-84E0-45B9-8580-3109479B1F49}.Release|Any CPU.Build.0 = Release|Any CPU 116 | {7F3A9BF2-137D-41AA-9346-4E117326850D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 117 | {7F3A9BF2-137D-41AA-9346-4E117326850D}.Debug|Any CPU.Build.0 = Debug|Any CPU 118 | {7F3A9BF2-137D-41AA-9346-4E117326850D}.Release|Any CPU.ActiveCfg = Release|Any CPU 119 | {7F3A9BF2-137D-41AA-9346-4E117326850D}.Release|Any CPU.Build.0 = Release|Any CPU 120 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 121 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC}.Debug|Any CPU.Build.0 = Debug|Any CPU 122 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC}.Release|Any CPU.ActiveCfg = Release|Any CPU 123 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC}.Release|Any CPU.Build.0 = Release|Any CPU 124 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 125 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Debug|Any CPU.Build.0 = Debug|Any CPU 126 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|Any CPU.ActiveCfg = Release|Any CPU 127 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|Any CPU.Build.0 = Release|Any CPU 128 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 129 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU 130 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU 131 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Release|Any CPU.Build.0 = Release|Any CPU 132 | {A3F748C6-13F4-4DE1-880B-73664F801EA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 133 | {A3F748C6-13F4-4DE1-880B-73664F801EA7}.Debug|Any CPU.Build.0 = Debug|Any CPU 134 | {A3F748C6-13F4-4DE1-880B-73664F801EA7}.Release|Any CPU.ActiveCfg = Release|Any CPU 135 | {A3F748C6-13F4-4DE1-880B-73664F801EA7}.Release|Any CPU.Build.0 = Release|Any CPU 136 | {C404876C-B04B-451F-B159-2C0CF8CB861B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 137 | {C404876C-B04B-451F-B159-2C0CF8CB861B}.Debug|Any CPU.Build.0 = Debug|Any CPU 138 | {C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|Any CPU.ActiveCfg = Release|Any CPU 139 | {C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|Any CPU.Build.0 = Release|Any CPU 140 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 141 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Debug|Any CPU.Build.0 = Debug|Any CPU 142 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|Any CPU.ActiveCfg = Release|Any CPU 143 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|Any CPU.Build.0 = Release|Any CPU 144 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 145 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612}.Debug|Any CPU.Build.0 = Debug|Any CPU 146 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612}.Release|Any CPU.ActiveCfg = Release|Any CPU 147 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612}.Release|Any CPU.Build.0 = Release|Any CPU 148 | {F214B483-E8BB-477D-807F-24F176B588D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 149 | {F214B483-E8BB-477D-807F-24F176B588D5}.Debug|Any CPU.Build.0 = Debug|Any CPU 150 | {F214B483-E8BB-477D-807F-24F176B588D5}.Release|Any CPU.ActiveCfg = Release|Any CPU 151 | {F214B483-E8BB-477D-807F-24F176B588D5}.Release|Any CPU.Build.0 = Release|Any CPU 152 | {F8ED6676-6701-4476-9606-4D150EF4A537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 153 | {F8ED6676-6701-4476-9606-4D150EF4A537}.Debug|Any CPU.Build.0 = Debug|Any CPU 154 | {F8ED6676-6701-4476-9606-4D150EF4A537}.Release|Any CPU.ActiveCfg = Release|Any CPU 155 | {F8ED6676-6701-4476-9606-4D150EF4A537}.Release|Any CPU.Build.0 = Release|Any CPU 156 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 157 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Debug|Any CPU.Build.0 = Debug|Any CPU 158 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Release|Any CPU.ActiveCfg = Release|Any CPU 159 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Release|Any CPU.Build.0 = Release|Any CPU 160 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 161 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4}.Debug|Any CPU.Build.0 = Debug|Any CPU 162 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4}.Release|Any CPU.ActiveCfg = Release|Any CPU 163 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4}.Release|Any CPU.Build.0 = Release|Any CPU 164 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 165 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29}.Debug|Any CPU.Build.0 = Debug|Any CPU 166 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29}.Release|Any CPU.ActiveCfg = Release|Any CPU 167 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29}.Release|Any CPU.Build.0 = Release|Any CPU 168 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 169 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698}.Debug|Any CPU.Build.0 = Debug|Any CPU 170 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698}.Release|Any CPU.ActiveCfg = Release|Any CPU 171 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698}.Release|Any CPU.Build.0 = Release|Any CPU 172 | EndGlobalSection 173 | GlobalSection(NestedProjects) = preSolution 174 | {A291BF92-4BE9-42DB-9414-8E09F04FD3F0} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 175 | {65459173-442A-4E0D-B758-3B9EDCE54D75} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 176 | {6007D4E1-1676-4FBC-816D-1B19F6829F22} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 177 | {2A37AC43-5F17-4774-B9BE-8D5008F41F43} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 178 | {9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 179 | {C404876C-B04B-451F-B159-2C0CF8CB861B} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 180 | {2823F21D-D474-447D-9C8B-74B29D0D9B35} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 181 | {F214B483-E8BB-477D-807F-24F176B588D5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 182 | {D728B5BD-4BE1-4B45-A0A3-08A878DAE254} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 183 | {58B97FDC-8D6B-48A6-A1D0-2EC686254CC5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 184 | {3A2CEFD2-07E1-41C1-B52F-F1801D277E32} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D} 185 | {FF8A73BD-7DCB-46C1-8F83-EE84D65320B4} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 186 | {92346BF9-D8E8-4D1E-A3B6-48EA49BD9ADC} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 187 | {040AFA71-3175-4C6C-8DB2-98810D3E98F4} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 188 | {598AAD5B-C38A-4AC8-89C5-14CAC60AE46E} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 189 | {14333E87-2DCC-42A7-91FF-2DFEABF2AA95} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 190 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 191 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 192 | {F8ED6676-6701-4476-9606-4D150EF4A537} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 193 | {7F3A9BF2-137D-41AA-9346-4E117326850D} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 194 | {A3F748C6-13F4-4DE1-880B-73664F801EA7} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 195 | {7E303E38-84E0-45B9-8580-3109479B1F49} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 196 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 197 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E} 198 | {FA5CB853-0E45-4A38-9936-D68BB88B3B29} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 199 | {15F148FB-F4A1-4E0D-82B2-047F63ED5698} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C} 200 | EndGlobalSection 201 | GlobalSection(SolutionProperties) = preSolution 202 | HideSolutionNode = FALSE 203 | EndGlobalSection 204 | EndGlobal 205 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CSharp Design Patterns 2 | 3 | 4 | [![Powered by DartNode](https://dartnode.com/branding/DN-Open-Source-sm.png)](https://dartnode.com "Powered by DartNode - Free VPS for Open Source") 5 | -------------------------------------------------------------------------------- /Structural/Adapter/Adaptee.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Adapter { 4 | 5 | class Adaptee { 6 | 7 | public void PerformRequest() { 8 | Console.WriteLine("Addaptee perform request."); 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Structural/Adapter/Adapter.cs: -------------------------------------------------------------------------------- 1 | namespace Adapter { 2 | 3 | class Adapter : ITarget { 4 | 5 | private readonly Adaptee _adaptee; 6 | 7 | public Adapter(Adaptee adaptee) { 8 | this._adaptee = adaptee; 9 | } 10 | 11 | void ITarget.Request() { 12 | this._adaptee.PerformRequest(); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Structural/Adapter/Adapter.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0} 9 | Exe 10 | Properties 11 | Adapter 12 | Adapter 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 50 | -------------------------------------------------------------------------------- /Structural/Adapter/ITarget.cs: -------------------------------------------------------------------------------- 1 | namespace Adapter { 2 | 3 | interface ITarget { 4 | 5 | void Request(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Structural/Adapter/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Adapter { 7 | class Program { 8 | 9 | static void Main(string[] args) { 10 | ITarget target = new Adapter(new Adaptee()); 11 | target.Request(); 12 | 13 | Console.ReadLine(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Structural/Adapter/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Adapter")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Adapter")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c245f461-c21b-4eb6-a7be-291d08c4047d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Bridge/Bridge.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {FECD9206-68C5-4F31-A7AB-AD876E05ABDA} 9 | Exe 10 | Properties 11 | Bridge 12 | Bridge 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 52 | -------------------------------------------------------------------------------- /Structural/Bridge/CircleShape.cs: -------------------------------------------------------------------------------- 1 | namespace Bridge { 2 | 3 | class CircleShape : Shape { 4 | 5 | private readonly double _x; 6 | private readonly double _y; 7 | private double _radius; 8 | 9 | public CircleShape(double x, double y, double radius, IDrawingApi drawingApi) 10 | : base(drawingApi) { 11 | this._x = x; 12 | this._y = y; 13 | this._radius = radius; 14 | } 15 | 16 | public override void Draw() { 17 | this.DrawingApi.DrawCircle(this._x, this._y, this._radius); 18 | } 19 | 20 | public override void ResizeByPercent(double percent) { 21 | this._radius *= percent; 22 | } 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Structural/Bridge/DrawingApi1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bridge { 4 | 5 | class DrawingApi1 : IDrawingApi { 6 | 7 | public void DrawCircle(double x, double y, double radius) { 8 | Console.WriteLine("API1.circle at {0},{1} radius {2}", x, y, radius); 9 | } 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /Structural/Bridge/DrawingApi2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bridge { 4 | 5 | class DrawingApi2 : IDrawingApi { 6 | 7 | public void DrawCircle(double x, double y, double radius) { 8 | Console.WriteLine("API2.circle at {0},{1} radius {2}", x, y, radius); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Structural/Bridge/IDrawingApi.cs: -------------------------------------------------------------------------------- 1 | namespace Bridge { 2 | 3 | interface IDrawingApi { 4 | 5 | void DrawCircle(double x, double y, double radius); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Structural/Bridge/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Bridge { 7 | 8 | class Program { 9 | 10 | static void Main(string[] args) { 11 | 12 | var shapes = new Shape[] { 13 | new CircleShape(1, 2, 3, new DrawingApi1()), 14 | new CircleShape(5, 7, 11, new DrawingApi2()), 15 | }; 16 | 17 | foreach (var shape in shapes) { 18 | shape.ResizeByPercent(2.5); 19 | shape.Draw(); 20 | } 21 | 22 | Console.ReadKey(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Structural/Bridge/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Bridge")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Bridge")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("a0448aef-795d-4394-ad5b-f6329481a579")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Bridge/Shape.cs: -------------------------------------------------------------------------------- 1 | namespace Bridge { 2 | 3 | abstract class Shape { 4 | 5 | protected IDrawingApi DrawingApi; 6 | 7 | protected Shape(IDrawingApi drawingApi) { 8 | this.DrawingApi = drawingApi; 9 | } 10 | 11 | public abstract void Draw(); 12 | 13 | public abstract void ResizeByPercent(double percent); 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /Structural/Composite/Composite.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {F8ED6676-6701-4476-9606-4D150EF4A537} 9 | Exe 10 | Properties 11 | Composite 12 | Composite 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 50 | -------------------------------------------------------------------------------- /Structural/Composite/CompositeGraphic.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Composite { 4 | 5 | class CompositeGraphic : IGraphic { 6 | 7 | private readonly IList _childGraphics = new List(); 8 | 9 | public void Print() { 10 | foreach (var childGraphic in this._childGraphics) { 11 | childGraphic.Print(); 12 | } 13 | } 14 | 15 | public void Add(IGraphic graphic) { 16 | this._childGraphics.Add(graphic); 17 | } 18 | 19 | public void Remove(IGraphic graphic) { 20 | this._childGraphics.Remove(graphic); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /Structural/Composite/Ellipse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Composite { 4 | 5 | class Ellipse : IGraphic { 6 | 7 | public void Print() { 8 | Console.WriteLine("Ellipse"); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /Structural/Composite/IGraphic.cs: -------------------------------------------------------------------------------- 1 | namespace Composite { 2 | 3 | interface IGraphic { 4 | 5 | void Print(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Structural/Composite/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Composite { 4 | 5 | class Program { 6 | 7 | static void Main(string[] args) { 8 | var ellipse1 = new Ellipse(); 9 | var ellipse2 = new Ellipse(); 10 | var ellipse3 = new Ellipse(); 11 | var ellipse4 = new Ellipse(); 12 | 13 | var graphic = new CompositeGraphic(); 14 | var graphic1 = new CompositeGraphic(); 15 | var graphic2 = new CompositeGraphic(); 16 | 17 | graphic1.Add(ellipse1); 18 | graphic1.Add(ellipse2); 19 | graphic1.Add(ellipse3); 20 | 21 | graphic2.Add(ellipse4); 22 | 23 | graphic.Add(graphic1); 24 | graphic.Add(graphic2); 25 | 26 | graphic.Print(); 27 | 28 | Console.ReadKey(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Structural/Composite/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Composite")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Composite")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d78ae913-063c-4985-8610-b9520f267348")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Decorator/Decorator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {7F3A9BF2-137D-41AA-9346-4E117326850D} 9 | Exe 10 | Properties 11 | Decorator 12 | Decorator 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 52 | -------------------------------------------------------------------------------- /Structural/Decorator/HorizontalScrollbarWindow.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator { 2 | 3 | class HorizontalScrollbarWindow : WindowDecorator { 4 | 5 | public HorizontalScrollbarWindow(IWindow decoratedWindow) : base(decoratedWindow) { 6 | } 7 | 8 | public override void Draw() { 9 | base.Draw(); 10 | this.DrawHorizontalScrollbar(); 11 | } 12 | 13 | private void DrawHorizontalScrollbar() { 14 | // 15 | } 16 | 17 | public override string GetDescription() { 18 | return this.DecoratedWindow.GetDescription() + ", include horizontal scrollbars"; 19 | } 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Structural/Decorator/IWindow.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator { 2 | 3 | interface IWindow { 4 | 5 | void Draw(); 6 | 7 | string GetDescription(); 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Structural/Decorator/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Decorator { 4 | 5 | class Program { 6 | 7 | static void Main(string[] args) { 8 | IWindow window = new HorizontalScrollbarWindow(new VerticalScrollbarWindow(new SimpleWindow())); 9 | Console.WriteLine(window.GetDescription()); 10 | 11 | Console.ReadKey(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Structural/Decorator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Decorator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Decorator")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("80658378-6937-4a74-ae5b-69d472906587")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Decorator/SimpleWindow.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator { 2 | 3 | class SimpleWindow : IWindow { 4 | 5 | public void Draw() { 6 | // 7 | } 8 | 9 | public string GetDescription() { 10 | return "Simple window"; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Structural/Decorator/VerticalScrollbarWindow.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator { 2 | 3 | class VerticalScrollbarWindow : WindowDecorator { 4 | 5 | public VerticalScrollbarWindow(IWindow decoratedWindow) : base(decoratedWindow) { 6 | } 7 | 8 | public override void Draw() { 9 | base.Draw(); 10 | this.DrawVerticalScrollbar(); 11 | } 12 | 13 | private void DrawVerticalScrollbar() { 14 | // 15 | } 16 | 17 | public override string GetDescription() { 18 | return this.DecoratedWindow.GetDescription() + ", include vertical scrollbars"; 19 | } 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Structural/Decorator/WindowDecorator.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator { 2 | 3 | class WindowDecorator : IWindow { 4 | 5 | protected IWindow DecoratedWindow; 6 | 7 | public WindowDecorator(IWindow decoratedWindow) { 8 | this.DecoratedWindow = decoratedWindow; 9 | } 10 | 11 | public virtual void Draw() { 12 | this.DecoratedWindow.Draw(); 13 | } 14 | 15 | public virtual string GetDescription() { 16 | return "Window decorator"; 17 | } 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /Structural/Facade/Computer.cs: -------------------------------------------------------------------------------- 1 | namespace Facade { 2 | 3 | class Computer { 4 | 5 | private readonly Cpu _cpu; 6 | private readonly Memory _memory; 7 | private readonly HardDrive _hardDrive; 8 | 9 | private const long BootAddress = 0; 10 | 11 | public Computer() { 12 | this._cpu = new Cpu(); 13 | this._memory = new Memory(); 14 | this._hardDrive = new HardDrive(); 15 | } 16 | 17 | public void StartComputer() { 18 | this._cpu.Freeze(); 19 | this._memory.Load(BootAddress, this._hardDrive.Read(BootAddress)); 20 | this._cpu.Execute(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Structural/Facade/Cpu.cs: -------------------------------------------------------------------------------- 1 | namespace Facade { 2 | 3 | class Cpu { 4 | 5 | public void Freeze() { 6 | } 7 | 8 | public void Jump(long position) { 9 | } 10 | 11 | public void Execute() { 12 | } 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Structural/Facade/Facade.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {A3F748C6-13F4-4DE1-880B-73664F801EA7} 9 | Exe 10 | Properties 11 | Facade 12 | Facade 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | -------------------------------------------------------------------------------- /Structural/Facade/HardDrive.cs: -------------------------------------------------------------------------------- 1 | namespace Facade { 2 | 3 | class HardDrive { 4 | 5 | public byte[] Read(long address) { 6 | return new byte[0]; 7 | } 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Structural/Facade/Memory.cs: -------------------------------------------------------------------------------- 1 | namespace Facade { 2 | 3 | class Memory { 4 | 5 | public void Load(long address, byte[] data) { 6 | } 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /Structural/Facade/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Facade { 2 | 3 | class Program { 4 | 5 | static void Main(string[] args) { 6 | var facade = new Computer(); 7 | facade.StartComputer(); 8 | } 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Structural/Facade/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Facade")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Facade")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("19b69e2a-36b9-42ba-bb2d-1e6770e448e7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Flyweight/CoffeeFlavor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Flyweight { 4 | 5 | public class CoffeeFlavor : ICoffeeOrder { 6 | 7 | public string Flavor { 8 | get; 9 | private set; 10 | } 11 | 12 | public CoffeeFlavor(string newFlavor) { 13 | this.Flavor = newFlavor; 14 | } 15 | 16 | public void ServeCoffee(CoffeeOrderContext context) { 17 | Console.WriteLine("Serving coffee flavor {0} to table {1} .", this.Flavor, context.Table); 18 | } 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /Structural/Flyweight/CoffeeFlavorFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Flyweight { 4 | 5 | public class CoffeeFlavorFactory { 6 | 7 | private readonly IDictionary _flavors = new Dictionary(); 8 | 9 | public CoffeeFlavor GetCoffeeFlavor(string flavorName) { 10 | CoffeeFlavor flavor; 11 | if (this._flavors.TryGetValue(flavorName, out flavor)) { 12 | return flavor; 13 | } 14 | flavor = new CoffeeFlavor(flavorName); 15 | this._flavors.Add(flavorName, flavor); 16 | return flavor; 17 | } 18 | 19 | public int TotalFlaversMade { 20 | get { 21 | return this._flavors.Count; 22 | } 23 | } 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Structural/Flyweight/CoffeeOrderContext.cs: -------------------------------------------------------------------------------- 1 | namespace Flyweight { 2 | 3 | public class CoffeeOrderContext { 4 | 5 | public int Table { 6 | get; 7 | private set; 8 | } 9 | 10 | public CoffeeOrderContext(int tableNumber) { 11 | this.Table = tableNumber; 12 | } 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /Structural/Flyweight/Flyweight.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {7E303E38-84E0-45B9-8580-3109479B1F49} 9 | Exe 10 | Properties 11 | Flyweight 12 | Flyweight 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | -------------------------------------------------------------------------------- /Structural/Flyweight/ICoffeeOrder.cs: -------------------------------------------------------------------------------- 1 | namespace Flyweight { 2 | 3 | public interface ICoffeeOrder { 4 | 5 | void ServeCoffee(CoffeeOrderContext context); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Structural/Flyweight/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Flyweight { 4 | 5 | class Program { 6 | 7 | private static readonly CoffeeFlavor[] Flavors = new CoffeeFlavor[100]; 8 | private static readonly CoffeeOrderContext[] Tables = new CoffeeOrderContext[100]; 9 | 10 | private static int _ordersMade; 11 | private static CoffeeFlavorFactory _factory; 12 | 13 | private static void TakeOrder(string flaver, int table) { 14 | Flavors[_ordersMade] = _factory.GetCoffeeFlavor(flaver); 15 | Tables[_ordersMade++] = new CoffeeOrderContext(table); 16 | } 17 | 18 | static void Main(string[] args) { 19 | _factory = new CoffeeFlavorFactory(); 20 | 21 | TakeOrder("Cappuccino", 2); 22 | TakeOrder("Cappuccino", 2); 23 | TakeOrder("Frappe", 1); 24 | TakeOrder("Frappe", 1); 25 | TakeOrder("Xpresso", 1); 26 | TakeOrder("Frappe", 897); 27 | TakeOrder("Cappuccino", 97); 28 | TakeOrder("Cappuccino", 97); 29 | TakeOrder("Frappe", 3); 30 | TakeOrder("Xpresso", 3); 31 | TakeOrder("Cappuccino", 3); 32 | TakeOrder("Xpresso", 96); 33 | TakeOrder("Frappe", 552); 34 | TakeOrder("Cappuccino", 121); 35 | TakeOrder("Xpresso", 121); 36 | 37 | for (int i = 0; i < _ordersMade; i++) { 38 | Flavors[i].ServeCoffee(Tables[i]); 39 | } 40 | 41 | Console.WriteLine(); 42 | Console.WriteLine("Total CoffeeFlavor objects made {0}", _factory.TotalFlaversMade); 43 | Console.ReadKey(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Structural/Flyweight/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Flyweight")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Flyweight")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2043b2a6-efc1-41de-8f0d-fe176f989983")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Proxy/IImage.cs: -------------------------------------------------------------------------------- 1 | namespace Proxy { 2 | 3 | public interface IImage { 4 | 5 | void DisplayImage(); 6 | 7 | } 8 | } -------------------------------------------------------------------------------- /Structural/Proxy/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Proxy { 7 | 8 | class Program { 9 | 10 | static void Main(string[] args) { 11 | IImage image1 = new ProxyImage("HiRes_10MB_Photo1"); 12 | IImage image2 = new ProxyImage("HiRes_10MB_Photo2"); 13 | 14 | image1.DisplayImage(); 15 | image1.DisplayImage(); 16 | image2.DisplayImage(); 17 | image2.DisplayImage(); 18 | 19 | Console.ReadKey(); 20 | } 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Structural/Proxy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Proxy")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Proxy")] 13 | [assembly: AssemblyCopyright("Copyright © 2012")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("80fb6245-65d0-44bd-ac22-d88276a9db91")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Structural/Proxy/Proxy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {D859AF5D-34D0-4D0A-8264-0C2AB88B6612} 9 | Exe 10 | Properties 11 | Proxy 12 | Proxy 13 | Client 14 | 512 15 | 16 | 17 | false 18 | bin\Debug 19 | 4 20 | 21 | 22 | false 23 | bin\Release 24 | 4 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 50 | -------------------------------------------------------------------------------- /Structural/Proxy/ProxyImage.cs: -------------------------------------------------------------------------------- 1 | namespace Proxy { 2 | 3 | public class ProxyImage : IImage { 4 | 5 | private RealImage _realImage; 6 | private readonly string _fileName; 7 | 8 | public ProxyImage(string fileName) { 9 | this._fileName = fileName; 10 | } 11 | 12 | public void DisplayImage() { 13 | if (this._realImage == null) { 14 | this._realImage = new RealImage(this._fileName); 15 | } 16 | this._realImage.DisplayImage(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Structural/Proxy/RealImage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Proxy { 4 | 5 | public class RealImage : IImage { 6 | 7 | private readonly string _fileName; 8 | 9 | public RealImage(string fileName) { 10 | this._fileName = fileName; 11 | this.LoadImageFromFile(); 12 | } 13 | 14 | private void LoadImageFromFile() { 15 | Console.WriteLine("Load image from file {0}", this._fileName); 16 | } 17 | 18 | public void DisplayImage() { 19 | Console.WriteLine("Displaying image {0}", this._fileName); 20 | } 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /Structural/Specification/AndSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Specification; 3 | 4 | namespace Specification { 5 | 6 | public class AndSpecification : CompositSpecification { 7 | 8 | readonly ISpecification x; 9 | readonly ISpecification y; 10 | 11 | public AndSpecification(ISpecification x, ISpecification y) { 12 | this.x = x; 13 | this.y = y; 14 | } 15 | 16 | public override bool IsSatisfiedBy(TTarget candidate) { 17 | return x.IsSatisfiedBy(candidate) && y.IsSatisfiedBy(candidate); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Structural/Specification/CompositSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Specification; 3 | 4 | namespace Specification { 5 | 6 | public abstract class CompositSpecification : ISpecification { 7 | 8 | public abstract bool IsSatisfiedBy(TTarget candidate); 9 | 10 | public ISpecification And(ISpecification specification) { 11 | return new AndSpecification(this, specification); 12 | } 13 | 14 | public ISpecification Or(ISpecification specification) { 15 | return new OrSpecification(this, specification); 16 | } 17 | 18 | public ISpecification Not(ISpecification specification) { 19 | return new NotSpecification(specification); 20 | } 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Structural/Specification/ExpressionSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Specification { 4 | 5 | public class ExpressionSpecification : CompositSpecification { 6 | 7 | readonly Func expression; 8 | 9 | public ExpressionSpecification(Func expression) { 10 | this.expression = expression; 11 | } 12 | 13 | public override bool IsSatisfiedBy(TTarget candidate) { 14 | return expression(candidate); 15 | } 16 | 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Structural/Specification/ISpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Specification; 3 | 4 | namespace Specification { 5 | 6 | public interface ISpecification { 7 | 8 | bool IsSatisfiedBy(TTarget candidate); 9 | 10 | ISpecification And(ISpecification specification); 11 | 12 | ISpecification Or(ISpecification specification); 13 | 14 | ISpecification Not(ISpecification specification); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Structural/Specification/NotSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Specification; 3 | 4 | namespace Specification { 5 | 6 | public class NotSpecification : CompositSpecification { 7 | 8 | readonly ISpecification x; 9 | 10 | public NotSpecification(ISpecification x) { 11 | this.x = x; 12 | } 13 | 14 | public override bool IsSatisfiedBy(TTarget candidate) { 15 | return !x.IsSatisfiedBy(candidate); 16 | } 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Structural/Specification/OrSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Specification; 3 | 4 | namespace Specification { 5 | 6 | public class OrSpecification : CompositSpecification { 7 | 8 | readonly ISpecification x; 9 | readonly ISpecification y; 10 | 11 | public OrSpecification(ISpecification x, ISpecification y) { 12 | this.x = x; 13 | this.y = y; 14 | } 15 | 16 | public override bool IsSatisfiedBy(TTarget candidate) { 17 | return x.IsSatisfiedBy(candidate) || y.IsSatisfiedBy(candidate); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Structural/Specification/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Specification { 5 | 6 | public class Program { 7 | 8 | public static void Main(string[] args) { 9 | var mobiles = new List() { 10 | new Mobile(MobileBrand.Apple, MobileType.Smart), 11 | new Mobile(MobileBrand.Samsung, MobileType.Smart), 12 | new Mobile(MobileBrand.Samsung, MobileType.Basic) 13 | }; 14 | 15 | var smartSpecification = new ExpressionSpecification(mobile => mobile.Type == MobileType.Smart); 16 | var appleSpecification = new ExpressionSpecification(mobile => mobile.Brand == MobileBrand.Apple); 17 | // find all smart mobiles; 18 | var smartMobiles = mobiles.FindAll(mobile => smartSpecification.IsSatisfiedBy(mobile)); 19 | // find all apple smart mobiles; 20 | var andSpecification = new AndSpecification(smartSpecification, appleSpecification); 21 | var appleSmartMobiles = mobiles.FindAll(mobile => andSpecification.IsSatisfiedBy(mobile)); 22 | 23 | Console.WriteLine("Hello World!"); 24 | 25 | Console.ReadKey(); 26 | } 27 | 28 | } 29 | 30 | class Mobile { 31 | 32 | public MobileBrand Brand { get; } 33 | public MobileType Type { get; } 34 | 35 | public Mobile(MobileBrand brand, MobileType type) { 36 | Brand = brand; 37 | Type = type; 38 | } 39 | } 40 | 41 | enum MobileBrand { 42 | Samsung, 43 | Apple 44 | } 45 | 46 | enum MobileType { 47 | Smart, Basic 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Structural/Specification/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle("Specification")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("")] 12 | [assembly: AssemblyCopyright("zhang")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | 28 | -------------------------------------------------------------------------------- /Structural/Specification/Specification.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {7C8D50CF-566A-4CDE-ACC3-3D36A06790E9} 9 | Exe 10 | Specification 11 | Specification 12 | v4.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG; 20 | prompt 21 | 4 22 | true 23 | 24 | 25 | full 26 | true 27 | bin\Release 28 | prompt 29 | 4 30 | true 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | --------------------------------------------------------------------------------