├── .gitignore ├── BehavioralPatterns ├── BehavioralPatterns.sln ├── ChainOfResponsibility │ ├── ChainOfResponsibility.Common │ │ ├── ChainOfResponsibility.Common.csproj │ │ ├── Constants.cs │ │ └── PrintMessages.cs │ ├── PurchaseApproval │ │ ├── Models │ │ │ ├── Approver.cs │ │ │ ├── President.cs │ │ │ ├── Purchase.cs │ │ │ ├── TeamLead.cs │ │ │ └── VicePresident.cs │ │ ├── Program.cs │ │ └── PurchaseApproval.csproj │ └── RestaurantComplaint │ │ ├── Enums │ │ └── Priority.cs │ │ ├── Models │ │ ├── Complaint.cs │ │ ├── Cook.cs │ │ ├── Employee.cs │ │ ├── Manager.cs │ │ └── Waiter.cs │ │ ├── Program.cs │ │ ├── RestaurantChain.cs │ │ └── RestaurantComplaint.csproj ├── Command │ ├── Command.Common │ │ ├── Command.Common.csproj │ │ ├── Constants.cs │ │ └── PrintMessages.cs │ └── LightSwitch │ │ ├── FlipDownCommand.cs │ │ ├── FlipUpCommand.cs │ │ ├── ICommand.cs │ │ ├── Light.cs │ │ ├── LightSwitch.csproj │ │ ├── Program.cs │ │ └── Switch.cs ├── Interpreter │ ├── ContextExpressions │ │ ├── Context.cs │ │ ├── Models │ │ │ ├── Expression.cs │ │ │ ├── HundredExpression.cs │ │ │ ├── OneExpression.cs │ │ │ ├── TenExpression.cs │ │ │ └── ThousandExpression.cs │ │ ├── Program.cs │ │ └── RomanNumerals.csproj │ └── Interpreter.Common │ │ ├── Constants.cs │ │ ├── Interpreter.Common.csproj │ │ └── PrintMessages.cs ├── Iterator │ ├── Iterator.Common │ │ ├── Iterator.Common.csproj │ │ └── PrintMessages.cs │ └── SequenceNumbers │ │ ├── Contracts │ │ ├── IIterator.cs │ │ └── ISequence.cs │ │ ├── Models │ │ ├── Sequence.cs │ │ └── SequenceIterator.cs │ │ ├── Program.cs │ │ └── SequenceNumbers.csproj ├── Mediator │ ├── Kitchen │ │ ├── IKitchenMediator.cs │ │ ├── Kitchen.csproj │ │ ├── KitchenMediator.cs │ │ ├── Models │ │ │ ├── Cook.cs │ │ │ ├── Employee.cs │ │ │ └── Waiter.cs │ │ └── Program.cs │ ├── Mediator.Common │ │ ├── Constants.cs │ │ ├── Mediator.Common.csproj │ │ └── PrintMessages.cs │ └── Messaging │ │ ├── AbstractChatRoom.cs │ │ ├── ChatRoom.cs │ │ ├── Messaging.csproj │ │ ├── Models │ │ ├── ConcreteParticipant.cs │ │ └── Participant.cs │ │ └── Program.cs ├── Memento │ ├── Memento.Common │ │ ├── Memento.Common.csproj │ │ └── PrintMessages.cs │ └── ProspectState │ │ ├── Program.cs │ │ ├── ProspectMemento.cs │ │ ├── ProspectMemory.cs │ │ ├── ProspectState.csproj │ │ └── SalesProspect.cs ├── Observer │ ├── InvestorStocks │ │ ├── InvestorStocks.csproj │ │ ├── ObserverModels │ │ │ ├── IInvestor.cs │ │ │ └── Investor.cs │ │ ├── Program.cs │ │ └── SubjectModels │ │ │ ├── IBM.cs │ │ │ └── Stock.cs │ └── Observer.Common │ │ ├── Constants.cs │ │ ├── Observer.Common.csproj │ │ └── PrintMessages.cs ├── State │ ├── AccountState │ │ ├── Account.cs │ │ ├── AccountState.csproj │ │ ├── Models │ │ │ ├── AccountState.cs │ │ │ ├── GoldState.cs │ │ │ ├── RedState.cs │ │ │ └── SilverState.cs │ │ └── Program.cs │ └── State.Common │ │ ├── PrintMessages.cs │ │ └── State.Common.csproj ├── Strategy │ ├── SortTypes │ │ ├── Models │ │ │ ├── MergeSort.cs │ │ │ ├── QuickSort.cs │ │ │ ├── SortStrategy.cs │ │ │ └── SortedList.cs │ │ ├── Program.cs │ │ └── SortTypes.csproj │ └── Strategy.Common │ │ ├── PrintMessages.cs │ │ └── Strategy.Common.csproj ├── TemplateMethod │ ├── TeaHouse │ │ ├── Models │ │ │ ├── Coffee.cs │ │ │ ├── HotDrink.cs │ │ │ └── Tea.cs │ │ ├── Program.cs │ │ └── TeaHouse.csproj │ └── TemplateMethod.Common │ │ ├── PrintMessages.cs │ │ └── TemplateMethod.Common.csproj └── Visitor │ ├── EmployeeAdministration │ ├── ElementModels │ │ ├── Clerk.cs │ │ ├── Director.cs │ │ ├── Element.cs │ │ ├── Employee.cs │ │ └── President.cs │ ├── EmployeeAdministration.csproj │ ├── Program.cs │ └── VisitorModels │ │ ├── IVisitor.cs │ │ ├── IncomeVisitor.cs │ │ └── VacationVisitor.cs │ └── Visitor.Common │ ├── PrintMessages.cs │ └── Visitor.Common.csproj ├── CreationalPatterns ├── AbstractFactory │ ├── AbstractFactory.Common │ │ ├── AbstractFactory.Common.csproj │ │ ├── Constants.cs │ │ └── PrintMessages.cs │ ├── CoffeeShop │ │ ├── AbstractProductModels │ │ │ ├── Coffee.cs │ │ │ ├── MilkCoffee.cs │ │ │ └── PlainCoffee.cs │ │ ├── CoffeeShop.csproj │ │ ├── Factories │ │ │ ├── CoffeeFactory.cs │ │ │ ├── FrenchCoffeeFactory.cs │ │ │ └── ItalianCoffeeFactory.cs │ │ ├── ProductModels │ │ │ ├── Cappuccino.cs │ │ │ ├── DoubleCoffee.cs │ │ │ ├── Macchiato.cs │ │ │ └── RegularCoffee.cs │ │ └── Program.cs │ └── ContinentAnimals │ │ ├── AbstractProductModels │ │ ├── Carnivore.cs │ │ └── Herbivore.cs │ │ ├── ContinentAnimals.csproj │ │ ├── Factories │ │ ├── AfricaFactory.cs │ │ ├── AmericaFactory.cs │ │ └── ContinentFactory.cs │ │ ├── ProductModels │ │ ├── Bison.cs │ │ ├── Lion.cs │ │ ├── Wildbeast.cs │ │ └── Wolf.cs │ │ └── Program.cs ├── Builder │ ├── Builder.Common │ │ ├── Builder.Common.csproj │ │ ├── Constants.cs │ │ └── PrintMessages.cs │ ├── CoffeeShop │ │ ├── Builders │ │ │ ├── FrenchBreakfastBuilder.cs │ │ │ ├── IMenuBuilder.cs │ │ │ └── ItalianBreakfastBuilder.cs │ │ ├── CoffeeShop.csproj │ │ ├── Menu.cs │ │ ├── Program.cs │ │ └── Shop.cs │ └── VehicleShop │ │ ├── Builders │ │ ├── CarBuilder.cs │ │ ├── MotorcycleBuilder.cs │ │ ├── ScooterBuilder.cs │ │ └── VehicleBuilder.cs │ │ ├── Program.cs │ │ ├── Shop.cs │ │ ├── Vehicle.cs │ │ └── VehicleShop.csproj ├── CreationalPatterns.sln ├── Factory │ ├── Factory.Common │ │ ├── Constants.cs │ │ ├── Factory.Common.csproj │ │ └── PrintMessages.cs │ └── PizzaRestaurant │ │ ├── Factories │ │ ├── CheesePizzaFactory.cs │ │ ├── HawaiPizzaFactory.cs │ │ ├── PepperoniPizzaFactory.cs │ │ └── PizzaFactory.cs │ │ ├── Models │ │ ├── CheesePizza.cs │ │ ├── HawaiPizza.cs │ │ ├── PepperoniPizza.cs │ │ └── Pizza.cs │ │ ├── PizzaRestaurant.csproj │ │ └── Program.cs ├── FactoryMethod │ ├── DocumentTypes │ │ ├── CreatorModels │ │ │ ├── CV.cs │ │ │ ├── Document.cs │ │ │ └── Report.cs │ │ ├── DocumentTypes.csproj │ │ ├── ProductModels │ │ │ ├── BioPage.cs │ │ │ ├── Page.cs │ │ │ ├── ResultsPage.cs │ │ │ ├── SkillsPage.cs │ │ │ └── SummaryPage.cs │ │ └── Program.cs │ └── FactoryMethod.Common │ │ ├── FactoryMethod.Common.csproj │ │ └── PrintMessages.cs ├── Prototype │ ├── ColorCloning │ │ ├── ColorCloning.csproj │ │ ├── ColorManager.cs │ │ ├── Models │ │ │ ├── Color.cs │ │ │ └── ColorPrototype.cs │ │ └── Program.cs │ └── Prototype.Common │ │ ├── Constants.cs │ │ ├── PrintMessages.cs │ │ └── Prototype.Common.csproj ├── SimpleFactory │ ├── CoffeeShop │ │ ├── CoffeeShop.csproj │ │ ├── Enums │ │ │ └── CoffeeType.cs │ │ ├── Factories │ │ │ └── CoffeeFactory.cs │ │ ├── Models │ │ │ ├── Cappuccino.cs │ │ │ ├── Coffee.cs │ │ │ ├── DoubleCoffee.cs │ │ │ ├── Macchiato.cs │ │ │ └── RegularCoffee.cs │ │ └── Program.cs │ ├── PizzaRestaurant │ │ ├── Enums │ │ │ └── PizzaType.cs │ │ ├── Factories │ │ │ └── PizzaFactory.cs │ │ ├── Models │ │ │ ├── CheesePizza.cs │ │ │ ├── HawaiPizza.cs │ │ │ ├── PepperoniPizza.cs │ │ │ └── Pizza.cs │ │ ├── PizzaRestaurant.csproj │ │ └── Program.cs │ └── SimpleFactory.Common │ │ ├── Constants.cs │ │ ├── PrintMessages.cs │ │ └── SimpleFactory.Common.csproj └── Singleton │ ├── Instance │ ├── Instance.csproj │ ├── Program.cs │ └── SingletonInstance.cs │ ├── Logger │ ├── Log.cs │ ├── Logger.cs │ ├── Logger.csproj │ └── Program.cs │ └── Singleton.Common │ ├── Constants.cs │ ├── PrintMessages.cs │ └── Singleton.Common.csproj ├── LICENSE ├── README.md └── StructuralPatterns ├── Adapter ├── Adapter.Common │ ├── Adapter.Common.csproj │ ├── Constants.cs │ ├── PrintMessages.cs │ └── StringExtensions.cs ├── Cafeteria │ ├── Cafeteria.csproj │ ├── CoffeeShop.cs │ ├── DessertStorage.cs │ ├── DessertsAdapter.cs │ ├── Models │ │ ├── Dessert.cs │ │ └── DessertInfo.cs │ └── Program.cs └── ChemicalCompounds │ ├── ChemicalCompounds.csproj │ ├── ChemicalDatabank.cs │ ├── ICompound.cs │ ├── Program.cs │ └── RichCompound.cs ├── Bridge ├── Bridge.Common │ ├── Bridge.Common.csproj │ └── Constants.cs └── ManuscriptMaker │ ├── AbstractionModels │ ├── Book.cs │ ├── Document.cs │ └── FAQ.cs │ ├── ImplementatorModels │ ├── FancyFormatter.cs │ ├── IFormatter.cs │ └── StandardFormatter.cs │ ├── ManuscriptMaker.csproj │ └── Program.cs ├── Composite ├── Composite.Common │ ├── Composite.Common.csproj │ ├── Constants.cs │ └── PrintMessages.cs └── EmployeeITStructure │ ├── Contractor.cs │ ├── Employee.cs │ ├── EmployeeITStructure.csproj │ ├── IEmployee.cs │ └── Program.cs ├── Decorator ├── Decorator.Common │ ├── Constants.cs │ ├── Decorator.Common.csproj │ ├── ErrorMessages.cs │ └── PrintMessages.cs └── ItemRent │ ├── ComponentModels │ ├── Book.cs │ ├── LibraryItem.cs │ └── Video.cs │ ├── DecoratorModels │ ├── BorrowableLibraryItem.cs │ └── LibraryItemDecorator.cs │ ├── ItemRent.csproj │ └── Program.cs ├── Facade ├── Bar │ ├── Bar.csproj │ ├── BarFacade.cs │ ├── Models │ │ ├── Calculator.cs │ │ ├── Drink.cs │ │ └── Order.cs │ └── Program.cs ├── Facade.Common │ ├── Constants.cs │ ├── Facade.Common.csproj │ ├── PrintMessages.cs │ └── StringExtensions.cs └── Garage │ ├── Garage.csproj │ ├── GarageFacade.cs │ ├── Models │ ├── CarAccessories.cs │ ├── CarBody.cs │ ├── CarEngine.cs │ └── CarModel.cs │ └── Program.cs ├── Flyweight ├── CharacterPointSize │ ├── CharacterFactory.cs │ ├── CharacterPointSize.csproj │ ├── Models │ │ ├── Character.cs │ │ ├── CharacterA.cs │ │ ├── CharacterB.cs │ │ └── CharacterZ.cs │ └── Program.cs └── Flyweight.Common │ ├── Constants.cs │ ├── Flyweight.Common.csproj │ └── PrintMessages.cs ├── Proxy ├── MathProxy │ ├── IMath.cs │ ├── Math.cs │ ├── MathProxy.cs │ ├── MathProxy.csproj │ └── Program.cs └── Proxy.Common │ ├── PrintMessages.cs │ └── Proxy.Common.csproj └── StructuralPatterns.sln /BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibility.Common/ChainOfResponsibility.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibility.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibility.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayMacchiato = "Macchiato"; 6 | public const string DisplayRegularCoffee = "Regular Coffee"; 7 | public const string DisplayCappuccino = "Cappuccino"; 8 | public const string DisplayDoubleCoffee = "Double Coffee"; 9 | 10 | public const int ContentZeroMl = 0; 11 | public const int ContentHundredMl = 100; 12 | public const int ContentTwoHundredMl = 200; 13 | public const int ObjectsCount = 10000; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibility.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibility.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string ComplaintForm = "--- Complaint Form ---"; 6 | public const string CustomerName = "Customer Name: "; 7 | public const string EnterPriority = "Enter priority number (1-Low, 2-Medium, 3-High, 4-Crutial) : "; 8 | 9 | public const string ApproveRequestTemplate = "{0} approved request #{1}"; 10 | public const string MeetingRequired = "Request #{0} requires an executive meeting!"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Models/Approver.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval.Models 2 | { 3 | /// 4 | /// The 'Handler' abstract class 5 | /// 6 | internal abstract class Approver 7 | { 8 | public void SetNext(Approver next) 9 | { 10 | this.Next = next; 11 | } 12 | 13 | protected Approver Next { get; set; } 14 | 15 | public abstract void ProcessRequest(Purchase purchase); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Models/President.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval.Models 2 | { 3 | using System; 4 | 5 | using ChainOfResponsibility.Common; 6 | 7 | /// 8 | /// The 'ConcreteHandler' class 9 | /// 10 | internal class President : Approver 11 | { 12 | public override void ProcessRequest(Purchase purchase) 13 | { 14 | if (purchase.Amount < 100000) 15 | { 16 | Console.WriteLine(PrintMessages.ApproveRequestTemplate, nameof(President), purchase.Number); 17 | } 18 | else if (base.Next != null) 19 | { 20 | base.Next.ProcessRequest(purchase); 21 | } 22 | else 23 | { 24 | Console.WriteLine(PrintMessages.MeetingRequired, purchase.Number); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Models/Purchase.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval.Models 2 | { 3 | /// 4 | /// Class holding request details 5 | /// 6 | public class Purchase 7 | { 8 | public Purchase(decimal amount, int number) 9 | { 10 | this.Amount = amount; 11 | this.Number = number; 12 | } 13 | 14 | public decimal Amount { get; private set; } 15 | 16 | public int Number { get; set; } 17 | } 18 | } -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Models/TeamLead.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval.Models 2 | { 3 | using System; 4 | 5 | using ChainOfResponsibility.Common; 6 | 7 | /// 8 | /// The 'ConcreteHandler' class 9 | /// 10 | internal class TeamLead : Approver 11 | { 12 | public override void ProcessRequest(Purchase purchase) 13 | { 14 | if (purchase.Amount < 10000) 15 | { 16 | Console.WriteLine(PrintMessages.ApproveRequestTemplate, nameof(TeamLead), purchase.Number); 17 | } 18 | else if (base.Next != null) 19 | { 20 | base.Next.ProcessRequest(purchase); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Models/VicePresident.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval.Models 2 | { 3 | using System; 4 | 5 | using ChainOfResponsibility.Common; 6 | 7 | /// 8 | /// The 'ConcreteHandler' class 9 | /// 10 | internal class VicePresident : Approver 11 | { 12 | public override void ProcessRequest(Purchase purchase) 13 | { 14 | if (purchase.Amount < 25000) 15 | { 16 | Console.WriteLine(PrintMessages.ApproveRequestTemplate, nameof(VicePresident), purchase.Number); 17 | } 18 | else if (base.Next != null) 19 | { 20 | base.Next.ProcessRequest(purchase); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/Program.cs: -------------------------------------------------------------------------------- 1 | namespace PurchaseApproval 2 | { 3 | using PurchaseApproval.Models; 4 | 5 | public class Program 6 | { 7 | public static void Main() 8 | { 9 | Approver teamLead = new TeamLead(); 10 | Approver vicePresident = new VicePresident(); 11 | Approver president = new President(); 12 | 13 | teamLead.SetNext(vicePresident); 14 | vicePresident.SetNext(president); 15 | 16 | var purchase = new Purchase(350, 1); 17 | teamLead.ProcessRequest(purchase); 18 | 19 | purchase = new Purchase(24000, 2); 20 | teamLead.ProcessRequest(purchase); 21 | 22 | purchase = new Purchase(32590, 3); 23 | teamLead.ProcessRequest(purchase); 24 | 25 | purchase = new Purchase(100001, 4); 26 | teamLead.ProcessRequest(purchase); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/PurchaseApproval/PurchaseApproval.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Enums/Priority.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Enums 2 | { 3 | public enum Priority 4 | { 5 | Low = 1, 6 | Medium, 7 | High, 8 | Crutial, 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Models/Complaint.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Models 2 | { 3 | using RestaurantComplaint.Enums; 4 | 5 | /// 6 | /// Class holding request details 7 | /// 8 | public class Complaint 9 | { 10 | public string CuromerName { get; set; } 11 | 12 | public Priority Priority { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Models/Cook.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Models 2 | { 3 | using RestaurantComplaint.Enums; 4 | 5 | /// 6 | /// The 'ConcreteHandler' class 7 | /// 8 | public class Cook : Employee 9 | { 10 | public Cook(Priority level) 11 | { 12 | this.level = level; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Models/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Models 2 | { 3 | using System; 4 | 5 | using RestaurantComplaint.Enums; 6 | 7 | /// 8 | /// The 'Handler' abstract class 9 | /// 10 | public abstract class Employee 11 | { 12 | protected Priority level; 13 | 14 | public void SetSuccessor(Employee successor) 15 | { 16 | this.Successor = successor; 17 | } 18 | 19 | public Employee Successor { get; private set; } 20 | 21 | public void ProcessComplaint(Complaint complaint) 22 | { 23 | if (complaint.Priority <= this.level) 24 | { 25 | Respond(complaint); 26 | } 27 | 28 | else if (this.Successor != null) 29 | { 30 | this.Successor.ProcessComplaint(complaint); 31 | } 32 | 33 | else 34 | { 35 | Console.WriteLine("Complaint cannot be handled!"); 36 | } 37 | } 38 | 39 | protected virtual void Respond(Complaint complaint) 40 | { 41 | Console.WriteLine("Mr/Mrs {0}, your complaint will be handled by the {1}", complaint.CuromerName, this.GetType().Name.ToString()); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Models/Manager.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Models 2 | { 3 | using RestaurantComplaint.Enums; 4 | 5 | /// 6 | /// The 'ConcreteHandler' class 7 | /// 8 | public class Manager : Employee 9 | { 10 | public Manager(Priority level) 11 | { 12 | this.level = level; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Models/Waiter.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint.Models 2 | { 3 | using RestaurantComplaint.Enums; 4 | 5 | /// 6 | /// The 'ConcreteHandler' class 7 | /// 8 | public class Waiter : Employee 9 | { 10 | public Waiter(Priority level) 11 | { 12 | this.level = level; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/Program.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint 2 | { 3 | using System; 4 | 5 | using ChainOfResponsibility.Common; 6 | using RestaurantComplaint.Enums; 7 | using RestaurantComplaint.Models; 8 | 9 | public class Program 10 | { 11 | public static void Main() 12 | { 13 | Complaint complaint = new(); 14 | Console.WriteLine(PrintMessages.ComplaintForm); 15 | 16 | Console.WriteLine(PrintMessages.CustomerName); 17 | complaint.CuromerName = Console.ReadLine(); 18 | 19 | Console.Write(PrintMessages.EnterPriority); 20 | complaint.Priority = (Priority)int.Parse(Console.ReadLine()); 21 | 22 | Employee employee = RestaurantChain.GetEmployee(); 23 | employee.ProcessComplaint(complaint); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/RestaurantChain.cs: -------------------------------------------------------------------------------- 1 | namespace RestaurantComplaint 2 | { 3 | using RestaurantComplaint.Enums; 4 | using RestaurantComplaint.Models; 5 | 6 | /// 7 | /// The 'Client' class 8 | /// 9 | public class RestaurantChain 10 | { 11 | public static Employee GetEmployee() 12 | { 13 | Employee waiter = new Waiter(Priority.Low); 14 | Employee cook = new Cook(Priority.Medium); 15 | Employee manager = new Manager(Priority.High); 16 | 17 | waiter.SetSuccessor(cook); 18 | cook.SetSuccessor(manager); 19 | 20 | return waiter; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /BehavioralPatterns/ChainOfResponsibility/RestaurantComplaint/RestaurantComplaint.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/Command.Common/Command.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/Command.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Command.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayOn = "ON"; 6 | public const string DisplayOff = "OFF"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/Command.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Command.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string EnterCommands = "Enter Commands (ON/OFF) : "; 6 | public const string RequiredCommand = "Command \"ON\" or \"OFF\" is required."; 7 | public const string LightOn = "The light is on!"; 8 | public const string LightOff = "The light is off!"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/FlipDownCommand.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | /// 4 | /// The Command for turning off the light - ConcreteCommand #2 5 | /// 6 | public class FlipDownCommand : ICommand 7 | { 8 | private readonly Light light; 9 | 10 | public FlipDownCommand(Light light) 11 | { 12 | this.light = light; 13 | } 14 | 15 | public void Execute() 16 | { 17 | light.TurnOff(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/FlipUpCommand.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | /// 4 | /// The Command for turning on the light - ConcreteCommand #1 5 | /// 6 | public class FlipUpCommand : ICommand 7 | { 8 | private readonly Light light; 9 | 10 | public FlipUpCommand(Light light) 11 | { 12 | this.light = light; 13 | } 14 | 15 | public void Execute() 16 | { 17 | light.TurnOn(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/ICommand.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | /// 4 | /// The 'Command' interface 5 | /// 6 | public interface ICommand 7 | { 8 | void Execute(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/Light.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | using System; 4 | 5 | using Command.Common; 6 | 7 | /// 8 | /// The 'Receiver' class 9 | /// 10 | public class Light 11 | { 12 | public bool TurnedOn { get; set; } 13 | 14 | public void TurnOn() 15 | { 16 | this.TurnedOn = true; 17 | Console.WriteLine(PrintMessages.LightOn); 18 | } 19 | 20 | public void TurnOff() 21 | { 22 | this.TurnedOn = false; 23 | Console.WriteLine(PrintMessages.LightOff); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/LightSwitch.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/Program.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | using System; 4 | 5 | using Command.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | Console.WriteLine(PrintMessages.EnterCommands); 12 | string command = Console.ReadLine().ToUpper(); 13 | 14 | Light lamp = new(); 15 | ICommand switchUp = new FlipUpCommand(lamp); 16 | ICommand switchDown = new FlipDownCommand(lamp); 17 | 18 | Switch s = new(); 19 | 20 | if (command == Constants.DisplayOn) 21 | { 22 | s.AddCommand(switchUp); 23 | s.ExecuteCommand(switchUp); 24 | } 25 | else if (command == Constants.DisplayOff) 26 | { 27 | s.AddCommand(switchDown); 28 | s.ExecuteCommand(switchDown); 29 | } 30 | else 31 | { 32 | Console.WriteLine(PrintMessages.RequiredCommand); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BehavioralPatterns/Command/LightSwitch/Switch.cs: -------------------------------------------------------------------------------- 1 | namespace LightSwitch 2 | { 3 | using System.Collections.Generic; 4 | 5 | /// 6 | /// The 'Invoker' class 7 | /// 8 | public class Switch 9 | { 10 | private readonly List commands = new(); 11 | 12 | public void AddCommand(ICommand command) 13 | { 14 | commands.Add(command); 15 | } 16 | 17 | public void ExecuteCommand(ICommand command) 18 | { 19 | command.Execute(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Context.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals 2 | { 3 | /// 4 | /// The 'Context' class 5 | /// 6 | public class Context 7 | { 8 | public Context(string input) 9 | { 10 | this.Input = input; 11 | } 12 | 13 | public string Input { get; set; } 14 | 15 | public int Output { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Models/Expression.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals.Models 2 | { 3 | /// 4 | /// The 'AbstractExpression' class 5 | /// 6 | public abstract class Expression 7 | { 8 | public abstract string One(); 9 | 10 | public abstract string Four(); 11 | 12 | public abstract string Five(); 13 | 14 | public abstract string Nine(); 15 | 16 | public abstract int Multiplier(); 17 | 18 | public void Interpret(Context context) 19 | { 20 | if (context.Input.Length == 0) 21 | { 22 | return; 23 | } 24 | 25 | if (context.Input.StartsWith(this.Nine())) 26 | { 27 | context.Output += 9 * Multiplier(); 28 | context.Input = context.Input.Substring(2); 29 | } 30 | else if (context.Input.StartsWith(this.Four())) 31 | { 32 | context.Output += 4 * Multiplier(); 33 | context.Input = context.Input.Substring(2); 34 | } 35 | else if (context.Input.StartsWith(this.Five())) 36 | { 37 | context.Output += 5 * Multiplier(); 38 | context.Input = context.Input.Substring(1); 39 | } 40 | 41 | while (context.Input.StartsWith(this.One())) 42 | { 43 | context.Output += 1 * Multiplier(); 44 | context.Input = context.Input.Substring(1); 45 | } 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Models/HundredExpression.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals.Models 2 | { 3 | /// 4 | /// The 'Concrete' expression class 5 | /// 6 | public class HundredExpression : Expression 7 | { 8 | public override string One() => "C"; 9 | 10 | public override string Four() => "CD"; 11 | 12 | public override string Five() => "D"; 13 | 14 | public override string Nine() => "CM"; 15 | 16 | public override int Multiplier() => 100; 17 | } 18 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Models/OneExpression.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals.Models 2 | { 3 | /// 4 | /// The 'Concrete' expression class 5 | /// 6 | public class OneExpression : Expression 7 | { 8 | public override string One() => "I"; 9 | 10 | public override string Four() => "IV"; 11 | 12 | public override string Five() => "V"; 13 | 14 | public override string Nine() => "IX"; 15 | 16 | public override int Multiplier() => 1; 17 | } 18 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Models/TenExpression.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals.Models 2 | { 3 | /// 4 | /// The 'Concrete' expression class 5 | /// 6 | public class TenExpression : Expression 7 | { 8 | public override string One() => "X"; 9 | 10 | public override string Four() => "XL"; 11 | 12 | public override string Five() => "L"; 13 | 14 | public override string Nine() => "XC"; 15 | 16 | public override int Multiplier() => 10; 17 | } 18 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Models/ThousandExpression.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals.Models 2 | { 3 | using Interpreter.Common; 4 | 5 | internal class ThousandExpression : Expression 6 | { 7 | public override string One() => "M"; 8 | 9 | public override string Four() => PrintMessages.NotSupported; 10 | 11 | public override string Five() => PrintMessages.NotSupported; 12 | 13 | public override string Nine() => PrintMessages.NotSupported; 14 | 15 | public override int Multiplier() => 1000; 16 | } 17 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/Program.cs: -------------------------------------------------------------------------------- 1 | namespace RomanNumerals 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Interpreter.Common; 7 | using RomanNumerals.Models; 8 | 9 | public class Program 10 | { 11 | public static void Main() 12 | { 13 | string input = Constants.Input; 14 | 15 | var context = new Context(input); 16 | 17 | var tree = new List() 18 | { 19 | new ThousandExpression(), 20 | new HundredExpression(), 21 | new TenExpression(), 22 | new OneExpression() 23 | }; 24 | 25 | Console.WriteLine(PrintMessages.CurrentContext, context.Input, context.Output); 26 | 27 | foreach (var expression in tree) 28 | { 29 | expression.Interpret(context); 30 | Console.WriteLine(PrintMessages.CurrentContext, context.Input, context.Output); 31 | } 32 | 33 | Console.WriteLine(); 34 | Console.WriteLine(PrintMessages.Final, input, context.Output); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/ContextExpressions/RomanNumerals.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/Interpreter.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Interpreter.Common 2 | { 3 | public class Constants 4 | { 5 | public const string Input = "MCMXXVIII"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/Interpreter.Common/Interpreter.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Interpreter/Interpreter.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Interpreter.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string NotSupported = "Not supported"; 6 | public const string CurrentContext = "Current context: Input: {0} Output: {1}"; 7 | public const string Final = "Final: Input: {0} Output: {1}"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/Iterator.Common/Iterator.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/Iterator.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Iterator.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string CustomIterator = "Custom Iterator pattern implementation"; 6 | public const string NETIterator = "Iterator pattern implementation in .NET"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/Contracts/IIterator.cs: -------------------------------------------------------------------------------- 1 | namespace SequenceNumbers.Contracts 2 | { 3 | /// 4 | /// The 'Iterator' interface 5 | /// 6 | public interface IIterator 7 | { 8 | T Current { get; } 9 | 10 | bool Next(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/Contracts/ISequence.cs: -------------------------------------------------------------------------------- 1 | namespace SequenceNumbers.Contracts 2 | { 3 | /// 4 | /// The 'Collection' interface 5 | /// 6 | public interface ISequence 7 | { 8 | IIterator CreateIterator(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/Models/Sequence.cs: -------------------------------------------------------------------------------- 1 | namespace SequenceNumbers.Models 2 | { 3 | using System.Collections.Generic; 4 | 5 | using SequenceNumbers.Contracts; 6 | 7 | /// 8 | /// The 'ConcreteCollection' class 9 | /// 10 | public class Sequence : ISequence 11 | { 12 | private readonly List items = new(); 13 | 14 | public IIterator CreateIterator() 15 | { 16 | return new SequenceIterator(this); 17 | } 18 | 19 | public T this[int index] 20 | { 21 | get => this.items[index]; 22 | set => this.items[index] = value; 23 | } 24 | 25 | public int Count => this.items.Count; 26 | 27 | public void Add(T item) 28 | { 29 | this.items.Add(item); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/Models/SequenceIterator.cs: -------------------------------------------------------------------------------- 1 | namespace SequenceNumbers.Models 2 | { 3 | using System; 4 | 5 | using SequenceNumbers.Contracts; 6 | 7 | /// 8 | /// The 'ConcreteIterator' class 9 | /// 10 | public class SequenceIterator : IIterator 11 | { 12 | private readonly Sequence sequence; 13 | private int index; 14 | 15 | public SequenceIterator(Sequence sequence) 16 | { 17 | this.sequence = sequence; 18 | this.index = -1; 19 | } 20 | 21 | public T Current 22 | { 23 | get 24 | { 25 | if (this.index < this.sequence.Count) 26 | { 27 | return this.sequence[index]; 28 | } 29 | else 30 | { 31 | throw new InvalidOperationException(); 32 | } 33 | } 34 | } 35 | 36 | public bool Next() 37 | { 38 | this.index++; 39 | return this.index < this.sequence.Count; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SequenceNumbers 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Iterator.Common; 7 | using SequenceNumbers.Contracts; 8 | using SequenceNumbers.Models; 9 | 10 | public class Program 11 | { 12 | public static void Main() 13 | { 14 | Console.WriteLine(PrintMessages.CustomIterator); 15 | 16 | Sequence collection = new(); 17 | collection.Add(1); 18 | collection.Add(2); 19 | collection.Add(3); 20 | collection.Add(4); 21 | collection.Add(5); 22 | 23 | IIterator iterator = collection.CreateIterator(); 24 | while (iterator.Next()) 25 | { 26 | int item = iterator.Current; 27 | Console.WriteLine(item); 28 | } 29 | 30 | Console.WriteLine(new string('-', 50)); 31 | 32 | Console.WriteLine(PrintMessages.NETIterator); 33 | 34 | IEnumerable elements = new List() { 1, 2, 3, 4, 5 }; 35 | 36 | IEnumerator enumerator = elements.GetEnumerator(); 37 | while (enumerator.MoveNext()) 38 | { 39 | int currentElement = enumerator.Current; 40 | Console.WriteLine(currentElement); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /BehavioralPatterns/Iterator/SequenceNumbers/SequenceNumbers.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/IKitchenMediator.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen 2 | { 3 | using Kitchen.Models; 4 | 5 | /// 6 | /// The 'Mediator' interface 7 | /// 8 | public interface IKitchenMediator 9 | { 10 | void Register(Employee employee); 11 | 12 | void SendMessage(string from, string to, string message); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/Kitchen.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/KitchenMediator.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen 2 | { 3 | using System.Collections.Generic; 4 | 5 | using Kitchen.Models; 6 | 7 | /// 8 | /// The 'ConcreteMediator' class 9 | /// 10 | public class KitchenMediator : IKitchenMediator 11 | { 12 | private readonly Dictionary employees = new(); 13 | 14 | public void Register(Employee employee) 15 | { 16 | if (!this.employees.ContainsValue(employee)) 17 | { 18 | this.employees[employee.Name] = employee; 19 | } 20 | 21 | employee.Mediator = this; 22 | } 23 | 24 | public void SendMessage(string from, string to, string message) 25 | { 26 | if (employees.ContainsKey(to)) 27 | { 28 | Employee employee = this.employees[to]; 29 | employee.Receive(from, message); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/Models/Cook.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen.Models 2 | { 3 | /// 4 | /// The 'ConcreteColleague' class 5 | /// 6 | public class Cook : Employee 7 | { 8 | public Cook(string name, IKitchenMediator mediator) 9 | : base(name, mediator) 10 | { 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/Models/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen.Models 2 | { 3 | using System; 4 | 5 | using Mediator.Common; 6 | 7 | /// 8 | /// The 'Colleague' class 9 | /// 10 | public abstract class Employee 11 | { 12 | public Employee(string name, IKitchenMediator mediator) 13 | { 14 | this.Name = name; 15 | this.Mediator = mediator; 16 | } 17 | 18 | public string Name { get; private set; } 19 | 20 | public IKitchenMediator Mediator { get; set; } 21 | 22 | public void Send(string to, string message) => this.Mediator.SendMessage(this.Name, to, message); 23 | 24 | public void Receive(string from, string message) 25 | { 26 | Console.WriteLine(PrintMessages.Response, from, this.Name, message); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/Models/Waiter.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen.Models 2 | { 3 | /// 4 | /// The 'ConcreteColleague' class 5 | /// 6 | public class Waiter : Employee 7 | { 8 | public Waiter(string name, IKitchenMediator mediator) 9 | : base(name, mediator) 10 | { 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Kitchen/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Kitchen 2 | { 3 | using Kitchen.Models; 4 | using Mediator.Common; 5 | 6 | public class Program 7 | { 8 | public static void Main() 9 | { 10 | IKitchenMediator mediator = new KitchenMediator(); 11 | 12 | Employee waiter = new Waiter(Constants.DisplayWaiter, mediator); 13 | Employee cook = new Cook(Constants.DisplayCook, mediator); 14 | 15 | mediator.Register(waiter); 16 | mediator.Register(cook); 17 | 18 | waiter.Send(Constants.DisplayCook, PrintMessages.Ordered); 19 | cook.Send(Constants.DisplayWaiter, PrintMessages.Ready); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Mediator.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Mediator.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayJohn = "John"; 6 | public const string DisplayPeter = "Peter"; 7 | public const string DisplayAtanas = "Atanas"; 8 | public const string DisplayWaiter = "Waiter"; 9 | public const string DisplayCook = "Cook"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Mediator.Common/Mediator.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Mediator.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Mediator.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Response = "{0} to {1}: '{2}'"; 6 | public const string Ordered = "Cheesecake ordered!"; 7 | public const string Ready = "Cheesecake is ready!"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/AbstractChatRoom.cs: -------------------------------------------------------------------------------- 1 | namespace Messaging 2 | { 3 | using Messaging.Models; 4 | 5 | /// 6 | /// The 'Mediator' abstract class 7 | /// 8 | public abstract class AbstractChatRoom 9 | { 10 | public abstract void Register(Participant participant); 11 | 12 | public abstract void Send(Participant from, Participant to, string message); 13 | } 14 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/ChatRoom.cs: -------------------------------------------------------------------------------- 1 | namespace Messaging 2 | { 3 | using Messaging.Models; 4 | 5 | /// 6 | /// The 'ConcreteMediator' class 7 | /// 8 | public class ChatRoom : AbstractChatRoom 9 | { 10 | public override void Register(Participant participant) => participant.ChatRoom = this; 11 | 12 | public override void Send(Participant from, Participant to, string message) => to.Receive(from.Name, message); 13 | } 14 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/Messaging.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/Models/ConcreteParticipant.cs: -------------------------------------------------------------------------------- 1 | namespace Messaging.Models 2 | { 3 | /// 4 | /// The 'ConcreteColleague' class 5 | /// 6 | public class ConcreteParticipant : Participant 7 | { 8 | public ConcreteParticipant(string name) 9 | : base(name) 10 | { 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/Models/Participant.cs: -------------------------------------------------------------------------------- 1 | namespace Messaging.Models 2 | { 3 | using System; 4 | 5 | using Mediator.Common; 6 | 7 | /// 8 | /// The 'Colleague' class 9 | /// 10 | public class Participant 11 | { 12 | public Participant(string name) 13 | { 14 | this.Name = name; 15 | } 16 | 17 | public string Name { get; private set; } 18 | 19 | public ChatRoom ChatRoom { get; set; } 20 | 21 | public void Send(Participant to, string message) => ChatRoom.Send(this, to, message); 22 | 23 | public virtual void Receive(string from, string message) 24 | { 25 | Console.WriteLine(PrintMessages.Response, from, this.Name, message); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BehavioralPatterns/Mediator/Messaging/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Messaging 2 | { 3 | using Mediator.Common; 4 | using Messaging.Models; 5 | 6 | public class Program 7 | { 8 | public static void Main() 9 | { 10 | Participant john = new ConcreteParticipant(Constants.DisplayJohn); 11 | Participant peter = new ConcreteParticipant(Constants.DisplayPeter); 12 | Participant nasko = new ConcreteParticipant(Constants.DisplayAtanas); 13 | 14 | ChatRoom chatRoom = new(); 15 | chatRoom.Register(john); 16 | chatRoom.Register(peter); 17 | chatRoom.Register(nasko); 18 | 19 | john.Send(peter, "Hi Peter! How are you?"); 20 | peter.Send(john, "Fine! Can you tell Atanas that the meeting will start at three o'clock?"); 21 | john.Send(peter, "Yes."); 22 | peter.Send(john, "Thank you!"); 23 | john.Send(nasko, "The meeting starts at three o'clock!"); 24 | nasko.Send(john, "Fine, I will be there."); 25 | john.Send(peter, "Nasko is informed."); 26 | john.Send(peter, "Bye, see you at the meeting."); 27 | nasko.Send(peter, "Can you pick me up from home since my car is broken?"); 28 | peter.Send(nasko, "No problem!"); 29 | nasko.Send(peter, "Thank you, I really appreciate it!"); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/Memento.Common/Memento.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/Memento.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Memento.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string InitialState = "--- Initial state ---"; 6 | public const string SavingState = "--- Saving state ---\n"; 7 | public const string ChangeState = "--- Change initial prospect ---"; 8 | public const string RestoreState = "--- Restore intitial state ---"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/ProspectState/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ProspectState 2 | { 3 | using System; 4 | 5 | using Memento.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | SalesProspect prospect = new("Atanas", "088 8999 891", 200); 12 | Console.WriteLine(PrintMessages.InitialState); 13 | Console.WriteLine(prospect); 14 | 15 | Console.WriteLine(PrintMessages.SavingState); 16 | ProspectMemory prospectMemory = new() 17 | { 18 | Memento = prospect.SaveMemento() 19 | }; 20 | 21 | Console.WriteLine(PrintMessages.ChangeState); 22 | prospect.Name = "Nasko"; 23 | prospect.Phone = "123 456 789"; 24 | prospect.Budget = 12000; 25 | Console.WriteLine(prospect); 26 | 27 | Console.WriteLine(PrintMessages.RestoreState); 28 | prospect.RestoreMemento(prospectMemory.Memento); 29 | Console.WriteLine(prospect); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/ProspectState/ProspectMemento.cs: -------------------------------------------------------------------------------- 1 | namespace ProspectState 2 | { 3 | /// 4 | /// The 'Memento' class 5 | /// 6 | public class ProspectMemento 7 | { 8 | public ProspectMemento(string name, string phone, double budget) 9 | { 10 | this.Name = name; 11 | this.Phone = phone; 12 | this.Budget = budget; 13 | } 14 | 15 | public string Name { get; private set; } 16 | 17 | public string Phone { get; private set; } 18 | 19 | public double Budget { get; private set; } 20 | } 21 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/ProspectState/ProspectMemory.cs: -------------------------------------------------------------------------------- 1 | namespace ProspectState 2 | { 3 | /// 4 | /// The 'Caretaker' class 5 | /// 6 | public class ProspectMemory 7 | { 8 | public ProspectMemento Memento { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/ProspectState/ProspectState.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Memento/ProspectState/SalesProspect.cs: -------------------------------------------------------------------------------- 1 | namespace ProspectState 2 | { 3 | /// 4 | /// The 'Originator' class 5 | /// 6 | public class SalesProspect 7 | { 8 | public SalesProspect(string name, string phone, double budget) 9 | { 10 | this.Name = name; 11 | this.Phone = phone; 12 | this.Budget = budget; 13 | } 14 | 15 | public string Name { get; set; } 16 | 17 | public string Phone { get; set; } 18 | 19 | public double Budget { get; set; } 20 | 21 | public ProspectMemento SaveMemento() => new(this.Name, this.Phone, this.Budget); 22 | 23 | public void RestoreMemento(ProspectMemento memento) 24 | { 25 | this.Name = memento.Name; 26 | this.Phone = memento.Phone; 27 | this.Budget = memento.Budget; 28 | } 29 | 30 | public override string ToString() => $"Name: {this.Name}\nPhone: {this.Phone}\nBudget: {this.Budget}\n"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/InvestorStocks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/ObserverModels/IInvestor.cs: -------------------------------------------------------------------------------- 1 | namespace InvestorStocks.ObserverModels 2 | { 3 | using InvestorStocks.SubjectModels; 4 | 5 | /// 6 | /// The 'Observer' interface 7 | /// 8 | public interface IInvestor 9 | { 10 | void Update(Stock stock); 11 | } 12 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/ObserverModels/Investor.cs: -------------------------------------------------------------------------------- 1 | namespace InvestorStocks.ObserverModels 2 | { 3 | using System; 4 | 5 | using InvestorStocks.SubjectModels; 6 | using Observer.Common; 7 | 8 | /// 9 | /// The 'ConcreteObserver' class 10 | /// 11 | public class Investor : IInvestor 12 | { 13 | private readonly string name; 14 | 15 | public Investor(string name) 16 | { 17 | this.name = name; 18 | } 19 | 20 | public void Update(Stock stock) 21 | { 22 | Console.WriteLine(PrintMessages.NotifyChange, this.name, stock.Symbol, stock.Price); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/Program.cs: -------------------------------------------------------------------------------- 1 | namespace InvestorStocks 2 | { 3 | using InvestorStocks.ObserverModels; 4 | using InvestorStocks.SubjectModels; 5 | using Observer.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | Stock ibm = new(nameof(IBM), 120.2); 12 | var firstInvestor = new Investor(Constants.DisplayInvestor + 1); 13 | ibm.Attach(firstInvestor); 14 | ibm.Attach(new Investor(Constants.DisplayInvestor + 2)); 15 | 16 | ibm.Price = 120.4; 17 | ibm.Detach(firstInvestor); 18 | 19 | ibm.Price = 125.58; 20 | 21 | ibm.Attach(new Investor(Constants.DisplayInvestor + 3)); 22 | ibm.Price = 120.99; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/SubjectModels/IBM.cs: -------------------------------------------------------------------------------- 1 | namespace InvestorStocks.SubjectModels 2 | { 3 | /// 4 | /// The 'ConcreteSubject' class 5 | /// 6 | public class IBM : Stock 7 | { 8 | public IBM(string symbol, double price) 9 | : base(symbol, price) 10 | { 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/InvestorStocks/SubjectModels/Stock.cs: -------------------------------------------------------------------------------- 1 | namespace InvestorStocks.SubjectModels 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using InvestorStocks.ObserverModels; 7 | 8 | /// 9 | /// The 'Subject' class 10 | /// 11 | public class Stock 12 | { 13 | private readonly List investors = new(); 14 | private double price; 15 | 16 | public Stock(string symbol, double price) 17 | { 18 | this.Symbol = symbol; 19 | this.price = price; 20 | } 21 | 22 | public string Symbol { get; private set; } 23 | 24 | public double Price 25 | { 26 | get => this.price; 27 | set 28 | { 29 | if (Math.Abs(this.price - value) > 0.001) 30 | { 31 | this.price = value; 32 | this.Notify(); 33 | } 34 | } 35 | } 36 | 37 | public void Attach(IInvestor investor) => this.investors.Add(investor); 38 | 39 | public void Detach(IInvestor investor) => this.investors.Remove(investor); 40 | 41 | private void Notify() 42 | { 43 | foreach (var investor in this.investors) 44 | { 45 | investor.Update(this); 46 | } 47 | 48 | Console.WriteLine(); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/Observer.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Observer.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayInvestor = "Investor #"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/Observer.Common/Observer.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Observer/Observer.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Observer.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string NotifyChange = "Notified {0} of {1}'s change to {2}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Account.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState 2 | { 3 | using System; 4 | 5 | using AccountState.Models; 6 | using State.Common; 7 | 8 | /// 9 | /// The 'Context' class 10 | /// 11 | public class Account 12 | { 13 | public Account(string owner) 14 | { 15 | this.State = new SilverState(0, this); 16 | this.Owner = owner; 17 | } 18 | 19 | public string Owner { get; set; } 20 | 21 | public AccountState State { get; set; } 22 | 23 | public double Balance => this.State.Balance; 24 | 25 | public void Deposit(double amount) 26 | { 27 | this.State.Deposit(amount); 28 | Console.WriteLine(PrintMessages.Deposited, amount); 29 | Console.WriteLine(PrintMessages.Balance, this.Balance); 30 | Console.WriteLine(PrintMessages.Status, this.State.GetType().Name); 31 | } 32 | 33 | public void PayInterest() 34 | { 35 | this.State.PayInterest(); 36 | Console.WriteLine(PrintMessages.InterestPaid); 37 | Console.WriteLine(PrintMessages.Balance, this.Balance); 38 | Console.WriteLine(PrintMessages.Status, this.State.GetType().Name); 39 | } 40 | 41 | public void Withdraw(int amount) 42 | { 43 | this.State.Withdraw(amount); 44 | Console.WriteLine(PrintMessages.Withdraw, amount); 45 | Console.WriteLine(PrintMessages.Balance, this.Balance); 46 | Console.WriteLine(PrintMessages.Status, this.State.GetType().Name); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/AccountState.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Models/AccountState.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState.Models 2 | { 3 | /// 4 | /// The 'State' abstract class 5 | /// 6 | public abstract class AccountState 7 | { 8 | protected double Interest { get; set; } 9 | 10 | protected double LowerLimit { get; set; } 11 | 12 | protected double UpperLimit { get; set; } 13 | 14 | public Account Account { get; protected set; } 15 | 16 | public double Balance { get; protected set; } 17 | 18 | public abstract void Deposit(double money); 19 | 20 | public abstract void Withdraw(double money); 21 | 22 | public abstract void PayInterest(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Models/GoldState.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState.Models 2 | { 3 | /// 4 | /// The 'ConcreteState' class 5 | /// 6 | public class GoldState : AccountState 7 | { 8 | public GoldState(AccountState state) 9 | : this(state.Balance, state.Account) 10 | { 11 | } 12 | 13 | public GoldState(double balance, Account account) 14 | { 15 | this.Balance = balance; 16 | this.Account = account; 17 | this.Initialize(); 18 | } 19 | 20 | public override void Deposit(double amount) 21 | { 22 | this.Balance += amount; 23 | this.CheckStateChanged(); 24 | } 25 | 26 | public override void PayInterest() 27 | { 28 | this.Balance += base.Interest * base.Balance; 29 | this.CheckStateChanged(); 30 | } 31 | 32 | public override void Withdraw(double amount) 33 | { 34 | this.Balance -= amount; 35 | this.CheckStateChanged(); 36 | } 37 | 38 | private void CheckStateChanged() 39 | { 40 | if(this.Balance < base.LowerLimit) 41 | { 42 | this.Account.State = new SilverState(this); 43 | } 44 | } 45 | 46 | private void Initialize() 47 | { 48 | base.Interest = 0.05; 49 | base.LowerLimit = 1000; 50 | base.UpperLimit = 1000000; 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Models/RedState.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState.Models 2 | { 3 | using System; 4 | 5 | using State.Common; 6 | 7 | /// 8 | /// The 'ConcreteState' class 9 | /// 10 | public class RedState : AccountState 11 | { 12 | public RedState(AccountState state) 13 | : this(state.Balance, state.Account) 14 | { 15 | } 16 | 17 | public RedState(double balance, Account account) 18 | { 19 | this.Balance = balance; 20 | this.Account = account; 21 | this.Initialize(); 22 | } 23 | 24 | public override void Deposit(double amount) 25 | { 26 | this.Balance += amount; 27 | this.CheckStateChanged(); 28 | } 29 | 30 | 31 | public override void PayInterest() 32 | { 33 | this.Balance += base.Interest * base.Balance; 34 | this.CheckStateChanged(); 35 | } 36 | 37 | public override void Withdraw(double amount) 38 | { 39 | Console.WriteLine(PrintMessages.NoFundsAvailable); 40 | } 41 | 42 | private void CheckStateChanged() 43 | { 44 | if (this.Balance > base.UpperLimit) 45 | { 46 | this.Account.State = new SilverState(this); 47 | } 48 | } 49 | 50 | private void Initialize() 51 | { 52 | base.Interest = 0; 53 | base.UpperLimit = 0; 54 | base.LowerLimit = -100; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Models/SilverState.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState.Models 2 | { 3 | /// 4 | /// The 'ConcreteState' class 5 | /// 6 | public class SilverState : AccountState 7 | { 8 | public SilverState(AccountState state) 9 | : this(state.Balance, state.Account) 10 | { 11 | } 12 | 13 | public SilverState(double balance, Account account) 14 | { 15 | this.Balance = balance; 16 | this.Account = account; 17 | this.Initialize(); 18 | } 19 | 20 | 21 | public override void Deposit(double amount) 22 | { 23 | this.Balance += amount; 24 | this.CheckStateChanged(); 25 | } 26 | 27 | public override void PayInterest() 28 | { 29 | this.Balance += base.Interest * base.Balance; 30 | this.CheckStateChanged(); 31 | } 32 | 33 | public override void Withdraw(double amount) 34 | { 35 | this.Balance -= amount; 36 | this.CheckStateChanged(); 37 | } 38 | 39 | private void CheckStateChanged() 40 | { 41 | if (this.Balance < base.LowerLimit) 42 | { 43 | this.Account.State = new RedState(this); 44 | } 45 | else if (this.Balance > base.UpperLimit) 46 | { 47 | this.Account.State = new GoldState(this); 48 | } 49 | } 50 | 51 | private void Initialize() 52 | { 53 | base.Interest = 0; 54 | base.UpperLimit = 1000; 55 | base.LowerLimit = 0; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/AccountState/Program.cs: -------------------------------------------------------------------------------- 1 | namespace AccountState 2 | { 3 | using AccountState.Models; 4 | 5 | public class Program 6 | { 7 | public static void Main() 8 | { 9 | Account account = new("Atanas Vasilev"); 10 | 11 | account.Deposit(500); 12 | account.Deposit(300); 13 | account.Deposit(350); 14 | 15 | account.PayInterest(); 16 | 17 | account.Withdraw(1000); 18 | account.Withdraw(1100); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/State.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace State.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Deposited = "Deposited {0} ---"; 6 | public const string Withdraw = "Withdraw {0} ---"; 7 | public const string InterestPaid = "Interest paid"; 8 | public const string Balance = " Balance = {0}"; 9 | public const string Status = " Status = {0}\n"; 10 | public const string NoFundsAvailable = "No funds available for withdrawal!"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/State/State.Common/State.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/Models/MergeSort.cs: -------------------------------------------------------------------------------- 1 | namespace SortTypes.Models 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Strategy.Common; 7 | 8 | /// 9 | /// A 'ConcreteStrategy' class 10 | /// 11 | public class MergeSort : SortStrategy 12 | { 13 | public override void Sort(IEnumerable collection) 14 | { 15 | Console.WriteLine(PrintMessages.SortAlgorithm, nameof(MergeSort)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/Models/QuickSort.cs: -------------------------------------------------------------------------------- 1 | namespace SortTypes.Models 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Strategy.Common; 7 | 8 | /// 9 | /// A 'ConcreteStrategy' class 10 | /// 11 | public class QuickSort : SortStrategy 12 | { 13 | public override void Sort(IEnumerable collection) 14 | { 15 | Console.WriteLine(PrintMessages.SortAlgorithm, nameof(QuickSort)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/Models/SortStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace SortTypes.Models 2 | { 3 | using System.Collections.Generic; 4 | 5 | /// 6 | /// The 'Strategy' abstract class 7 | /// 8 | public abstract class SortStrategy 9 | { 10 | public abstract void Sort(IEnumerable collection); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/Models/SortedList.cs: -------------------------------------------------------------------------------- 1 | namespace SortTypes.Models 2 | { 3 | using System.Collections.Generic; 4 | 5 | /// 6 | /// The 'Context' class 7 | /// 8 | public class SortedList 9 | { 10 | private readonly List items; 11 | 12 | public SortedList() 13 | { 14 | this.items = new List(); 15 | } 16 | 17 | public void Sort(SortStrategy sortStrategy) 18 | { 19 | sortStrategy.Sort(this.items); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SortTypes 2 | { 3 | using SortTypes.Models; 4 | 5 | public class Program 6 | { 7 | public static void Main() 8 | { 9 | SortStrategy quickSort = new QuickSort(); 10 | SortedList numbers = new(); 11 | numbers.Sort(quickSort); 12 | 13 | SortStrategy mergeSort = new MergeSort(); 14 | SortedList names = new(); 15 | names.Sort(mergeSort); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/SortTypes/SortTypes.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/Strategy.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Strategy.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string SortAlgorithm = "Sort collection using {0} algorithm!"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Strategy/Strategy.Common/Strategy.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TeaHouse/Models/Coffee.cs: -------------------------------------------------------------------------------- 1 | namespace TeaHouse 2 | { 3 | using System; 4 | 5 | using TemplateMethod.Common; 6 | 7 | /// 8 | /// A 'ConcreteClass' class 9 | /// 10 | public class Coffee : HotDrink 11 | { 12 | public override void AddSpices() 13 | { 14 | Console.WriteLine(PrintMessages.AddCream); 15 | } 16 | 17 | public override void Brew() 18 | { 19 | Console.WriteLine(PrintMessages.AddCoffee); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TeaHouse/Models/HotDrink.cs: -------------------------------------------------------------------------------- 1 | namespace TeaHouse 2 | { 3 | using System; 4 | 5 | using TemplateMethod.Common; 6 | 7 | /// 8 | /// The 'AbstractClass' abstract class 9 | /// 10 | public abstract class HotDrink 11 | { 12 | public void PrepareRecipe() 13 | { 14 | this.BoilWater(); 15 | this.Brew(); 16 | this.PourInCup(); 17 | this.AddSpices(); 18 | } 19 | 20 | public abstract void Brew(); 21 | 22 | public abstract void AddSpices(); 23 | 24 | public void BoilWater() 25 | { 26 | Console.WriteLine(PrintMessages.WaterBoil); 27 | } 28 | 29 | public void PourInCup() 30 | { 31 | Console.WriteLine(PrintMessages.DrinkPour); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TeaHouse/Models/Tea.cs: -------------------------------------------------------------------------------- 1 | namespace TeaHouse.Models 2 | { 3 | using System; 4 | 5 | using TemplateMethod.Common; 6 | 7 | /// 8 | /// A 'ConcreteClass' class 9 | /// 10 | public class Tea : HotDrink 11 | { 12 | public override void AddSpices() 13 | { 14 | Console.WriteLine(PrintMessages.AddSugar); 15 | } 16 | 17 | public override void Brew() 18 | { 19 | Console.WriteLine(PrintMessages.AddHerbs); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TeaHouse/Program.cs: -------------------------------------------------------------------------------- 1 | namespace TeaHouse 2 | { 3 | using System; 4 | 5 | using TeaHouse.Models; 6 | using TemplateMethod.Common; 7 | 8 | public class Program 9 | { 10 | public static void Main() 11 | { 12 | HotDrink tea = new Tea(); 13 | Console.WriteLine(PrintMessages.PrepareDrink, nameof(Tea)); 14 | tea.PrepareRecipe(); 15 | 16 | Console.WriteLine(); 17 | 18 | HotDrink coffee = new Coffee(); 19 | Console.WriteLine(PrintMessages.PrepareDrink, nameof(Coffee)); 20 | coffee.PrepareRecipe(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TeaHouse/TeaHouse.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TemplateMethod.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace TemplateMethod.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string PrepareDrink = "--- Preparing {0} ---"; 6 | public const string WaterBoil = "Water reached 100 degrees"; 7 | public const string AddHerbs = "Added herbs in the water"; 8 | public const string AddCoffee = "Added coffee grains in the water"; 9 | public const string AddSugar = "Added sugar to tea"; 10 | public const string AddCream = "Added cream to coffee"; 11 | public const string DrinkPour = "Drink poured in a cup"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BehavioralPatterns/TemplateMethod/TemplateMethod.Common/TemplateMethod.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/ElementModels/Clerk.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.ElementModels 2 | { 3 | public class Clerk : Employee 4 | { 5 | public Clerk(string name) 6 | : base(name, 25000, 14) 7 | { 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/ElementModels/Director.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.ElementModels 2 | { 3 | public class Director : Employee 4 | { 5 | public Director(string name) 6 | : base(name, 35000, 16) 7 | { 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/ElementModels/Element.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.ElementModels 2 | { 3 | using EmployeeAdministration.VisitorModels; 4 | 5 | /// 6 | /// The 'Element' abstract class 7 | /// 8 | public abstract class Element 9 | { 10 | public abstract void Accept(IVisitor visitor); 11 | } 12 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/ElementModels/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.ElementModels 2 | { 3 | using EmployeeAdministration.VisitorModels; 4 | 5 | /// 6 | /// The 'ConcreteElement' class 7 | /// 8 | public class Employee : Element 9 | { 10 | public Employee(string name, double income, int vacationDays) 11 | { 12 | this.Name = name; 13 | this.Income = income; 14 | this.VacationDays = vacationDays; 15 | } 16 | 17 | public string Name { get; set; } 18 | 19 | public double Income { get; set; } 20 | 21 | public int VacationDays { get; set; } 22 | 23 | public override void Accept(IVisitor visitor) 24 | { 25 | visitor.Visit(this); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/ElementModels/President.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.ElementModels 2 | { 3 | public class President : Employee 4 | { 5 | public President(string name) 6 | : base(name, 50000, 20) 7 | { 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/EmployeeAdministration.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/Program.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using EmployeeAdministration.ElementModels; 7 | using EmployeeAdministration.VisitorModels; 8 | using Visitor.Common; 9 | 10 | public class Program 11 | { 12 | public static void Main() 13 | { 14 | var employees = new List 15 | { 16 | new Clerk("Peter"), 17 | new Director("Nasko"), 18 | new President("Georgi") 19 | }; 20 | 21 | foreach (var employee in employees) 22 | { 23 | var employeeType = employee.GetType().Name; 24 | Console.WriteLine(PrintMessages.VacationDays, employeeType, employee.VacationDays); 25 | Console.WriteLine(PrintMessages.Income, employeeType, employee.Income); 26 | } 27 | 28 | Console.WriteLine(PrintMessages.VisitingEmployees, nameof(IncomeVisitor), nameof(VacationVisitor)); 29 | 30 | foreach (var employee in employees) 31 | { 32 | employee.Accept(new IncomeVisitor()); 33 | employee.Accept(new VacationVisitor()); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/VisitorModels/IVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.VisitorModels 2 | { 3 | using EmployeeAdministration.ElementModels; 4 | 5 | /// 6 | /// The 'Visitor' interface 7 | /// 8 | public interface IVisitor 9 | { 10 | void Visit(Element element); 11 | } 12 | } -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/VisitorModels/IncomeVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.VisitorModels 2 | { 3 | using System; 4 | 5 | using EmployeeAdministration.ElementModels; 6 | using Visitor.Common; 7 | 8 | /// 9 | /// The 'ConcreteVisitor' class 10 | /// 11 | public class IncomeVisitor : IVisitor 12 | { 13 | public void Visit(Element element) 14 | { 15 | if (element is Employee employee) 16 | { 17 | employee.Income *= 1.1; 18 | Console.WriteLine(PrintMessages.NewIncome, employee.GetType().Name, employee.Name, employee.Income); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/EmployeeAdministration/VisitorModels/VacationVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeAdministration.VisitorModels 2 | { 3 | using System; 4 | 5 | using EmployeeAdministration.ElementModels; 6 | using Visitor.Common; 7 | 8 | /// 9 | /// The 'ConcreteVisitor' class 10 | /// 11 | public class VacationVisitor : IVisitor 12 | { 13 | public void Visit(Element element) 14 | { 15 | if (element is Employee employee) 16 | { 17 | employee.VacationDays += 3; 18 | Console.WriteLine(PrintMessages.NewVacationDays, employee.GetType().Name, employee.Name, employee.VacationDays); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/Visitor.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Visitor.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string VacationDays = "{0} vacation days: {1}"; 6 | public const string Income = "{0} income: {1}\n"; 7 | public const string VisitingEmployees = "Visiting employees with {0} and {1}\n"; 8 | public const string NewIncome = "{0} {1} updated income: {2:F2}"; 9 | public const string NewVacationDays = "{0} {1} updated vacation days: {2}\n"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /BehavioralPatterns/Visitor/Visitor.Common/Visitor.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/AbstractFactory.Common/AbstractFactory.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/AbstractFactory.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayMacchiato = "Macchiato"; 6 | public const string DisplayRegularCoffee = "Regular Coffee"; 7 | public const string DisplayCappuccino = "Cappuccino"; 8 | public const string DisplayDoubleCoffee = "Double Coffee"; 9 | 10 | public const int ContentZeroMl = 0; 11 | public const int ContentHundredMl = 100; 12 | public const int ContentTwoHundredMl = 200; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/AbstractFactory.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace AbstractFactory.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string CoffeeShop = "{0} contains {1} ml coffee and {2} ml milk"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/AbstractProductModels/Coffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.AbstractProductModels 2 | { 3 | using System; 4 | 5 | using AbstractFactory.Common; 6 | 7 | /// 8 | /// The 'AbstractProduct' abstract class 9 | /// 10 | public abstract class Coffee 11 | { 12 | public abstract string Name { get; } 13 | 14 | public abstract int CoffeeContent { get; } 15 | 16 | public abstract int MilkContent { get; } 17 | 18 | public void Print() 19 | { 20 | Console.WriteLine(PrintMessages.CoffeeShop, this.Name, this.CoffeeContent, this.MilkContent); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/AbstractProductModels/MilkCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.AbstractProductModels 2 | { 3 | /// 4 | /// The 'AbstractProductA' abstract class 5 | /// 6 | public abstract class MilkCoffee : Coffee 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/AbstractProductModels/PlainCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.AbstractProductModels 2 | { 3 | /// 4 | /// The 'AbstractProductB' abstract class 5 | /// 6 | public abstract class PlainCoffee : Coffee 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/CoffeeShop.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/Factories/CoffeeFactory.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Factories 2 | { 3 | using CoffeeShop.AbstractProductModels; 4 | 5 | /// 6 | /// The 'AbstractFactory' abstract class 7 | /// 8 | public abstract class CoffeeFactory 9 | { 10 | public abstract PlainCoffee GetPlainCoffee(); 11 | 12 | public abstract MilkCoffee GetMilkCoffee(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/Factories/FrenchCoffeeFactory.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Factories 2 | { 3 | using CoffeeShop.AbstractProductModels; 4 | using CoffeeShop.ProductModels; 5 | 6 | /// 7 | /// The 'ConcreteFactory' class 8 | /// 9 | public class FrenchCoffeeFactory : CoffeeFactory 10 | { 11 | public override PlainCoffee GetPlainCoffee() => new DoubleCoffee(); 12 | 13 | public override MilkCoffee GetMilkCoffee() => new Macchiato(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/Factories/ItalianCoffeeFactory.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Factories 2 | { 3 | using CoffeeShop.AbstractProductModels; 4 | using CoffeeShop.ProductModels; 5 | 6 | /// 7 | /// The 'ConcreteFactory' class 8 | /// 9 | public class ItalianCoffeeFactory : CoffeeFactory 10 | { 11 | public override PlainCoffee GetPlainCoffee() => new RegularCoffee(); 12 | 13 | public override MilkCoffee GetMilkCoffee() => new Cappuccino(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/ProductModels/Cappuccino.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.ProductModels 2 | { 3 | using AbstractFactory.Common; 4 | using CoffeeShop.AbstractProductModels; 5 | 6 | /// 7 | /// The 'ProductA' class 8 | /// 9 | public class Cappuccino : MilkCoffee 10 | { 11 | public override string Name => Constants.DisplayCappuccino; 12 | 13 | public override int CoffeeContent => Constants.ContentHundredMl; 14 | 15 | public override int MilkContent => Constants.ContentTwoHundredMl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/ProductModels/DoubleCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.ProductModels 2 | { 3 | using AbstractFactory.Common; 4 | using CoffeeShop.AbstractProductModels; 5 | 6 | /// 7 | /// The 'ProductB' class 8 | /// 9 | public class DoubleCoffee : PlainCoffee 10 | { 11 | public override string Name => Constants.DisplayDoubleCoffee; 12 | 13 | public override int CoffeeContent => Constants.ContentTwoHundredMl; 14 | 15 | public override int MilkContent => Constants.ContentZeroMl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/ProductModels/Macchiato.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.ProductModels 2 | { 3 | using AbstractFactory.Common; 4 | using CoffeeShop.AbstractProductModels; 5 | 6 | /// 7 | /// The 'ProductA' class 8 | /// 9 | public class Macchiato : MilkCoffee 10 | { 11 | public override string Name => Constants.DisplayMacchiato; 12 | 13 | public override int CoffeeContent => Constants.ContentHundredMl; 14 | 15 | public override int MilkContent => Constants.ContentHundredMl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/ProductModels/RegularCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.ProductModels 2 | { 3 | using AbstractFactory.Common; 4 | using CoffeeShop.AbstractProductModels; 5 | 6 | /// 7 | /// The 'ProductB' class 8 | /// 9 | public class RegularCoffee : PlainCoffee 10 | { 11 | public override string Name => Constants.DisplayRegularCoffee; 12 | 13 | public override int CoffeeContent => Constants.ContentHundredMl; 14 | 15 | public override int MilkContent => Constants.ContentZeroMl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/CoffeeShop/Program.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop 2 | { 3 | using CoffeeShop.AbstractProductModels; 4 | using CoffeeShop.Factories; 5 | 6 | public class Program 7 | { 8 | public static void Main() 9 | { 10 | CoffeeFactory frenchCoffeeFactory = new FrenchCoffeeFactory(); 11 | CoffeeFactory italianCoffeeFactory = new ItalianCoffeeFactory(); 12 | 13 | PlainCoffee frenchPlainCoffee = frenchCoffeeFactory.GetPlainCoffee(); 14 | MilkCoffee frenchMilkCoffee = frenchCoffeeFactory.GetMilkCoffee(); 15 | 16 | PlainCoffee italianPlainCoffee = italianCoffeeFactory.GetPlainCoffee(); 17 | MilkCoffee italianMilkCoffee = italianCoffeeFactory.GetMilkCoffee(); 18 | 19 | frenchPlainCoffee.Print(); 20 | frenchMilkCoffee.Print(); 21 | italianPlainCoffee.Print(); 22 | italianMilkCoffee.Print(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/AbstractProductModels/Carnivore.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.AbstractProductModels 2 | { 3 | /// 4 | /// The 'AbstractProductA' abstract class 5 | /// 6 | public abstract class Carnivore 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/AbstractProductModels/Herbivore.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.AbstractProductModels 2 | { 3 | /// 4 | /// The 'AbstractProductB' abstract class 5 | /// 6 | public abstract class Herbivore 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/ContinentAnimals.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/Factories/AfricaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.Factories 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | using ContinentAnimals.ProductModels; 5 | 6 | /// 7 | /// The 'ConcreteFactory' class 8 | /// 9 | public class AfricaFactory : ContinentFactory 10 | { 11 | public override Herbivore CreateHerbivore() => new Wildbeast(); 12 | 13 | public override Carnivore CreateCarnivore() => new Lion(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/Factories/AmericaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.Factories 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | using ContinentAnimals.ProductModels; 5 | 6 | /// 7 | /// The 'ConcreteFactory' class 8 | /// 9 | public class AmericaFactory : ContinentFactory 10 | { 11 | public override Carnivore CreateCarnivore() => new Wolf(); 12 | 13 | public override Herbivore CreateHerbivore() => new Bison(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/Factories/ContinentFactory.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.Factories 2 | { 3 | using System; 4 | using ContinentAnimals.AbstractProductModels; 5 | 6 | /// 7 | /// The 'AbstractFactory' abstract class 8 | /// 9 | public abstract class ContinentFactory 10 | { 11 | public abstract Herbivore CreateHerbivore(); 12 | 13 | public abstract Carnivore CreateCarnivore(); 14 | 15 | public override string ToString() 16 | { 17 | Herbivore herbivore = this.CreateHerbivore(); 18 | string herbivoreMessage = $"{nameof(Herbivore)}: {herbivore.GetType().Name}"; 19 | 20 | Carnivore carnivore = this.CreateCarnivore(); 21 | string carnivoreMessage = $"{nameof(Carnivore)}: {carnivore.GetType().Name}"; 22 | 23 | return $"{this.GetType().Name}{Environment.NewLine}{herbivoreMessage}{Environment.NewLine}{carnivoreMessage}"; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/ProductModels/Bison.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.ProductModels 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | 5 | /// 6 | /// The 'ProductB' class 7 | /// 8 | public class Bison : Herbivore 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/ProductModels/Lion.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.ProductModels 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | 5 | /// 6 | /// The 'ProductA' class 7 | /// 8 | public class Lion : Carnivore 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/ProductModels/Wildbeast.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.ProductModels 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | 5 | /// 6 | /// The 'ProductB' class 7 | /// 8 | public class Wildbeast : Herbivore 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/ProductModels/Wolf.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals.ProductModels 2 | { 3 | using ContinentAnimals.AbstractProductModels; 4 | 5 | /// 6 | /// The 'ProductA' class 7 | /// 8 | public class Wolf : Carnivore 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /CreationalPatterns/AbstractFactory/ContinentAnimals/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ContinentAnimals 2 | { 3 | using System; 4 | 5 | using ContinentAnimals.Factories; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | ContinentFactory[] continentFactories = new ContinentFactory[] 12 | { 13 | new AfricaFactory(), 14 | new AmericaFactory() 15 | }; 16 | 17 | Console.WriteLine(string.Join($"{Environment.NewLine}{Environment.NewLine}", continentFactories)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/Builder.Common/Builder.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/Builder.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Builder.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayDoubleCoffee = "Double Coffee"; 6 | public const string DisplayWater = "Water"; 7 | public const string DisplayCroissant = "Croissant"; 8 | public const string DisplayCappuccino = "Cappuccino"; 9 | public const string DisplayJuice = "Juice"; 10 | public const string DisplayTiramisu = "Tiramisu"; 11 | 12 | public const string DisplayCar = "Car"; 13 | public const string DisplayMotorcycle = "Motorcycle"; 14 | public const string DisplayScooter = "Scooter"; 15 | public const string DisplayFrame = "frame"; 16 | public const string VehiclePartDoors = "doors"; 17 | public const string VehiclePartEngine = "engine"; 18 | public const string VehiclePartWheels = "wheels"; 19 | 20 | public const string StringNumberFour = "4"; 21 | public const string StringOptionNo = "no"; 22 | public const string StringNumberTwo = "2"; 23 | public const string StringCarEngineCapacity = "1900cc"; 24 | public const string StringMotorcycleEngineCapacity = "250cc"; 25 | public const string StringScooterEngineCapacity = "50cc"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/Builder.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Builder.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string MenuHotDrink = "Hot Drink: {0}"; 6 | public const string MenuColdDrink = "Cold Drink: {0}"; 7 | public const string MenuDessert = "Dessert: {0}"; 8 | 9 | public const string FrenchBreakfastMenu = "French Breakfast Menu: "; 10 | public const string ItalianBreakfast = "Italian Breakfast Menu: "; 11 | 12 | public const string VehicleType = "Vehicle Type: {0}"; 13 | public const string Frame = " Frame: {0}"; 14 | public const string Engine = " Engine: {0}"; 15 | public const string Wheels = " #Wheels: {0}"; 16 | public const string Doors = " #Doors: {0}"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Builders/FrenchBreakfastBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Builders 2 | { 3 | using Builder.Common; 4 | 5 | /// 6 | /// The 'ConcreteBuilder' class 7 | /// 8 | public class FrenchBreakfastBuilder : IMenuBuilder 9 | { 10 | private readonly Menu menu; 11 | 12 | public FrenchBreakfastBuilder() 13 | { 14 | this.menu = new Menu(); 15 | } 16 | 17 | public IMenuBuilder AddHotDrink() 18 | { 19 | this.menu.HotDrink = Constants.DisplayDoubleCoffee; 20 | return this; 21 | } 22 | 23 | public IMenuBuilder AddColdDrink() 24 | { 25 | this.menu.ColdDrink = Constants.DisplayWater; 26 | return this; 27 | } 28 | 29 | public IMenuBuilder AddDessert() 30 | { 31 | this.menu.Dessert = Constants.DisplayCroissant; 32 | return this; 33 | } 34 | 35 | public Menu GetMenu() => this.menu; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Builders/IMenuBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Builders 2 | { 3 | /// 4 | /// The 'Builder' interface 5 | /// 6 | public interface IMenuBuilder 7 | { 8 | abstract IMenuBuilder AddHotDrink(); 9 | 10 | abstract IMenuBuilder AddColdDrink(); 11 | 12 | abstract IMenuBuilder AddDessert(); 13 | 14 | Menu GetMenu(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Builders/ItalianBreakfastBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Builders 2 | { 3 | using Builder.Common; 4 | 5 | /// 6 | /// The 'ConcreteBuilder' class 7 | /// 8 | public class ItalianBreakfastBuilder : IMenuBuilder 9 | { 10 | private readonly Menu menu; 11 | 12 | public ItalianBreakfastBuilder() 13 | { 14 | this.menu = new Menu(); 15 | } 16 | 17 | public IMenuBuilder AddHotDrink() 18 | { 19 | this.menu.HotDrink = Constants.DisplayCappuccino; 20 | return this; 21 | } 22 | 23 | public IMenuBuilder AddColdDrink() 24 | { 25 | this.menu.ColdDrink = Constants.DisplayJuice; 26 | return this; 27 | } 28 | 29 | public IMenuBuilder AddDessert() 30 | { 31 | this.menu.Dessert = Constants.DisplayTiramisu; 32 | return this; 33 | } 34 | 35 | public Menu GetMenu() => this.menu; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/CoffeeShop.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Menu.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop 2 | { 3 | using System; 4 | 5 | using Builder.Common; 6 | 7 | /// 8 | /// The 'Product' class 9 | /// 10 | public class Menu 11 | { 12 | public string HotDrink { get; set; } 13 | 14 | public string ColdDrink { get; set; } 15 | 16 | public string Dessert { get; set; } 17 | 18 | public void Print() 19 | { 20 | Console.WriteLine(PrintMessages.MenuHotDrink, this.HotDrink); 21 | Console.WriteLine(PrintMessages.MenuColdDrink, this.ColdDrink); 22 | Console.WriteLine(PrintMessages.MenuDessert, this.Dessert); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Program.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop 2 | { 3 | using System; 4 | 5 | using Builder.Common; 6 | using CoffeeShop.Builders; 7 | 8 | public class Program 9 | { 10 | public static void Main() 11 | { 12 | Shop coffeeShop = new(); 13 | 14 | IMenuBuilder frenchMenuBuilder = new FrenchBreakfastBuilder(); 15 | IMenuBuilder italianMenuBuilder = new ItalianBreakfastBuilder(); 16 | 17 | coffeeShop.Construct(frenchMenuBuilder); 18 | Menu frenchMenu = frenchMenuBuilder.GetMenu(); 19 | 20 | coffeeShop.Construct(italianMenuBuilder); 21 | Menu italianMenu = italianMenuBuilder.GetMenu(); 22 | 23 | Console.WriteLine(PrintMessages.FrenchBreakfastMenu); 24 | frenchMenu.Print(); 25 | 26 | Console.WriteLine(); 27 | 28 | Console.WriteLine(PrintMessages.ItalianBreakfast); 29 | italianMenu.Print(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/CoffeeShop/Shop.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop 2 | { 3 | using CoffeeShop.Builders; 4 | 5 | /// 6 | /// The 'Director' class 7 | /// 8 | public class Shop 9 | { 10 | public void Construct(IMenuBuilder builder) 11 | { 12 | builder 13 | .AddHotDrink() 14 | .AddColdDrink() 15 | .AddDessert(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Builders/CarBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop.Builders 2 | { 3 | using Builder.Common; 4 | 5 | /// 6 | /// The 'ConcreteBuilder' class 7 | /// 8 | public class CarBuilder : VehicleBuilder 9 | { 10 | public CarBuilder() 11 | { 12 | Vehicle = new Vehicle(Constants.DisplayCar); 13 | } 14 | 15 | public override VehicleBuilder BuildDoors() 16 | { 17 | Vehicle[Constants.VehiclePartDoors] = Constants.StringNumberFour; 18 | return this; 19 | } 20 | 21 | public override VehicleBuilder BuildEngine() 22 | { 23 | Vehicle[Constants.VehiclePartEngine] = Constants.StringCarEngineCapacity; 24 | return this; 25 | } 26 | 27 | public override VehicleBuilder BuildFrame() 28 | { 29 | Vehicle[Constants.DisplayFrame] = Constants.DisplayCar + " " + Constants.DisplayFrame; 30 | return this; 31 | } 32 | 33 | public override VehicleBuilder BuildWheels() 34 | { 35 | Vehicle[Constants.VehiclePartWheels] = Constants.StringNumberFour; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Builders/MotorcycleBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop.Builders 2 | { 3 | using Builder.Common; 4 | 5 | /// 6 | /// The 'ConcreteBuilder' class 7 | /// 8 | public class MotorcycleBuilder : VehicleBuilder 9 | { 10 | public MotorcycleBuilder() 11 | { 12 | Vehicle = new Vehicle(Constants.DisplayMotorcycle); 13 | } 14 | 15 | public override VehicleBuilder BuildDoors() 16 | { 17 | Vehicle[Constants.VehiclePartDoors] = Constants.StringOptionNo; 18 | return this; 19 | } 20 | 21 | public override VehicleBuilder BuildEngine() 22 | { 23 | Vehicle[Constants.VehiclePartEngine] = Constants.StringMotorcycleEngineCapacity; 24 | return this; 25 | } 26 | 27 | public override VehicleBuilder BuildFrame() 28 | { 29 | Vehicle[Constants.DisplayFrame] = Constants.DisplayMotorcycle + " " + Constants.DisplayFrame; 30 | return this; 31 | } 32 | 33 | public override VehicleBuilder BuildWheels() 34 | { 35 | Vehicle[Constants.VehiclePartWheels] = Constants.StringNumberTwo; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Builders/ScooterBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop.Builders 2 | { 3 | using Builder.Common; 4 | 5 | /// 6 | /// The 'ConcreteBuilder' class 7 | /// 8 | public class ScooterBuilder : VehicleBuilder 9 | { 10 | public ScooterBuilder() 11 | { 12 | Vehicle = new Vehicle(Constants.DisplayScooter); 13 | } 14 | 15 | public override VehicleBuilder BuildDoors() 16 | { 17 | Vehicle[Constants.VehiclePartDoors] = Constants.StringOptionNo; 18 | return this; 19 | } 20 | 21 | public override VehicleBuilder BuildEngine() 22 | { 23 | Vehicle[Constants.VehiclePartEngine] = Constants.StringScooterEngineCapacity; 24 | return this; 25 | } 26 | 27 | public override VehicleBuilder BuildFrame() 28 | { 29 | Vehicle[Constants.DisplayFrame] = Constants.DisplayScooter + " " + Constants.DisplayFrame; 30 | return this; 31 | } 32 | 33 | public override VehicleBuilder BuildWheels() 34 | { 35 | Vehicle[Constants.VehiclePartWheels] = Constants.StringNumberTwo; 36 | return this; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Builders/VehicleBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop.Builders 2 | { 3 | /// 4 | /// The 'Builder' abstract class 5 | /// 6 | public abstract class VehicleBuilder 7 | { 8 | public Vehicle Vehicle { get; protected set; } 9 | 10 | public abstract VehicleBuilder BuildFrame(); 11 | 12 | public abstract VehicleBuilder BuildDoors(); 13 | 14 | public abstract VehicleBuilder BuildWheels(); 15 | 16 | public abstract VehicleBuilder BuildEngine(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Program.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop 2 | { 3 | using VehicleShop.Builders; 4 | 5 | public class Program 6 | { 7 | public static void Main() 8 | { 9 | Shop shop = new(); 10 | 11 | var builders = new VehicleBuilder[] 12 | { 13 | new ScooterBuilder(), 14 | new MotorcycleBuilder(), 15 | new CarBuilder() 16 | }; 17 | 18 | foreach (var builder in builders) 19 | { 20 | shop.Construct(builder); 21 | builder.Vehicle.Show(); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Shop.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop 2 | { 3 | using VehicleShop.Builders; 4 | 5 | /// 6 | /// The 'Director' class 7 | /// 8 | public class Shop 9 | { 10 | public void Construct(VehicleBuilder builder) 11 | { 12 | builder 13 | .BuildFrame() 14 | .BuildEngine() 15 | .BuildWheels() 16 | .BuildDoors(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/Vehicle.cs: -------------------------------------------------------------------------------- 1 | namespace VehicleShop 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Builder.Common; 7 | 8 | /// 9 | /// The 'Product' class 10 | /// 11 | public class Vehicle 12 | { 13 | private readonly string vehicleType; 14 | private readonly Dictionary parts = new(); 15 | 16 | public Vehicle(string vehicleType) 17 | { 18 | this.vehicleType = vehicleType; 19 | } 20 | 21 | public string this[string key] 22 | { 23 | get => this.parts[key]; 24 | set => this.parts[key] = value; 25 | } 26 | 27 | public void Show() 28 | { 29 | Console.WriteLine($"{Environment.NewLine}{new string('-', 30)}"); 30 | Console.WriteLine(PrintMessages.VehicleType, vehicleType); 31 | Console.WriteLine(PrintMessages.Frame, parts[Constants.DisplayFrame]); 32 | Console.WriteLine(PrintMessages.Engine, parts[Constants.VehiclePartEngine]); 33 | Console.WriteLine(PrintMessages.Wheels, parts[Constants.VehiclePartWheels]); 34 | Console.WriteLine(PrintMessages.Doors, parts[Constants.VehiclePartDoors]); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CreationalPatterns/Builder/VehicleShop/VehicleShop.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/Factory.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Factory.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayCheesePizza = "Cheese Pizza"; 6 | public const string DisplayHawaiPizza = "Hawai Pizza"; 7 | public const string DisplayPepperoniPizza = "Pepperoni Pizza"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/Factory.Common/Factory.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/Factory.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Factory.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string PizzaFactory = "{0} created by {1}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Factories/CheesePizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Factories 2 | { 3 | using PizzaRestaurant.Models; 4 | 5 | /// 6 | /// The 'ConcreteFactory' class 7 | /// 8 | public class CheesePizzaFactory : PizzaFactory 9 | { 10 | public override Pizza CreatePizza() 11 | { 12 | return new CheesePizza(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Factories/HawaiPizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Factories 2 | { 3 | using PizzaRestaurant.Models; 4 | 5 | /// 6 | /// The 'ConcreteFactory' class 7 | /// 8 | public class HawaiPizzaFactory : PizzaFactory 9 | { 10 | public override Pizza CreatePizza() 11 | { 12 | return new HawaiPizza(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Factories/PepperoniPizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Factories 2 | { 3 | using PizzaRestaurant.Models; 4 | 5 | /// 6 | /// The 'ConcreteFactory' class 7 | /// 8 | public class PepperoniPizzaFactory : PizzaFactory 9 | { 10 | public override Pizza CreatePizza() 11 | { 12 | return new PepperoniPizza(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Factories/PizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Factories 2 | { 3 | using PizzaRestaurant.Models; 4 | 5 | /// 6 | /// The 'AbstractFactory' abstract class 7 | /// 8 | public abstract class PizzaFactory 9 | { 10 | public abstract Pizza CreatePizza(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Models/CheesePizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using Factory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class CheesePizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayCheesePizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Models/HawaiPizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using Factory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class HawaiPizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayHawaiPizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Models/PepperoniPizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using Factory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// S 8 | public class PepperoniPizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayPepperoniPizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Models/Pizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | /// 4 | /// The 'AbstractProduct' abstract class 5 | /// 6 | public abstract class Pizza 7 | { 8 | public abstract string Description { get; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/PizzaRestaurant.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Factory/PizzaRestaurant/Program.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant 2 | { 3 | using System; 4 | 5 | using Factory.Common; 6 | using PizzaRestaurant.Factories; 7 | using PizzaRestaurant.Models; 8 | 9 | public class Program 10 | { 11 | public static void Main() 12 | { 13 | var factories = new PizzaFactory[] 14 | { 15 | new CheesePizzaFactory(), 16 | new PepperoniPizzaFactory(), 17 | new HawaiPizzaFactory() 18 | }; 19 | 20 | foreach (var factory in factories) 21 | { 22 | Pizza pizza = factory.CreatePizza(); 23 | 24 | var factoryName = factory.GetType().Name; 25 | Console.WriteLine(PrintMessages.PizzaFactory, pizza.Description, factoryName); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/CreatorModels/CV.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.CreatorModels 2 | { 3 | using DocumentTypes.ProductModels; 4 | 5 | /// 6 | /// A 'ConcreteCreator' class 7 | /// 8 | public class CV : Document 9 | { 10 | public override void CreatePages() 11 | { 12 | base.Pages.Add(new BioPage()); 13 | base.Pages.Add(new SkillsPage()); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/CreatorModels/Document.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.CreatorModels 2 | { 3 | using System.Collections.Generic; 4 | 5 | using DocumentTypes.ProductModels; 6 | 7 | /// 8 | /// The 'Creator' abstract class 9 | /// 10 | public abstract class Document 11 | { 12 | public List Pages { get; } 13 | 14 | public Document() 15 | { 16 | Pages = new List(); 17 | CreatePages(); 18 | } 19 | 20 | public abstract void CreatePages(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/CreatorModels/Report.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.CreatorModels 2 | { 3 | using DocumentTypes.ProductModels; 4 | 5 | /// 6 | /// A 'ConcreteCreator' class 7 | /// 8 | public class Report : Document 9 | { 10 | public override void CreatePages() 11 | { 12 | base.Pages.Add(new ResultsPage()); 13 | base.Pages.Add(new SummaryPage()); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/DocumentTypes.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/ProductModels/BioPage.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.ProductModels 2 | { 3 | /// 4 | /// A 'ConcreteProduct' class 5 | /// 6 | public class BioPage : Page 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/ProductModels/Page.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.ProductModels 2 | { 3 | /// 4 | /// The 'Product' abstract class 5 | /// 6 | public abstract class Page 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/ProductModels/ResultsPage.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.ProductModels 2 | { 3 | /// 4 | /// A 'ConcreteProduct' class 5 | /// 6 | public class ResultsPage : Page 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/ProductModels/SkillsPage.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.ProductModels 2 | { 3 | /// 4 | /// A 'ConcreteProduct' class 5 | /// 6 | public class SkillsPage : Page 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/ProductModels/SummaryPage.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes.ProductModels 2 | { 3 | /// 4 | /// A 'ConcreteProduct' class 5 | /// 6 | public class SummaryPage : Page 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/DocumentTypes/Program.cs: -------------------------------------------------------------------------------- 1 | namespace DocumentTypes 2 | { 3 | using System; 4 | 5 | using DocumentTypes.CreatorModels; 6 | using FactoryMethod.Common; 7 | 8 | public class Program 9 | { 10 | public static void Main() 11 | { 12 | var documents = new Document[] 13 | { 14 | new CV(), 15 | new Report() 16 | }; 17 | 18 | foreach (var document in documents) 19 | { 20 | var documentTypeName = document.GetType().Name; 21 | Console.WriteLine(documentTypeName); 22 | 23 | for (int i = 0; i < document.Pages.Count; i++) 24 | { 25 | var pageTypeName = document.Pages[i].GetType().Name; 26 | Console.WriteLine(PrintMessages.PageType, i + 1, pageTypeName); 27 | } 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/FactoryMethod.Common/FactoryMethod.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/FactoryMethod/FactoryMethod.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryMethod.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string PageType = "{0}. {1}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/ColorCloning/ColorCloning.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/ColorCloning/ColorManager.cs: -------------------------------------------------------------------------------- 1 | namespace ColorCloning 2 | { 3 | using System.Collections.Generic; 4 | 5 | using ColorCloning.Models; 6 | 7 | /// 8 | /// Prototype manager 9 | /// 10 | public class ColorManager 11 | { 12 | private readonly Dictionary colors = new(); 13 | 14 | public ColorPrototype this[string key] 15 | { 16 | get => colors[key]; 17 | set => colors[key] = value; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/ColorCloning/Models/Color.cs: -------------------------------------------------------------------------------- 1 | namespace ColorCloning.Models 2 | { 3 | using System; 4 | 5 | using Prototype.Common; 6 | 7 | /// 8 | /// The 'ConcretePrototype' class 9 | /// 10 | public class Color : ColorPrototype 11 | { 12 | private readonly int red; 13 | private readonly int green; 14 | private readonly int blue; 15 | 16 | public Color(int red, int green, int blue) 17 | { 18 | this.red = red; 19 | this.green = green; 20 | this.blue = blue; 21 | } 22 | 23 | public override ColorPrototype Clone() 24 | { 25 | Console.WriteLine(PrintMessages.ClonningColor, this.ToString()); 26 | return MemberwiseClone() as ColorPrototype; 27 | } 28 | 29 | public override string ToString() => $"RGB: {this.red}, {this.green}, {this.blue}"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/ColorCloning/Models/ColorPrototype.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ColorCloning.Models 4 | { 5 | /// 6 | /// The 'Prototype' abstract class 7 | /// 8 | public abstract class ColorPrototype 9 | { 10 | public abstract ColorPrototype Clone(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/ColorCloning/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ColorCloning 2 | { 3 | using System; 4 | 5 | using ColorCloning.Models; 6 | using Prototype.Common; 7 | 8 | public class Program 9 | { 10 | public static void Main() 11 | { 12 | Color color = new(10, 20, 30); 13 | Color otherColor = color.Clone() as Color; 14 | 15 | Console.WriteLine($"Are the cloned and original color with equal references: {color == otherColor}"); 16 | 17 | ColorManager colorManager = new(); 18 | 19 | colorManager[Constants.RedColor] = new Color(255, 0, 0); 20 | colorManager[Constants.GeenColor] = new Color(0, 255, 0); 21 | colorManager[Constants.BlueColor] = new Color(0, 0, 255); 22 | 23 | var angryColor = colorManager[Constants.RedColor].Clone() as Color; 24 | var relaxColor = colorManager[Constants.GeenColor].Clone() as Color; 25 | var peaceColor = colorManager[Constants.BlueColor].Clone() as Color; 26 | 27 | Console.WriteLine(angryColor); 28 | Console.WriteLine(relaxColor); 29 | Console.WriteLine(peaceColor); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/Prototype.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Prototype.Common 2 | { 3 | public class Constants 4 | { 5 | public const string RedColor = "red"; 6 | public const string GeenColor = "green"; 7 | public const string BlueColor = "blue"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/Prototype.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Prototype.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string ClonningColor = "Clonning color {0}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Prototype/Prototype.Common/Prototype.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/CoffeeShop.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Enums/CoffeeType.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Enums 2 | { 3 | public enum CoffeeType 4 | { 5 | Regular = 1, 6 | Double, 7 | Cappuccino, 8 | Macchiato 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Factories/CoffeeFactory.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Factories 2 | { 3 | using System; 4 | 5 | using CoffeeShop.Enums; 6 | using CoffeeShop.Models; 7 | 8 | /// 9 | /// The 'SimpleFactory' class 10 | /// 11 | public static class CoffeeFactory 12 | { 13 | public static Coffee GetCoffee(CoffeeType coffeType) 14 | { 15 | switch (coffeType) 16 | { 17 | case CoffeeType.Regular: 18 | return new RegularCoffee(); 19 | 20 | case CoffeeType.Double: 21 | return new DoubleCoffee(); 22 | 23 | case CoffeeType.Cappuccino: 24 | return new Cappuccino(); 25 | 26 | case CoffeeType.Macchiato: 27 | return new Macchiato(); 28 | 29 | default: 30 | throw new ArgumentException(); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Models/Cappuccino.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class Cappuccino : Coffee 9 | { 10 | public override string Name => Constants.DisplayCappuccino; 11 | 12 | public override int CoffeeContent => Constants.ContentHundredMl; 13 | 14 | public override int MilkContent => Constants.ContentTwoHundredMl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Models/Coffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Models 2 | { 3 | using System; 4 | 5 | using SimpleFactory.Common; 6 | 7 | /// 8 | /// The 'AbstractProduct' abstract class 9 | /// 10 | public abstract class Coffee 11 | { 12 | public abstract string Name { get; } 13 | 14 | public abstract int CoffeeContent { get; } 15 | 16 | public abstract int MilkContent { get; } 17 | 18 | public void Print() 19 | { 20 | Console.WriteLine(PrintMessages.CoffeeShop, this.Name, this.CoffeeContent, this.MilkContent); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Models/DoubleCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class DoubleCoffee : Coffee 9 | { 10 | public override string Name => Constants.DisplayDoubleCoffee; 11 | 12 | public override int CoffeeContent => Constants.ContentTwoHundredMl; 13 | 14 | public override int MilkContent => Constants.ContentZeroMl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Models/Macchiato.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class Macchiato : Coffee 9 | { 10 | public override string Name => Constants.DisplayMacchiato; 11 | 12 | public override int CoffeeContent => Constants.ContentHundredMl; 13 | 14 | public override int MilkContent => Constants.ContentHundredMl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Models/RegularCoffee.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class RegularCoffee : Coffee 9 | { 10 | public override string Name => Constants.DisplayRegularCoffee; 11 | 12 | public override int CoffeeContent => Constants.ContentHundredMl; 13 | 14 | public override int MilkContent => Constants.ContentZeroMl; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/CoffeeShop/Program.cs: -------------------------------------------------------------------------------- 1 | namespace CoffeeShop 2 | { 3 | using System; 4 | 5 | using CoffeeShop.Enums; 6 | using CoffeeShop.Factories; 7 | using CoffeeShop.Models; 8 | using SimpleFactory.Common; 9 | 10 | public class Program 11 | { 12 | public static void Main() 13 | { 14 | var coffeeTypes = Enum.GetValues(typeof(CoffeeType)); 15 | 16 | Console.WriteLine(PrintMessages.CoffeeTypes); 17 | 18 | foreach (var item in coffeeTypes) 19 | { 20 | int value = (int)item; 21 | 22 | Console.WriteLine($"{item} - {value}"); 23 | } 24 | 25 | int coffeeNumber; 26 | 27 | while (true) 28 | { 29 | Console.Write(PrintMessages.EnterCoffeeNumber); 30 | bool isNum = int.TryParse(Console.ReadLine(), out coffeeNumber); 31 | 32 | if (isNum) 33 | { 34 | if (coffeeNumber > coffeeTypes.Length || coffeeNumber < 1) 35 | { 36 | Console.WriteLine(PrintMessages.InvalidCoffeeNumber); 37 | continue; 38 | } 39 | break; 40 | } 41 | else 42 | { 43 | Console.WriteLine(PrintMessages.InvalidCoffeeNumber); 44 | continue; 45 | } 46 | } 47 | 48 | CoffeeType coffeeType = (CoffeeType)coffeeNumber; 49 | 50 | Coffee coffee = CoffeeFactory.GetCoffee(coffeeType); 51 | coffee.Print(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Enums/PizzaType.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Enums 2 | { 3 | public enum PizzaType 4 | { 5 | Cheese = 1, 6 | Pepperoni, 7 | Hawai 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Factories/PizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Factories 2 | { 3 | using PizzaRestaurant.Enums; 4 | using PizzaRestaurant.Models; 5 | 6 | /// 7 | /// The 'SimpleFactory' class 8 | /// 9 | public class PizzaFactory 10 | { 11 | public static Pizza CreatePizza(PizzaType pizzaType) 12 | { 13 | return pizzaType switch 14 | { 15 | PizzaType.Cheese => new CheesePizza(), 16 | PizzaType.Pepperoni => new PepperoniPizza(), 17 | PizzaType.Hawai => new HawaiPizza(), 18 | _ => null, 19 | }; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Models/CheesePizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class CheesePizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayCheesePizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Models/HawaiPizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class HawaiPizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayHawaiPizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Models/PepperoniPizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | using SimpleFactory.Common; 4 | 5 | /// 6 | /// The 'Product' class 7 | /// 8 | public class PepperoniPizza : Pizza 9 | { 10 | public override string Description => Constants.DisplayPepperoniPizza; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Models/Pizza.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant.Models 2 | { 3 | /// 4 | /// The 'AbstractProduct' abstract class 5 | /// 6 | public abstract class Pizza 7 | { 8 | public abstract string Description { get; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/PizzaRestaurant.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/PizzaRestaurant/Program.cs: -------------------------------------------------------------------------------- 1 | namespace PizzaRestaurant 2 | { 3 | using System; 4 | 5 | using PizzaRestaurant.Enums; 6 | using PizzaRestaurant.Factories; 7 | using PizzaRestaurant.Models; 8 | 9 | public class Program 10 | { 11 | public static void Main() 12 | { 13 | var objects = new Pizza[] 14 | { 15 | PizzaFactory.CreatePizza(PizzaType.Cheese), 16 | PizzaFactory.CreatePizza(PizzaType.Pepperoni), 17 | PizzaFactory.CreatePizza(PizzaType.Hawai) 18 | }; 19 | 20 | foreach (var pizza in objects) 21 | { 22 | Console.WriteLine(pizza.Description); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/SimpleFactory.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFactory.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayMacchiato = "Macchiato"; 6 | public const string DisplayRegularCoffee = "Regular Coffee"; 7 | public const string DisplayCappuccino = "Cappuccino"; 8 | public const string DisplayDoubleCoffee = "Double Coffee"; 9 | 10 | public const int ContentZeroMl = 0; 11 | public const int ContentHundredMl = 100; 12 | public const int ContentTwoHundredMl = 200; 13 | public const int ObjectsCount = 10000; 14 | 15 | public const string DisplayHawaiPizza = "Hawai Pizza"; 16 | public const string DisplayPepperoniPizza = "Pepperoni Pizza"; 17 | public const string DisplayCheesePizza = "Cheese Pizza"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/SimpleFactory.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleFactory.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string CoffeeTypes = "Coffee Types:"; 6 | public const string EnterCoffeeNumber = "Enter coffee number: "; 7 | public const string InvalidCoffeeNumber = "Please enter one of the above numbers!"; 8 | public const string CoffeeShop = "{0} contains {1} ml coffee and {2} ml milk"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CreationalPatterns/SimpleFactory/SimpleFactory.Common/SimpleFactory.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Instance/Instance.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Instance/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Instance 2 | { 3 | using System; 4 | 5 | using Singleton.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | SingletonInstance singletonOne = SingletonInstance.GetInstance(); 12 | SingletonInstance singletonTwo = SingletonInstance.GetInstance(); 13 | 14 | Console.WriteLine(PrintMessages.Instance, singletonOne == singletonTwo); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Instance/SingletonInstance.cs: -------------------------------------------------------------------------------- 1 | namespace Instance 2 | { 3 | /// 4 | /// The 'Singleton' class 5 | /// 6 | public class SingletonInstance 7 | { 8 | // eager loading 9 | // private static readonly SingletonInstance instance = new SingletonInstance(); 10 | private static SingletonInstance instance; 11 | 12 | public static SingletonInstance GetInstance() 13 | { 14 | if (instance == null) 15 | { 16 | instance = new SingletonInstance(); 17 | } 18 | 19 | return instance; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Logger/Log.cs: -------------------------------------------------------------------------------- 1 | namespace Logger 2 | { 3 | /// 4 | /// Singleton implementation with Double-Check Lock 5 | /// 6 | public sealed class Log 7 | { 8 | private static readonly object lockObject = new object(); 9 | private static Log instance; 10 | 11 | private Log() 12 | { 13 | } 14 | 15 | public static Log Instance 16 | { 17 | get 18 | { 19 | if (instance == null) 20 | { 21 | lock (lockObject) 22 | { 23 | if (instance == null) 24 | { 25 | instance = new Log(); 26 | } 27 | } 28 | } 29 | 30 | return instance; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Logger/Logger.cs: -------------------------------------------------------------------------------- 1 | namespace Logger 2 | { 3 | using System; 4 | 5 | public static class Logger 6 | { 7 | static Logger() 8 | { 9 | Instance = new InnerLogger(); 10 | } 11 | 12 | public static InnerLogger Instance { get; private set; } 13 | 14 | public class InnerLogger 15 | { 16 | internal InnerLogger() 17 | { 18 | } 19 | 20 | public void Log(string message) 21 | { 22 | Console.WriteLine(message); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Logger/Logger.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Logger/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Logger 2 | { 3 | using System; 4 | 5 | using Singleton.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | int objectsCount = Constants.ObjectsCount; 12 | object[] logObjects = CreateObjects(() => Log.Instance, objectsCount); 13 | bool logObjectsAreEqual = ObjectsAreEqual(logObjects); 14 | Console.WriteLine(PrintMessages.AllCreatedObjects, nameof(Log), logObjectsAreEqual); 15 | 16 | Console.WriteLine(new string('-', 50)); 17 | 18 | object[] loggerObjects = CreateObjects(() => Logger.Instance, objectsCount); 19 | bool loggerObjectsAreEqual = ObjectsAreEqual(loggerObjects); 20 | Console.WriteLine(PrintMessages.AllCreatedObjects, nameof(Logger), loggerObjectsAreEqual); 21 | 22 | Console.WriteLine(new string('-', 50)); 23 | 24 | // Second implementation usnig static constructor demo 25 | // Keep in mind the the cliet can create instances of InnerLogger 26 | var logger = new Logger.InnerLogger(); 27 | Logger.Instance.Log(PrintMessages.SuccesfullyImpemented); 28 | } 29 | 30 | private static object[] CreateObjects(Func objectFactory, int objectsCount) 31 | { 32 | object[] objects = new object[objectsCount]; 33 | 34 | for (int i = 0; i < objects.Length; i++) 35 | { 36 | objects[i] = objectFactory(); 37 | } 38 | 39 | return objects; 40 | } 41 | 42 | private static bool ObjectsAreEqual(object[] objects) 43 | { 44 | bool objectsAreEqual = true; 45 | //Check if all objects have the same instances 46 | for (int i = 0; i < objects.Length; i++) 47 | { 48 | if (!objectsAreEqual) 49 | { 50 | break; 51 | } 52 | 53 | for (int j = 0; j < objects.Length; j++) 54 | { 55 | if (i != j && !objects[i].Equals(objects[j])) 56 | { 57 | objectsAreEqual = false; 58 | break; 59 | } 60 | } 61 | } 62 | 63 | return objectsAreEqual; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Singleton.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.Common 2 | { 3 | public class Constants 4 | { 5 | public const int ObjectsCount = 10000; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Singleton.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Instance = "Singleton objects have the same instances => are equal: {0}"; 6 | public const string AllCreatedObjects = "All created {0} objects have the same instances => are equal: {1}"; 7 | public const string SuccesfullyImpemented = "Successfully implemented singleton pattern!"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /CreationalPatterns/Singleton/Singleton.Common/Singleton.Common.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Atanas Vasilev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Software Design Patterns 2 | Implementations and demos of the most popular creational, behavioral and structural software design patterns. 3 | 4 | ## [Creational Design Patterns](https://github.com/NaskoVasilev/Design-Patterns/tree/master/CreationalPatterns) 5 | - Abstract Factory 6 | - Builder 7 | - Factory Method 8 | - Factory 9 | - Prototype 10 | - Simple Factory 11 | - Singleton 12 | 13 | ## [Structural Design Patterns](https://github.com/NaskoVasilev/Design-Patterns/tree/master/StructuralPatterns) 14 | - Adapter 15 | - Bridge 16 | - Composite 17 | - Decorator 18 | - Facade 19 | - Flyweight 20 | - Proxy 21 | 22 | ## [Behavioral Design Patterns](https://github.com/NaskoVasilev/Design-Patterns/tree/master/BehavioralPatterns) 23 | - Chain Of Responsibility 24 | - Command 25 | - Interpreter 26 | - Iterator 27 | - Mediator 28 | - Memento 29 | - Observer 30 | - State 31 | - Strategy 32 | - Template Method 33 | - Visitor 34 | 35 | ## More examples and UML diagrams - [link](https://dofactory.com/net/design-patterns) 36 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Adapter.Common/Adapter.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Adapter.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Adapter.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayChocolateCake = "Chocolate Cake"; 6 | public const string DisplayCheeseCake = "Cheese Cake"; 7 | public const string DisplayIceCream = "Ice Cream"; 8 | 9 | public const string DisplayWater = "Water"; 10 | public const string DisplayBenzene = "Benzene"; 11 | public const string DisplayEthanol = "Ethanol"; 12 | public const string DisplayWaterCompound = "water"; 13 | public const string DisplayBenzeneCompound = "benzene"; 14 | public const string DisplayEthanolCompound = "ethanol"; 15 | public const string DisplayCompound = "Compound"; 16 | public const string DisplayFormula = "Formula"; 17 | public const string DisplayWeight = "Weight"; 18 | public const string DisplayMeltingPoint = "Melting Point"; 19 | public const string DisplayBoilingPoint = "Boiling Point"; 20 | public const string SignMeltingPoint = "M"; 21 | public const string SignBoilingPoint = "B"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Adapter.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Adapter.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Cafeteria = "{0} costs {1}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Adapter.Common/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Adapter.Common 2 | { 3 | public static class StringExtensions 4 | { 5 | public static string ToCurrency(double num) => num.ToString("C"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/Cafeteria.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/CoffeeShop.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Adapter.Common; 7 | using Cafeteria.Models; 8 | 9 | /// 10 | /// The 'ConcreteAdapter' class 11 | /// 12 | public class CoffeeShop 13 | { 14 | private readonly DessertsAdapter dessertsAdapter; 15 | 16 | public CoffeeShop() 17 | { 18 | this.dessertsAdapter = new DessertsAdapter(); 19 | } 20 | 21 | public void DisplayDesserts() 22 | { 23 | IList desserts = this.dessertsAdapter.GetDessertsList(); 24 | 25 | foreach (DessertInfo dessertInfo in desserts) 26 | { 27 | var price = StringExtensions.ToCurrency(dessertInfo.Price); 28 | Console.WriteLine(PrintMessages.Cafeteria, dessertInfo.Name, price); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/DessertStorage.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria 2 | { 3 | using System.Collections.Generic; 4 | 5 | using Adapter.Common; 6 | using Cafeteria.Models; 7 | 8 | /// 9 | /// The 'Adaptee' class 10 | /// 11 | public class DessertStorage 12 | { 13 | private readonly List desserts; 14 | 15 | public DessertStorage() 16 | { 17 | this.desserts = new List 18 | { 19 | new Dessert { Name = Constants.DisplayChocolateCake, Price = 4.50, Quantity = 12 }, 20 | new Dessert { Name = Constants.DisplayChocolateCake, Price = 5.20, Quantity = 10 }, 21 | new Dessert { Name = Constants.DisplayChocolateCake, Price = 2.90, Quantity = 20 } 22 | }; 23 | } 24 | 25 | public List GetDesserts() => this.desserts; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/DessertsAdapter.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria 2 | { 3 | using System.Collections.Generic; 4 | 5 | using Cafeteria.Models; 6 | 7 | /// 8 | /// The 'Adapter' class 9 | /// 10 | public class DessertsAdapter 11 | { 12 | private readonly DessertStorage dessertShop; 13 | 14 | public DessertsAdapter() 15 | { 16 | this.dessertShop = new DessertStorage(); 17 | } 18 | 19 | public IList GetDessertsList() 20 | { 21 | List desserts = this.dessertShop.GetDesserts(); 22 | List dessertsList = new(); 23 | 24 | foreach (Dessert dessert in desserts) 25 | { 26 | DessertInfo dessertInfo = new() 27 | { 28 | Name = dessert.Name, 29 | Price = dessert.Price 30 | }; 31 | 32 | dessertsList.Add(dessertInfo); 33 | } 34 | 35 | return dessertsList; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/Models/Dessert.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria.Models 2 | { 3 | public class Dessert 4 | { 5 | public string Name { get; set; } 6 | 7 | public double Price { get; set; } 8 | 9 | public int Quantity { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/Models/DessertInfo.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria.Models 2 | { 3 | public class DessertInfo 4 | { 5 | public string Name { get; set; } 6 | 7 | public double Price { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/Cafeteria/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Cafeteria 2 | { 3 | public class Program 4 | { 5 | public static void Main() 6 | { 7 | CoffeeShop coffeeShop = new(); 8 | coffeeShop.DisplayDesserts(); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/ChemicalCompounds/ChemicalCompounds.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/ChemicalCompounds/ChemicalDatabank.cs: -------------------------------------------------------------------------------- 1 | namespace ChemicalCompounds 2 | { 3 | using Adapter.Common; 4 | 5 | /// 6 | /// The 'Adaptee' class 7 | /// 8 | internal class ChemicalDatabank 9 | { 10 | // The databank leggacy API 11 | public float GetCriticalPoint(string compound, string point) 12 | { 13 | if (point == Constants.SignMeltingPoint) 14 | { 15 | // Melting point 16 | return (compound.ToLower()) switch 17 | { 18 | Constants.DisplayWaterCompound => 0.0f, 19 | Constants.DisplayBenzeneCompound => 5.5f, 20 | Constants.DisplayEthanolCompound => -114.1f, 21 | _ => 0f, 22 | }; 23 | } 24 | else 25 | { 26 | // Boiling point 27 | return (compound.ToLower()) switch 28 | { 29 | Constants.DisplayWaterCompound => 100.0f, 30 | Constants.DisplayBenzeneCompound => 80.1f, 31 | Constants.DisplayEthanolCompound => 78.3f, 32 | _ => 0f, 33 | }; 34 | } 35 | } 36 | 37 | public string GetMolecularStructure(string compound) 38 | { 39 | return compound.ToLower() switch 40 | { 41 | Constants.DisplayWaterCompound => "H2O", 42 | Constants.DisplayBenzeneCompound => "C6H6", 43 | Constants.DisplayEthanolCompound => "C2H5OH", 44 | _ => null, 45 | }; 46 | } 47 | 48 | public double GetMolecularWeigth(string compound) 49 | { 50 | return compound.ToLower() switch 51 | { 52 | Constants.DisplayWaterCompound => 18.015, 53 | Constants.DisplayBenzeneCompound => 78.1134, 54 | Constants.DisplayEthanolCompound => 46.0688, 55 | _ => 0, 56 | }; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/ChemicalCompounds/ICompound.cs: -------------------------------------------------------------------------------- 1 | namespace ChemicalCompounds 2 | { 3 | /// 4 | /// The 'Adapter' interface 5 | /// 6 | public interface ICompound 7 | { 8 | void Display(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/ChemicalCompounds/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ChemicalCompounds 2 | { 3 | using Adapter.Common; 4 | 5 | public class Program 6 | { 7 | public static void Main() 8 | { 9 | ICompound water = new RichCompound(Constants.DisplayWater); 10 | water.Display(); 11 | 12 | ICompound benzene = new RichCompound(Constants.DisplayBenzene); 13 | benzene.Display(); 14 | 15 | ICompound ethanol = new RichCompound(Constants.DisplayEthanol); 16 | ethanol.Display(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /StructuralPatterns/Adapter/ChemicalCompounds/RichCompound.cs: -------------------------------------------------------------------------------- 1 | namespace ChemicalCompounds 2 | { 3 | using System; 4 | 5 | using Adapter.Common; 6 | 7 | /// 8 | /// The 'ConcreteAdapter' class 9 | /// 10 | public class RichCompound : ICompound 11 | { 12 | private readonly string chemical; 13 | private readonly ChemicalDatabank chemicalBank; 14 | private const string MessageTemplate = "{0}: {1}"; 15 | 16 | public RichCompound(string chemical) 17 | { 18 | this.chemical = chemical; 19 | this.chemicalBank = new ChemicalDatabank(); 20 | } 21 | 22 | public void Display() 23 | { 24 | var mocelularStructure = this.chemicalBank.GetMolecularStructure(this.chemical); 25 | var mocelularWeight = this.chemicalBank.GetMolecularWeigth(this.chemical); 26 | var meltingPoint = this.chemicalBank.GetCriticalPoint(this.chemical, Constants.SignMeltingPoint); 27 | var boilingPoint = this.chemicalBank.GetCriticalPoint(this.chemical, Constants.SignBoilingPoint); 28 | 29 | Console.WriteLine(MessageTemplate, Constants.DisplayCompound, this.chemical); 30 | Console.WriteLine(MessageTemplate, Constants.DisplayFormula, mocelularStructure); 31 | Console.WriteLine(MessageTemplate, Constants.DisplayWeight, mocelularWeight); 32 | 33 | Console.WriteLine(MessageTemplate, Constants.DisplayMeltingPoint, meltingPoint); 34 | Console.WriteLine(MessageTemplate + Environment.NewLine, Constants.DisplayBoilingPoint, boilingPoint); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/Bridge.Common/Bridge.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/Bridge.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Bridge.Common 2 | { 3 | public class Constants 4 | { 5 | public const string Title = "The Bridge Pattern"; 6 | public const string QuestionOne = "What is it?"; 7 | public const string QuestionOneAnswer = "A design pattern."; 8 | public const string QuestionTwo = "When do we use it?"; 9 | public const string QuestionTwoAnswer = "When you need to separate the abstraction from an implementation."; 10 | public const string BookAuthor = "John Sonmez"; 11 | public const string BookTitle = "The Complete Software Developer’s Career Guide"; 12 | public const string BookText = "What do I actually need to know to be a software developer?"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/AbstractionModels/Book.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.AbstractionModels 2 | { 3 | using System; 4 | 5 | using ManuscriptMaker.ImplementatorModels; 6 | 7 | /// 8 | /// The 'RefinedAbstraction' class 9 | /// 10 | public class Book : Manuscript 11 | { 12 | public Book(IFormatter formatter) : base(formatter) 13 | { 14 | } 15 | 16 | public string Title { get; set; } 17 | 18 | public string Author { get; set; } 19 | 20 | public string Text { get; set; } 21 | 22 | public override void Print() 23 | { 24 | Console.WriteLine(Formatter.Format(nameof(Title), Title)); 25 | Console.WriteLine(Formatter.Format(nameof(Author), Author)); 26 | Console.WriteLine(Formatter.Format(nameof(Text), Text)); 27 | Console.WriteLine(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/AbstractionModels/Document.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.AbstractionModels 2 | { 3 | using ManuscriptMaker.ImplementatorModels; 4 | 5 | /// 6 | /// The 'Abstraction' class 7 | /// 8 | public abstract class Manuscript 9 | { 10 | protected Manuscript(IFormatter formatter) 11 | { 12 | Formatter = formatter; 13 | } 14 | 15 | protected IFormatter Formatter { get; private set; } 16 | 17 | public abstract void Print(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/AbstractionModels/FAQ.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.AbstractionModels 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using ManuscriptMaker.ImplementatorModels; 7 | 8 | /// 9 | /// The 'RefinedAbstraction' class 10 | /// 11 | public class FAQ : Manuscript 12 | { 13 | public FAQ(IFormatter formatter) : base(formatter) 14 | { 15 | Questions = new Dictionary(); 16 | } 17 | 18 | public string Title { get; set; } 19 | 20 | public Dictionary Questions { get; set; } 21 | 22 | public override void Print() 23 | { 24 | Console.WriteLine(Formatter.Format(nameof(Title), Title)); 25 | 26 | foreach (var question in Questions) 27 | { 28 | Console.WriteLine(Formatter.Format($" Question", question.Key)); 29 | Console.WriteLine(Formatter.Format($" Answer", question.Value)); 30 | } 31 | 32 | Console.WriteLine(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/ImplementatorModels/FancyFormatter.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.ImplementatorModels 2 | { 3 | /// 4 | /// The 'ConcreteImplementorA' class 5 | /// 6 | public class FancyFormatter : IFormatter 7 | { 8 | public string Format(string key, string value) => $"{key} => {value}"; 9 | } 10 | } -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/ImplementatorModels/IFormatter.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.ImplementatorModels 2 | { 3 | /// 4 | /// The 'Implementor' abstract class 5 | /// 6 | public interface IFormatter 7 | { 8 | string Format(string key, string value); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/ImplementatorModels/StandardFormatter.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker.ImplementatorModels 2 | { 3 | /// 4 | /// The 'ConcreteImplementorA' class 5 | /// 6 | public class StandardFormatter : IFormatter 7 | { 8 | public string Format(string key, string value) => $"{key}: {value}"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/ManuscriptMaker.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Bridge/ManuscriptMaker/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ManuscriptMaker 2 | { 3 | using System.Collections.Generic; 4 | 5 | using Bridge.Common; 6 | using ManuscriptMaker.AbstractionModels; 7 | using ManuscriptMaker.ImplementatorModels; 8 | 9 | public class Program 10 | { 11 | public static void Main() 12 | { 13 | var manuscripts = new List(); 14 | var fancyFormatter = new FancyFormatter(); 15 | var standardFormatter = new StandardFormatter(); 16 | 17 | var faq = new FAQ(standardFormatter) 18 | { 19 | Title = Constants.Title 20 | }; 21 | 22 | faq.Questions.Add(Constants.QuestionOne, Constants.QuestionOneAnswer); 23 | faq.Questions.Add(Constants.QuestionTwo, Constants.QuestionTwoAnswer); 24 | manuscripts.Add(faq); 25 | 26 | var book = new Book(fancyFormatter) 27 | { 28 | Author = Constants.BookAuthor, 29 | Title = Constants.BookTitle, 30 | Text = Constants.BookText, 31 | }; 32 | 33 | manuscripts.Add(book); 34 | 35 | foreach (var page in manuscripts) 36 | { 37 | page.Print(); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/Composite.Common/Composite.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/Composite.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Composite.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayCEO = "CEO"; 6 | public const string DisplayBackEndTeamLeader = "Back-end Team Leader"; 7 | public const string DisplayFrontEndTeamLeader = "Front-end Team Leader"; 8 | public const string DisplayCSharpSenior = "C# Senior Developer"; 9 | public const string DisplayPythonSenior = "Python Senior Developer"; 10 | public const string DisplayAngularSenior = "Angular Senior Developer"; 11 | public const string DisplayBlazorSenior = "Blazor Senior Developer"; 12 | public const string DisplayJuniorAngular = "Junior Angular Developer"; 13 | public const string DisplayJuniorReact = "Junior React Developer"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/Composite.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Composite.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Employee = "Id = {0}, Role = {1}"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/EmployeeITStructure/Contractor.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeITStructure 2 | { 3 | using Composite.Common; 4 | 5 | /// 6 | /// The 'Leaf' class 7 | /// 8 | public class Contractor : IEmployee 9 | { 10 | public int Id { get; set; } 11 | 12 | public string Position { get; set; } 13 | 14 | public override string ToString() => string.Format(PrintMessages.Employee, this.Id, this.Position); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/EmployeeITStructure/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeITStructure 2 | { 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | 6 | using Composite.Common; 7 | 8 | /// 9 | /// The 'Composite' class 10 | /// 11 | public class Employee : IEmployee, IEnumerable 12 | { 13 | private readonly Dictionary subordinates = new(); 14 | 15 | public int Id { get; set; } 16 | 17 | public string Position { get; set; } 18 | 19 | public void AddSubordinate(IEmployee subordinate) => this.subordinates.Add(subordinate.Id, subordinate); 20 | 21 | public void RemoveSubordinate(int subordinateId) => this.subordinates.Remove(subordinateId); 22 | 23 | public IEmployee GetSubordinate(int subordinateId) => this.subordinates[subordinateId]; 24 | 25 | public IEnumerator GetEnumerator() 26 | { 27 | foreach (var subordinate in this.subordinates.Values) 28 | { 29 | yield return subordinate; 30 | } 31 | } 32 | 33 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 34 | 35 | public override string ToString() => string.Format(PrintMessages.Employee, this.Id, this.Position); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/EmployeeITStructure/EmployeeITStructure.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/EmployeeITStructure/IEmployee.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeITStructure 2 | { 3 | /// 4 | /// The 'Component' Treenode 5 | /// 6 | public interface IEmployee 7 | { 8 | int Id { get; set; } 9 | 10 | string Position { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Composite/EmployeeITStructure/Program.cs: -------------------------------------------------------------------------------- 1 | namespace EmployeeITStructure 2 | { 3 | using System; 4 | 5 | using Composite.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | Employee boss = new() { Id = 1, Position = Constants.DisplayCEO }; 12 | 13 | Employee backEndTeamLeader = new() { Id = 2, Position = Constants.DisplayBackEndTeamLeader }; 14 | Employee frontEndTeamLeader = new() { Id = 3, Position = Constants.DisplayFrontEndTeamLeader }; 15 | 16 | boss.AddSubordinate(backEndTeamLeader); 17 | boss.AddSubordinate(frontEndTeamLeader); 18 | 19 | Employee cSharpSeniorDeveloper = new() { Id = 4, Position = Constants.DisplayCSharpSenior }; 20 | Employee pythonSeniorDeveloper = new() { Id = 5, Position = Constants.DisplayPythonSenior }; 21 | 22 | backEndTeamLeader.AddSubordinate(cSharpSeniorDeveloper); 23 | backEndTeamLeader.AddSubordinate(pythonSeniorDeveloper); 24 | 25 | Employee angularSeniorDeveloper = new() { Id = 6, Position = Constants.DisplayAngularSenior }; 26 | Employee blazorSeniorDeveloper = new() { Id = 7, Position = Constants.DisplayBlazorSenior }; 27 | 28 | Contractor juniorAngularDeveloper = new() { Id = 8, Position = Constants.DisplayJuniorAngular }; 29 | Contractor juniorReactDevelope = new() { Id = 9, Position = Constants.DisplayJuniorReact }; 30 | 31 | frontEndTeamLeader.AddSubordinate(angularSeniorDeveloper); 32 | frontEndTeamLeader.AddSubordinate(blazorSeniorDeveloper); 33 | frontEndTeamLeader.AddSubordinate(juniorAngularDeveloper); 34 | frontEndTeamLeader.AddSubordinate(juniorReactDevelope); 35 | 36 | Console.WriteLine(boss); 37 | 38 | foreach (Employee manager in boss) 39 | { 40 | Console.WriteLine($"\t" + manager); 41 | 42 | foreach (var employee in manager) 43 | { 44 | Console.WriteLine("\t\t" + employee); 45 | } 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/Decorator.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayDirector = "Director"; 6 | public const string DisplayAuthor = "Author"; 7 | public const string DisplayTitle = "Title"; 8 | public const string DisplayCopies = "# Copies"; 9 | public const string DisplayPlayTime = "PlayTime"; 10 | 11 | public const string DisplayMicrosoft = "Microsoft"; 12 | public const string DisplayAspNetCore = "ASP.NET Core"; 13 | public const string DisplayNetConference = ".NET conference"; 14 | public const string DisplayBlazor = "Blazor"; 15 | public const string FirstTestUser = "Atanas Vasilev"; 16 | public const string SecondTestUser = "Pesho Georgiev"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/Decorator.Common/Decorator.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/Decorator.Common/ErrorMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator.Common 2 | { 3 | public class ErrorMessages 4 | { 5 | public const string NotEnoughCopies = "There are no enough copies of the item in the library."; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/Decorator.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Decorator.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Template = " {0}: {1}\n"; 6 | public const string Borrower = " Borrower: {0}"; 7 | 8 | public const string Video = "\nVideo ---------- \n"; 9 | public const string Book = "\nBook ---------- \n"; 10 | 11 | public const string MakingBorrowable = "\nMaking {0} borrowable."; 12 | public const string ReturnItem = "\n{0} returned the {1}."; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/ComponentModels/Book.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | using System; 4 | using System.Text; 5 | 6 | using Decorator.Common; 7 | 8 | /// 9 | /// The 'ConcreteComponent' class 10 | /// 11 | internal class Book : LibraryItem 12 | { 13 | private readonly string author; 14 | private readonly string title; 15 | 16 | public Book(string author, string title, int copiesCount) 17 | { 18 | this.author = author; 19 | this.title = title; 20 | CopiesCount = copiesCount; 21 | } 22 | 23 | public override void Display() 24 | { 25 | var sb = new StringBuilder(); 26 | 27 | sb.AppendFormat(PrintMessages.Book); 28 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayAuthor, this.author); 29 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayTitle, this.title); 30 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayCopies, base.CopiesCount); 31 | 32 | string result = sb.ToString().TrimEnd(); 33 | 34 | Console.WriteLine(result); 35 | 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/ComponentModels/LibraryItem.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | /// 4 | /// The 'Component' abstract class 5 | /// 6 | internal abstract class LibraryItem 7 | { 8 | public int CopiesCount { get; set; } 9 | 10 | public abstract void Display(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/ComponentModels/Video.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | using System; 4 | using System.Text; 5 | 6 | using Decorator.Common; 7 | 8 | /// 9 | /// The 'ConcreteComponent' class 10 | /// 11 | internal class Video : LibraryItem 12 | { 13 | private readonly string director; 14 | private readonly string title; 15 | private readonly int playTime; 16 | 17 | public Video(string director, string title, int playTime, int copiesCount) 18 | { 19 | this.director = director; 20 | this.title = title; 21 | this.playTime = playTime; 22 | base.CopiesCount = copiesCount; 23 | } 24 | 25 | public override void Display() 26 | { 27 | var sb = new StringBuilder(); 28 | 29 | sb.AppendFormat(PrintMessages.Video); 30 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayDirector, this.director); 31 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayTitle, this.title); 32 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayCopies, base.CopiesCount); 33 | sb.AppendFormat(PrintMessages.Template, Constants.DisplayPlayTime, this.playTime); 34 | 35 | string result = sb.ToString().TrimEnd(); 36 | 37 | Console.WriteLine(result); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/DecoratorModels/BorrowableLibraryItem.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Decorator.Common; 7 | 8 | /// 9 | /// The 'ConcreteDecorator' class 10 | /// 11 | internal class BorrowableLibraryItem : LibraryItemDecorator 12 | { 13 | private readonly List borrowers = new(); 14 | 15 | public BorrowableLibraryItem(LibraryItem libraryItem) 16 | : base(libraryItem) 17 | { 18 | } 19 | 20 | public void BorrowItem(string name) 21 | { 22 | if(LibraryItem.CopiesCount == 0) 23 | { 24 | throw new InvalidOperationException(ErrorMessages.NotEnoughCopies); 25 | } 26 | 27 | this.borrowers.Add(name); 28 | LibraryItem.CopiesCount--; 29 | } 30 | 31 | public void ReturnItem(string name) 32 | { 33 | this.borrowers.Remove(name); 34 | LibraryItem.CopiesCount++; 35 | } 36 | 37 | public override void Display() 38 | { 39 | base.Display(); 40 | 41 | foreach (var borrower in this.borrowers) 42 | { 43 | Console.WriteLine(PrintMessages.Borrower, borrower); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/DecoratorModels/LibraryItemDecorator.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | /// 4 | /// The 'Decorator' abstract class 5 | /// 6 | internal abstract class LibraryItemDecorator : LibraryItem 7 | { 8 | protected LibraryItemDecorator(LibraryItem libraryItem) 9 | { 10 | LibraryItem = libraryItem; 11 | } 12 | 13 | protected LibraryItem LibraryItem { get; private set; } 14 | 15 | public override void Display() => LibraryItem.Display(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/ItemRent.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Decorator/ItemRent/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ItemRent 2 | { 3 | using System; 4 | 5 | using Decorator.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | //Create book 12 | var book = new Book(Constants.DisplayMicrosoft, Constants.DisplayAspNetCore, 10); 13 | book.Display(); 14 | 15 | //Create video 16 | var video = new Video(Constants.DisplayNetConference, Constants.DisplayBlazor, 120, 22); 17 | video.Display(); 18 | 19 | Console.WriteLine(PrintMessages.MakingBorrowable, nameof(video).ToLower()); 20 | 21 | var borrowableVideo = new BorrowableLibraryItem(video); 22 | borrowableVideo.BorrowItem(Constants.FirstTestUser); 23 | borrowableVideo.BorrowItem(Constants.SecondTestUser); 24 | 25 | borrowableVideo.Display(); 26 | 27 | borrowableVideo.ReturnItem(Constants.FirstTestUser); 28 | 29 | Console.WriteLine(PrintMessages.ReturnItem, Constants.FirstTestUser, nameof(video).ToLower()); 30 | borrowableVideo.Display(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/Bar.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/BarFacade.cs: -------------------------------------------------------------------------------- 1 | namespace Bar 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | using Bar.Models; 7 | using Facade.Common; 8 | 9 | /// 10 | /// The 'Facade' class 11 | /// 12 | public class BarFacade 13 | { 14 | Order order = new Order 15 | { 16 | Drinks = new List 17 | { 18 | new Drink { Name = Constants.DisplayWine, Price = 5.50 }, 19 | new Drink { Name = Constants.DisplayWhisky, Price = 5.90 }, 20 | new Drink { Name = Constants.DisplayBeer, Price = 3.20 } 21 | } 22 | }; 23 | 24 | public void Pay() 25 | { 26 | Calculator calc = new Calculator(); 27 | var sum = StringExtensions.ToCurrency(calc.Sum(order)); 28 | Console.WriteLine(PrintMessages.Bar, sum); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/Models/Calculator.cs: -------------------------------------------------------------------------------- 1 | namespace Bar.Models 2 | { 3 | /// 4 | /// The 'Subsystem ClassC' class 5 | /// 6 | public class Calculator 7 | { 8 | public double Sum(Order order) 9 | { 10 | double sum = 0; 11 | 12 | foreach (Drink drink in order.Drinks) 13 | { 14 | sum += drink.Price; 15 | } 16 | 17 | return sum; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/Models/Drink.cs: -------------------------------------------------------------------------------- 1 | namespace Bar.Models 2 | { 3 | /// 4 | /// The 'Subsystem ClassB' class 5 | /// 6 | public class Drink 7 | { 8 | public string Name { get; set; } 9 | 10 | public double Price { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/Models/Order.cs: -------------------------------------------------------------------------------- 1 | namespace Bar.Models 2 | { 3 | using System.Collections.Generic; 4 | 5 | /// 6 | /// The 'Subsystem ClassA' class 7 | /// 8 | public class Order 9 | { 10 | public List Drinks { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Bar/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Bar 2 | { 3 | public class Program 4 | { 5 | public static void Main() 6 | { 7 | BarFacade facade = new BarFacade(); 8 | facade.Pay(); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Facade.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Facade.Common 2 | { 3 | public class Constants 4 | { 5 | public const string DisplayWine = "Wine"; 6 | public const string DisplayWhisky = "Whisky"; 7 | public const string DisplayBeer = "Beer"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Facade.Common/Facade.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Facade.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Facade.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string Bar = "You paid: {0}"; 6 | 7 | public const string CarAccessories = " Car Accessories - Set Accessories"; 8 | public const string CarBody = " Car Body - Set Body"; 9 | public const string CarEngine = " Car Engine - Set Engine"; 10 | public const string CarModel = " Car Model - Set Model"; 11 | public const string CreatingCar = "******** Creating a Car **********\n"; 12 | public const string CreatedCar = "\n******** Car creation complete **********"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Facade.Common/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Facade.Common 2 | { 3 | public static class StringExtensions 4 | { 5 | public static string ToCurrency(double num) => num.ToString("C"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Garage.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/GarageFacade.cs: -------------------------------------------------------------------------------- 1 | namespace Garage 2 | { 3 | using System; 4 | 5 | using Facade.Common; 6 | using Garage.Models; 7 | 8 | /// 9 | /// The 'Facade' class 10 | /// 11 | public class GarageFacade 12 | { 13 | private readonly CarModel model; 14 | private readonly CarEngine engine; 15 | private readonly CarBody body; 16 | private readonly CarAccessories accessories; 17 | 18 | public GarageFacade() 19 | { 20 | model = new CarModel(); 21 | engine = new CarEngine(); 22 | body = new CarBody(); 23 | accessories = new CarAccessories(); 24 | } 25 | 26 | public void CreateCompleteCar() 27 | { 28 | Console.WriteLine(PrintMessages.CreatingCar); 29 | 30 | model.SetModel(); 31 | engine.SetEngine(); 32 | body.SetBody(); 33 | accessories.SetAccessories(); 34 | 35 | Console.WriteLine(PrintMessages.CreatedCar); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Models/CarAccessories.cs: -------------------------------------------------------------------------------- 1 | namespace Garage.Models 2 | { 3 | using System; 4 | 5 | using Facade.Common; 6 | 7 | /// 8 | /// The 'Subsystem ClassD' class 9 | /// 10 | internal class CarAccessories 11 | { 12 | public void SetAccessories() 13 | { 14 | Console.WriteLine(PrintMessages.CarAccessories); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Models/CarBody.cs: -------------------------------------------------------------------------------- 1 | namespace Garage.Models 2 | { 3 | using System; 4 | 5 | using Facade.Common; 6 | 7 | /// 8 | /// The 'Subsystem ClassC' class 9 | /// 10 | internal class CarBody 11 | { 12 | public void SetBody() 13 | { 14 | Console.WriteLine(PrintMessages.CarBody); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Models/CarEngine.cs: -------------------------------------------------------------------------------- 1 | namespace Garage.Models 2 | { 3 | using System; 4 | 5 | using Facade.Common; 6 | 7 | /// 8 | /// The 'Subsystem ClassB' class 9 | /// 10 | internal class CarEngine 11 | { 12 | public void SetEngine() 13 | { 14 | Console.WriteLine(PrintMessages.CarEngine); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Models/CarModel.cs: -------------------------------------------------------------------------------- 1 | namespace Garage.Models 2 | { 3 | using System; 4 | 5 | using Facade.Common; 6 | 7 | /// 8 | /// The 'Subsystem ClassA' class 9 | /// 10 | internal class CarModel 11 | { 12 | public void SetModel() 13 | { 14 | Console.WriteLine(PrintMessages.CarModel); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Facade/Garage/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Garage 2 | { 3 | public class Program 4 | { 5 | public static void Main() 6 | { 7 | GarageFacade carFacade = new(); 8 | carFacade.CreateCompleteCar(); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/CharacterFactory.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize 2 | { 3 | using System.Collections.Generic; 4 | 5 | using CharacterPointSize.Models; 6 | using Flyweight.Common; 7 | 8 | /// 9 | /// The 'FlyweigthFactory' class 10 | /// 11 | public class CharacterFactory 12 | { 13 | private readonly Dictionary characters = new(); 14 | 15 | public Character GetCharacter(char key) 16 | { 17 | //Uses lazy inizialization 18 | Character character = null; 19 | if (this.characters.ContainsKey(key)) 20 | { 21 | character = this.characters[key]; 22 | } 23 | else 24 | { 25 | switch (key) 26 | { 27 | case Constants.SymbolA: 28 | character = new CharacterA(); 29 | break; 30 | case Constants.SymbolB: 31 | character = new CharacterB(); 32 | break; 33 | case Constants.SymbolZ: 34 | character = new CharacterZ(); 35 | break; 36 | } 37 | 38 | this.characters[key] = character; 39 | } 40 | 41 | return character; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/CharacterPointSize.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/Models/Character.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize.Models 2 | { 3 | using System; 4 | 5 | using Flyweight.Common; 6 | 7 | /// 8 | /// The 'Flyweight' abstract class 9 | /// 10 | public abstract class Character 11 | { 12 | protected char Symbol { get; set; } 13 | 14 | protected int Width { get; set; } 15 | 16 | protected int Height { get; set; } 17 | 18 | public void Display(int pointSize) 19 | { 20 | Console.WriteLine(PrintMessages.PointSize, this.Symbol, pointSize); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/Models/CharacterA.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize.Models 2 | { 3 | using Flyweight.Common; 4 | 5 | /// 6 | /// The 'Concrete Flyweight' class 7 | /// 8 | public class CharacterA : Character 9 | { 10 | public CharacterA() 11 | { 12 | base.Symbol = Constants.SymbolA; 13 | base.Height = 100; 14 | base.Width = 120; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/Models/CharacterB.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize.Models 2 | { 3 | using Flyweight.Common; 4 | 5 | /// 6 | /// The 'Concrete Flyweight' class 7 | /// 8 | public class CharacterB : Character 9 | { 10 | public CharacterB() 11 | { 12 | base.Symbol = Constants.SymbolB; 13 | base.Height = 80; 14 | base.Width = 120; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/Models/CharacterZ.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize.Models 2 | { 3 | using Flyweight.Common; 4 | 5 | /// 6 | /// The 'Concrete Flyweight' class 7 | /// 8 | public class CharacterZ : Character 9 | { 10 | public CharacterZ() 11 | { 12 | base.Symbol = Constants.SymbolZ; 13 | base.Height = 100; 14 | base.Width = 120; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/CharacterPointSize/Program.cs: -------------------------------------------------------------------------------- 1 | namespace CharacterPointSize 2 | { 3 | using CharacterPointSize.Models; 4 | using Flyweight.Common; 5 | 6 | public class Program 7 | { 8 | public static void Main() 9 | { 10 | string text = Constants.CharacterText; 11 | var factory = new CharacterFactory(); 12 | int pointSize = Constants.InitialPointSize; 13 | 14 | foreach (var c in text) 15 | { 16 | pointSize++; 17 | Character character = factory.GetCharacter(c); 18 | character.Display(pointSize); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/Flyweight.Common/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace Flyweight.Common 2 | { 3 | public class Constants 4 | { 5 | public const string CharacterText = "AAZZBBZBAABZ"; 6 | public const int InitialPointSize = 10; 7 | public const char SymbolA = 'A'; 8 | public const char SymbolB = 'B'; 9 | public const char SymbolZ = 'Z'; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/Flyweight.Common/Flyweight.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Flyweight/Flyweight.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Flyweight.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string PointSize = "{0} (point size {1})"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/MathProxy/IMath.cs: -------------------------------------------------------------------------------- 1 | namespace MathProxy 2 | { 3 | /// 4 | /// The 'Subject' interface 5 | /// 6 | public interface IMath 7 | { 8 | double Add(double x, double y); 9 | 10 | double Sub(double x, double y); 11 | 12 | double Multiply(double x, double y); 13 | 14 | double Divide(double x, double y); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/MathProxy/Math.cs: -------------------------------------------------------------------------------- 1 | namespace MathProxy 2 | { 3 | /// 4 | /// The 'Real Subject' class 5 | /// 6 | public class Math : IMath 7 | { 8 | public double Add(double x, double y) => x + y; 9 | 10 | public double Sub(double x, double y) => x - y; 11 | 12 | public double Multiply(double x, double y) => x * y; 13 | 14 | public double Divide(double x, double y) => x / y; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/MathProxy/MathProxy.cs: -------------------------------------------------------------------------------- 1 | namespace MathProxy 2 | { 3 | using System; 4 | 5 | /// 6 | /// The 'Proxy Object' class 7 | /// 8 | public class MathProxy : IMath 9 | { 10 | private readonly Math math = new(); 11 | 12 | public double Add(double x, double y) 13 | { 14 | //Do some security checks , parameters changes, logging, validations 15 | return math.Add(x, y); 16 | } 17 | 18 | public double Divide(double x, double y) 19 | { 20 | //Do some security checks , parameters changes, logging, validations 21 | if (x == 0 || y == 0) 22 | { 23 | throw new DivideByZeroException(); 24 | } 25 | 26 | return math.Divide(x, y); 27 | } 28 | 29 | public double Multiply(double x, double y) 30 | { 31 | //Do some security checks , parameters changes, logging, validations 32 | return math.Multiply(x, y); 33 | } 34 | 35 | public double Sub(double x, double y) 36 | { 37 | //Do some security checks , parameters changes, logging, validations 38 | return math.Sub(x, y); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/MathProxy/MathProxy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | Exe 4 | net6.0 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/MathProxy/Program.cs: -------------------------------------------------------------------------------- 1 | namespace MathProxy 2 | { 3 | using System; 4 | 5 | using Proxy.Common; 6 | 7 | public class Program 8 | { 9 | public static void Main() 10 | { 11 | Console.WriteLine(PrintMessages.PickNumbers); 12 | 13 | int firstNum = int.Parse(Console.ReadLine()); 14 | int secondNum = int.Parse(Console.ReadLine()); 15 | 16 | MathProxy proxy = new(); 17 | Console.WriteLine($"{firstNum} + {secondNum} = {proxy.Add(firstNum, secondNum)}"); 18 | Console.WriteLine($"{firstNum} - {secondNum} = {proxy.Sub(firstNum, secondNum)}"); 19 | Console.WriteLine($"{firstNum} * {secondNum} = {proxy.Multiply(firstNum, secondNum)}"); 20 | Console.WriteLine($"{firstNum} / {secondNum} = {proxy.Divide(firstNum, secondNum)}"); 21 | 22 | try 23 | { 24 | Console.WriteLine($"{firstNum} / 0 = {proxy.Divide(firstNum, 0)}"); 25 | } 26 | catch (DivideByZeroException ex) 27 | { 28 | Console.WriteLine(ex.Message); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/Proxy.Common/PrintMessages.cs: -------------------------------------------------------------------------------- 1 | namespace Proxy.Common 2 | { 3 | public class PrintMessages 4 | { 5 | public const string PickNumbers = "Pick two numbers:"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StructuralPatterns/Proxy/Proxy.Common/Proxy.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | --------------------------------------------------------------------------------