Tutorials Design Patterns in C#
Strategy Pattern — Complete Guide
Strategy Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.
On this page
Design Patterns in C# · Lesson 21 of 69
Strategy Pattern
GoF Core ✓ → Enterprise → Cloud & Craft
Enterprise · 2 — App patterns · ~6 min · Module 3: Behavioral Design Patterns
What is this?
Strategy defines a family of interchangeable algorithms behind one interface — pick at runtime.
Why should you care?
ShopNest shipping cost (Standard, Express, StorePickup) should swap without editing checkout.
See it live — copy this example
Paste into a C# console or class library project and run dotnet run.
public interface IShippingStrategy { decimal Quote(decimal weightKg); }
public sealed class StandardShipping : IShippingStrategy
{
public decimal Quote(decimal w) => 40 + w * 8;
}
public sealed class ExpressShipping : IShippingStrategy
{
public decimal Quote(decimal w) => 80 + w * 15;
}
public sealed class Checkout
{
private readonly IShippingStrategy _shipping;
public Checkout(IShippingStrategy shipping) => _shipping = shipping;
public decimal Total(decimal items, decimal weight) => items + _shipping.Quote(weight);
}
Console.WriteLine(new Checkout(new ExpressShipping()).Total(1000, 2));
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Checkout depends on IShippingStrategy.
- Inject Express or Standard.
- Same structure as many “calculator” DI registrations.
Practice next
- Quote Standard vs Express.
- Add StorePickup returning 0.
- Select strategy from user choice.
- Pass destination city into Quote.
- Register strategies in a dictionary by code.
Remember
Interchangeable algorithms. Inject the strategy. Open for new options.
ShopNest shipping strategies
Buyer picks Express at checkout.
Outcome: Totals use ExpressShipping without if-else soup.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!