Design Patterns in .NET
Design Patterns in .NET
|
Pattern |
Type |
Usage in .NET |
Example in .NET |
|
Singleton |
Creational |
Ensure a class has
only one instance across the application. |
DbContext in Entity
Framework for database connections. |
|
Factory Method |
Creational |
Create objects
without specifying the exact class. |
A factory to create
instances of ILogger implementations. |
|
Abstract Factory |
Creational |
Create families of
related objects without specifying classes. |
Dependency injection
container resolving services for multiple platforms. |
|
Builder |
Creational |
Build complex
objects step by step. |
StringBuilder class
for constructing complex strings. |
|
Prototype |
Creational |
Clone objects to
avoid costly instantiation. |
Cloning a complex
object using the ICloneable interface. |
|
Adapter |
Structural |
Allow incompatible
interfaces to work together. |
Adapting a
third-party library for use in .NET applications. |
|
Bridge |
Structural |
Decouple abstraction
from implementation. |
Separating business
logic and data access layers in a repository pattern. |
|
Composite |
Structural |
Treat individual
objects and compositions uniformly. |
Representing a tree
structure in WPF or WinForms UI. |
|
Decorator |
Structural |
Add functionality to
objects dynamically. |
Adding logging or
caching to HttpClient using DelegatingHandler. |
|
Facade |
Structural |
Provide a unified
interface to a complex subsystem. |
Wrapping multiple
services into a single service class in a web API. |
|
Flyweight |
Structural |
Share common state
to save memory. |
Sharing font objects
in a WPF application to reduce memory usage. |
|
Proxy |
Structural |
Control access to an
object. |
A WCF proxy for
accessing remote services. |
|
Chain of
Responsibility |
Behavioral |
Pass requests
through a chain of handlers. |
Middleware pipeline
in ASP.NET Core. |
|
Command |
Behavioral |
Encapsulate a
request as an object. |
Binding commands in
WPF using ICommand interface. |
|
Interpreter |
Behavioral |
Define and interpret
a language or grammar. |
Parsing and
executing LINQ expressions. |
|
Iterator |
Behavioral |
Provide sequential
access to a collection. |
Iterating through
collections using IEnumerable and IEnumerator. |
|
Mediator |
Behavioral |
Reduce direct
communication between objects. |
Implementing a
message bus or event aggregator in a WPF/MVVM application. |
|
Memento |
Behavioral |
Save and restore an
object's state. |
Implementing
undo/redo functionality in a text editor or a form. |
|
Observer |
Behavioral |
Notify dependent
objects of state changes. |
Using events and
delegates to notify subscribers in .NET. |
|
State |
Behavioral |
Change an object’s
behavior based on its state. |
State machine
implementation for workflow processing. |
|
Strategy |
Behavioral |
Select algorithms at
runtime. |
Different payment methods (credit card, PayPal). |
|
Template Method |
Behavioral |
Define the skeleton
of an algorithm with steps customizable. |
Defining reusable
logic in a base controller in ASP.NET MVC. |
|
Visitor |
Behavioral |
Add new operations
to existing object structures. |
Traversing an object
structure like XML documents using XmlDocument. |
|
Thread Pool |
Concurrency |
Manage multiple
threads efficiently. |
Using
Task.Factory.StartNew or ThreadPool for background tasks. |
|
Active Object |
Concurrency |
Decouple method
execution and invocation for asynchronous tasks. |
Asynchronous task
execution using Task and async/await. |
Comments
Post a Comment