Behavioral Design Patterns in C# / .NET

What are Behavioral Patterns?

Behavioral patterns focus on communication and interaction between objects.

They define:

  • How objects communicate
  • How responsibilities are distributed
  • How behavior changes dynamically

Purpose of Behavioral Patterns

  • Improve communication flow
  • Reduce tight coupling
  • Make behavior flexible
  • Simplify object interactions
  • Improve maintainability

Types of Behavioral Patterns

Pattern Purpose Real-Time Example
Strategy Change behavior dynamically Tax/Discount calculation
Observer Notify multiple objects Email/SMS notifications
Command Encapsulate requests Button click actions
Mediator Central communication control Chat system
State Change behavior based on state Order status
Chain of Responsibility Pass request through handlers Middleware pipeline
Template Method Define algorithm skeleton Report generation
Iterator Traverse collections List pagination
Memento Save/restore object state Undo feature
Visitor Add operations without modifying object Report export
Interpreter Interpret language/rules Expression evaluation

1. Strategy Pattern

Idea

Change behavior at runtime.

Real Scenario

Tax or discount calculation.

Example


ITaxStrategy strategy = new GstTax();

Use Cases

  • Payment processing
  • Pricing engines
  • Validation rules

2. Observer Pattern

Idea

One object notifies multiple objects.

Real Scenario

Order placed:

  • Email sent
  • SMS sent
  • Push notification sent

Use Cases

  • Event systems
  • Notification systems
  • Real-time updates

3. Command Pattern

Idea

Encapsulate request as an object.

Real Scenario

Button click:

  • Save
  • Delete
  • Update

Use Cases

  • Undo/redo
  • Queue processing
  • Task scheduling

4. Mediator Pattern

Idea

Central object manages communication.

Real Scenario

Chat application.

Use Cases

  • Chat systems
  • Service coordination
  • UI communication

5. State Pattern

Idea

Object changes behavior based on current state.

Real Scenario

Order states:

  • Pending
  • Paid
  • Shipped

Example


OrderState → PendingState / PaidState

Use Cases

  • Workflow engines
  • Payment lifecycle
  • Authentication states

6. Chain of Responsibility

Idea

Pass request through multiple handlers.

Real Scenario

ASP.NET Core Middleware Pipeline.

Example


app.UseAuthentication();
app.UseAuthorization();

Use Cases

  • Middleware
  • Validation pipelines
  • Request processing

7. Template Method

Idea

Define algorithm structure in base class.

Real Scenario

Report generation:

  • PDF report
  • Excel report

Use Cases

  • Data processing
  • Export systems
  • Batch jobs

8. Iterator Pattern

Idea

Sequentially access collection elements.

Real Scenario

Pagination in APIs.

Use Cases

  • Lists
  • Data traversal
  • Custom collections

9. Memento Pattern

Idea

Save and restore object state.

Real Scenario

Undo functionality in editor.

Use Cases

  • Undo/redo
  • Draft systems
  • Checkpoints

10. Visitor Pattern

Idea

Add new operations without changing object structure.

Real Scenario

Export reports:

  • PDF export
  • Excel export

Use Cases

  • Reporting systems
  • Analytics engines
  • Data transformation

11. Interpreter Pattern

Idea

Interpret grammar/rules/language.

Real Scenario


1 + 2 - 3

Use Cases

  • Rule engines
  • Query parsers
  • Formula evaluation

Advantages of Behavioral Patterns

  • ✔ Better communication
  • ✔ Flexible behavior
  • ✔ Reduced coupling
  • ✔ Easier maintenance
  • ✔ Better scalability

Disadvantages

  • ✖ More abstraction/classes
  • ✖ Increased complexity
  • ✖ Harder debugging
  • ✖ Learning curve

When to Use Behavioral Patterns

Use when:

  • Multiple objects interact frequently
  • Behavior changes dynamically
  • Event-driven systems are needed
  • Complex workflows exist
  • Communication logic becomes difficult

Real Project Mapping (.NET + Angular)

Feature Pattern
Tax calculation Strategy
Notifications Observer
Button actions Command
Chat system Mediator
Order workflow State
Middleware Chain of Responsibility
Report generation Template Method
Pagination Iterator
Undo feature Memento
Export feature Visitor
Rule engine Interpreter

ASP.NET Core Real Examples

ASP.NET Core Feature Pattern
Middleware pipeline Chain of Responsibility
Authentication states State
Logging providers Strategy
SignalR events Observer

Summary

Behavioral patterns help:

  • Manage object communication
  • Build flexible workflows
  • Reduce dependency between objects
  • Improve scalability and maintainability

Perfect for enterprise-level:

  • APIs
  • Microservices
  • Event-driven systems
  • Workflow engines
  • Real-time applications

Comments

Popular posts from this blog

Promises in Angular

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Csharp Coding - Session