├── .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