The Strategy Design Pattern: Why We Need It

In software development, creating maintainable and scalable systems is crucial. The Strategy Design Pattern helps address challenges caused by duplication and tightly coupled behavior in object-oriented designs. Let’s explore this pattern with a common example

Problem Statement

Imagine you’re building a Notification System with a Notification class that has two methods:

  • Format: Handles message formatting.
  • Send: Sends the formatted message to a recipient.

This class is inherited by SMSNotification, PushNotification, and EmailNotification. Each subclass can override the Format method if needed.


Example Without the Strategy Pattern


UML Diagram

Issues with This Approach

  • Code Duplication: SMSNotification and EmailNotification have the same formatting logic, but it’s duplicated in both classes.
  • Tight Coupling: The formatting logic is tied to the individual notification classes. Any change to the SMS/Email formatting must be updated in both classes.
  • Low Maintainability: Adding a new format (e.g., for Slack notifications) means adding more subclasses or overriding methods, increasing complexity.



How "Has-a" Relationship Solves It

By introducing the Strategy Pattern, we can decouple the formatting logic from the Notification class and move it into separate strategy classes. The Notification class will now have a FormatStrategy that handles the formatting dynamically.

Example Without the Strategy Pattern


UML Diagram

Why This is Better

  • No Duplication: The shared formatting logic for SMS and Email is now in one place (SmsEmailFormatStrategy).
  • Easier to Maintain: If the formatting logic changes, you only need to update the strategy class, and both SMS and Email will use the updated logic automatically.
  • More Flexible: Adding a new notification type, like Slack, is simple—just create a new formatting strategy without touching the existing code.


Learnings

The Strategy Pattern introduces composition over inheritance. Instead of hardcoding behaviors in a base class, we encapsulate them into separate strategy classes and allow objects to "have" a behavior dynamically. Here’s what I learned from implementing the Strategy Pattern:

  • Duplication leads to problems: It makes code harder to maintain and scale.
  • Composition over inheritance: By using the "has-a" relationship, you can make your code more modular and flexible.
  • The Strategy Pattern promotes reusability: Shared behaviors can be encapsulated and reused across multiple contexts.


Saurabh Hirve

UIS | Ex-Associate Consultant/Software Engineer at Capgemini

2 个月
回复
Arif K.

Technology Leader | Founder | Digital Transformation

2 个月

"composition over inheritance" I think is the key to building extensible software. Your explanation was very good. Keep these coming.

回复
Nancy Bhargava

Data Analyst | Python, SQL, Tableau | Transforming Data into Insights and Solutions

2 个月

Great explanation, Shekhar! The Strategy Design Pattern is indeed a game-changer for decoupling behavior and enhancing maintainability. Looking forward to more insights from you!

Pedram Soheil

Software Development Manager

2 个月

Very informative, thanks for sharing!

要查看或添加评论,请登录

Shekhar Singh的更多文章

社区洞察

其他会员也浏览了