Skip to content

iamahsanmehmood/csharp-design-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# Design Patterns — Real-World Implementations

Clean, practical implementations of the most important design patterns in C#/.NET, using real-world business scenarios — POS systems, invoices, inventory, payments, and more.


📦 Patterns Included

Creational Patterns

Pattern File Real-World Example
Singleton DatabaseConnection.cs Thread-safe database connection manager
Factory Method PaymentProcessorFactory.cs Payment gateway selector (CreditCard, JazzCash, Bank Transfer)
Builder InvoiceBuilder.cs Fluent invoice generator with line items & tax

Behavioral Patterns

Pattern File Real-World Example
Observer OrderSystem.cs Order notification system (Email, SMS, Analytics)
Strategy PosCheckout.cs POS discount strategies (%, BOGO, Loyalty)
Command InventoryCommands.cs Inventory management with Undo/Redo

Structural Patterns

Pattern File Real-World Example
Adapter NotificationAdapters.cs Unified notification service (Twilio, SendGrid, Slack)
Decorator OrderDecorator.cs Restaurant order customization (toppings, combos)
Repository ProductRepository.cs Generic product CRUD with soft-delete

🚀 Quick Start

git clone https://github.com/iamahsanmehmood/csharp-design-patterns.git
cd csharp-design-patterns
dotnet run

The Program.cs entry point runs all pattern demos with formatted console output.


🗂️ Project Structure

csharp-design-patterns/
├── src/
│   ├── Creational/
│   │   ├── Singleton/      → Thread-safe lazy initialization
│   │   ├── Factory/        → Payment processor factory
│   │   └── Builder/        → Fluent invoice builder
│   ├── Behavioral/
│   │   ├── Observer/       → Event-driven order notifications
│   │   ├── Strategy/       → Interchangeable discount algorithms
│   │   └── Command/        → Undo/redo inventory operations
│   └── Structural/
│       ├── Adapter/        → Third-party API integration
│       ├── Decorator/      → Dynamic order customization
│       └── Repository/     → Data access abstraction
├── Program.cs
├── DesignPatterns.csproj
└── README.md

💡 Why These Examples?

Each pattern uses a real business scenario rather than abstract shapes or animals:

  • 🏪 POS Systems — Strategy, Observer, and Decorator patterns mirror actual point-of-sale logic
  • 💳 Payment Processing — Factory Method shows real gateway switching
  • 📦 Inventory — Command pattern with real undo/redo for stock adjustments
  • 📄 Invoicing — Builder pattern for constructing complex invoice documents
  • 🔔 Notifications — Adapter and Observer patterns for multi-channel alerting

These examples come from 5+ years of building production software at XechTech.


🛠️ Tech Stack

  • Language: C# 12
  • Framework: .NET 8
  • IDE: Visual Studio 2022 / VS Code
  • No external dependencies — pure .NET

📚 Learning Resources


👤 Author

Ahsan Mehmood — Full-Stack Developer & Technical Founder

LinkedIn GitHub XechTech


📄 License

This project is licensed under the MIT License. See LICENSE for details.

About

Clean, practical C# design pattern implementations using real-world business scenarios - POS systems, invoices, payments, inventory & more. Built with .NET 8.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages