Skip to content

Commit 8bc3dce

Browse files
committed
Initial commit
0 parents  commit 8bc3dce

File tree

123 files changed

+1799
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1799
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin
2+
obj
3+
packages
4+
.vscode
5+
.vs

README.md

+263

assets/Adapter.png

55.6 KB

assets/Behavioral.png

396 KB

assets/Builder.png

52 KB

assets/Command.png

65 KB

assets/Composite.png

64 KB

assets/Creational.png

437 KB

assets/Decorator.png

77.9 KB

assets/DesignPatterns.png

251 KB

assets/DesignPatternsCheatSheet.png

1.15 MB

assets/DesignPatternsTrap.jpg

26.5 KB

assets/FactoryMethod.png

62.8 KB

assets/Observer.png

71.9 KB

assets/Proxy.png

47 KB

assets/SelectPattern.png

418 KB

assets/Singleton.png

50.2 KB

assets/Solution.png

12.3 KB

assets/State.png

50.4 KB

assets/Strategy.png

68.4 KB

assets/Structural.png

409 KB

assets/TemplateMethod.png

66.9 KB
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/AbstractFactory/CsvParser.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class CsvParser : ICsvParser
2+
{
3+
public void Parse(string input)
4+
{
5+
Console.WriteLine($"Parsing CSV: {input}");
6+
}
7+
}

src/AbstractFactory/ICsvParser.cs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface ICsvParser
2+
{
3+
void Parse(string input);
4+
}

src/AbstractFactory/IJsonParser.cs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IJsonParser
2+
{
3+
void Parse(string input);
4+
}

src/AbstractFactory/IParserFactory.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public interface IParserFactory
2+
{
3+
IJsonParser CreateJsonParser();
4+
IXmlParser CreateXmlParser();
5+
ICsvParser CreateCsvParser();
6+
}

src/AbstractFactory/IXmlParser.cs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IXmlParser
2+
{
3+
void Parse(string input);
4+
}

src/AbstractFactory/JsonParser.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class JsonParser : IJsonParser
2+
{
3+
public void Parse(string input)
4+
{
5+
Console.WriteLine($"Parsing JSON: {input}");
6+
}
7+
}

src/AbstractFactory/ParserFactory.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class ParserFactory : IParserFactory
2+
{
3+
public IJsonParser CreateJsonParser()
4+
{
5+
return new JsonParser();
6+
}
7+
8+
public IXmlParser CreateXmlParser()
9+
{
10+
return new XmlParser();
11+
}
12+
13+
public ICsvParser CreateCsvParser()
14+
{
15+
return new CsvParser();
16+
}
17+
}

src/AbstractFactory/Program.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* In this example, IParserFactory acts as the abstract factory that defines a method
2+
* for each kind of parser product (IJsonParser, IXmlParser, ICsvParser).
3+
* The ParserFactory concrete factory implements these methods, creating instances of
4+
* the concrete products (JsonParser, XmlParser, CsvParser). This pattern allows you to
5+
* introduce new parser types or parser families (factories) without changing the client
6+
* code that uses the parsers, adhering to the open/closed principle. */
7+
8+
IParserFactory parserFactory = new ParserFactory();
9+
10+
var jsonParser = parserFactory.CreateJsonParser();
11+
jsonParser.Parse("{\"name\": \"John Doe\"}");
12+
13+
var xmlParser = parserFactory.CreateXmlParser();
14+
xmlParser.Parse("<name>John Doe</name>");
15+
16+
var csvParser = parserFactory.CreateCsvParser();
17+
csvParser.Parse("name,John Doe");

src/AbstractFactory/XmlParser.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class XmlParser : IXmlParser
2+
{
3+
public void Parse(string input)
4+
{
5+
Console.WriteLine($"Parsing XML: {input}");
6+
}
7+
}

src/Adapter/Adapter.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Adapter/IPaymentGateway.cs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IPaymentGateway
2+
{
3+
void ProcessPayment(string merchantId, decimal amount);
4+
}

src/Adapter/PayPal.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class PayPal
2+
{
3+
public void SendPayment(decimal amount)
4+
{
5+
Console.WriteLine($"Paying via PayPal: {amount}");
6+
}
7+
}

src/Adapter/PayPalAdapter.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class PayPalAdapter : IPaymentGateway
2+
{
3+
private readonly PayPal _payPal;
4+
5+
public PayPalAdapter(PayPal payPal)
6+
{
7+
_payPal = payPal;
8+
}
9+
10+
public void ProcessPayment(string merchantId, decimal amount)
11+
{
12+
_payPal.SendPayment(amount); // Translate and forward the call
13+
}
14+
}

src/Adapter/PaymentProcessor.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class PaymentProcessor
2+
{
3+
public void ProcessPayment(IPaymentGateway paymentGateway, string merchantId, decimal amount)
4+
{
5+
paymentGateway.ProcessPayment(merchantId, amount);
6+
}
7+
}

src/Adapter/Program.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* In this example, the PayPalAdapter and StripeAdapter classes adapt the
2+
* interfaces of the PayPal and Stripe payment services, respectively,
3+
* to the IPaymentGateway interface expected by your application.
4+
* This allows your application to support multiple payment gateways seamlessly,
5+
* facilitating easy integration and expansion with minimal changes to the core application code. */
6+
7+
IPaymentGateway paypalGateway = new PayPalAdapter(new PayPal());
8+
IPaymentGateway stripeGateway = new StripeAdapter(new Stripe());
9+
10+
PaymentProcessor processor = new PaymentProcessor();
11+
12+
// Process payment through PayPal
13+
processor.ProcessPayment(paypalGateway, "merchant123", 100.00m);
14+
15+
// Process payment through Stripe
16+
processor.ProcessPayment(stripeGateway, "merchant456", 200.00m);

src/Adapter/Stripe.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Stripe
2+
{
3+
public void MakePayment(string merchantId, decimal amount)
4+
{
5+
Console.WriteLine($"Paying via Stripe: {amount}");
6+
}
7+
}

src/Adapter/StripeAdapter.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class StripeAdapter : IPaymentGateway
2+
{
3+
private readonly Stripe _stripe;
4+
5+
public StripeAdapter(Stripe stripe)
6+
{
7+
_stripe = stripe;
8+
}
9+
10+
public void ProcessPayment(string merchantId, decimal amount)
11+
{
12+
_stripe.MakePayment(merchantId, amount); // Translate and forward the call
13+
}
14+
}

src/Bridge/Bridge.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Bridge/Document.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public abstract class Document
2+
{
3+
protected IRenderer renderer;
4+
5+
public Document(IRenderer renderer)
6+
{
7+
this.renderer = renderer;
8+
}
9+
10+
public abstract string Render();
11+
}

src/Bridge/HtmlRenderer.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class HtmlRenderer : IRenderer
2+
{
3+
public string RenderHeader(string text)
4+
{
5+
return $"<h1>{text}</h1>";
6+
}
7+
8+
public string RenderBody(string text)
9+
{
10+
return $"<p>{text}</p>";
11+
}
12+
13+
public string RenderFooter(string text)
14+
{
15+
return $"<footer>{text}</footer>";
16+
}
17+
}

src/Bridge/IRenderer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public interface IRenderer
2+
{
3+
string RenderHeader(string text);
4+
string RenderBody(string text);
5+
string RenderFooter(string text);
6+
}

src/Bridge/PdfRenderer.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class PdfRenderer : IRenderer
2+
{
3+
public string RenderHeader(string text)
4+
{
5+
return $"PDF Header: {text}";
6+
}
7+
8+
public string RenderBody(string text)
9+
{
10+
return $"PDF Body: {text}";
11+
}
12+
13+
public string RenderFooter(string text)
14+
{
15+
return $"PDF Footer: {text}";
16+
}
17+
}

src/Bridge/Program.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* In this example, IRenderer acts as the implementor interface in the Bridge pattern,
2+
* with HtmlRenderer and PdfRenderer serving as concrete implementations.
3+
* The Document class represents the abstraction, and ReportDocument is a
4+
* refined abstraction that uses an IRenderer for rendering.
5+
* This setup allows the rendering logic to vary independently
6+
* from the document's structure, demonstrating the Bridge pattern's
7+
* ability to separate abstraction from implementation. */
8+
9+
var htmlRenderer = new HtmlRenderer();
10+
var pdfRenderer = new PdfRenderer();
11+
12+
Document htmlDocument = new ReportDocument(htmlRenderer, "Report Title", "Report Body", "Report Footer");
13+
Console.WriteLine(htmlDocument.Render());
14+
15+
Document pdfDocument = new ReportDocument(pdfRenderer, "Report Title", "Report Body", "Report Footer");
16+
Console.WriteLine(pdfDocument.Render());

src/Bridge/ReportDocument.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class ReportDocument : Document
2+
{
3+
private string title;
4+
private string body;
5+
private string footer;
6+
7+
public ReportDocument(IRenderer renderer, string title, string body, string footer) : base(renderer)
8+
{
9+
this.title = title;
10+
this.body = body;
11+
this.footer = footer;
12+
}
13+
14+
public override string Render()
15+
{
16+
var header = renderer.RenderHeader(title);
17+
var bodyContent = renderer.RenderBody(body);
18+
var footerContent = renderer.RenderFooter(footer);
19+
return $"{header}\n{bodyContent}\n{footerContent}";
20+
}
21+
}

src/Builder/Builder.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Builder/Computer.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Computer
2+
{
3+
public string? CPU { get; set; }
4+
public string? RAM { get; set; }
5+
public string? HardDrive { get; set; }
6+
public string? GraphicsCard { get; set; }
7+
8+
public override string ToString()
9+
{
10+
return $"CPU: {CPU}, RAM: {RAM}, Hard Drive: {HardDrive}, Graphics Card: {GraphicsCard}";
11+
}
12+
}

src/Builder/ComputerDirector.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class ComputerDirector
2+
{
3+
private IComputerBuilder _builder;
4+
5+
public ComputerDirector(IComputerBuilder builder)
6+
{
7+
_builder = builder;
8+
}
9+
10+
public void ConstructComputer()
11+
{
12+
_builder.BuildCPU();
13+
_builder.BuildRAM();
14+
_builder.BuildHardDrive();
15+
_builder.BuildGraphicsCard();
16+
}
17+
18+
public Computer GetComputer()
19+
{
20+
return _builder.GetComputer();
21+
}
22+
}

src/Builder/GamingComputerBuilder.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class GamingComputerBuilder : IComputerBuilder
2+
{
3+
private Computer _computer = new Computer();
4+
5+
public void BuildCPU()
6+
{
7+
_computer.CPU = "High-End CPU";
8+
}
9+
10+
public void BuildRAM()
11+
{
12+
_computer.RAM = "32GB";
13+
}
14+
15+
public void BuildHardDrive()
16+
{
17+
_computer.HardDrive = "1TB SSD";
18+
}
19+
20+
public void BuildGraphicsCard()
21+
{
22+
_computer.GraphicsCard = "High-End Graphics Card";
23+
}
24+
25+
public Computer GetComputer()
26+
{
27+
return _computer;
28+
}
29+
}

src/Builder/IComputerBuilder.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public interface IComputerBuilder
2+
{
3+
void BuildCPU();
4+
void BuildRAM();
5+
void BuildHardDrive();
6+
void BuildGraphicsCard();
7+
Computer GetComputer();
8+
}

0 commit comments

Comments
 (0)