🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Definition
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.
2. Problem Statement
Suppose you're designing a navigation app that provides different modes of transportation: driving, walking, or taking public transit. If you try to implement all algorithms (routes) within the app, it will become cluttered and challenging to manage.
3. Solution
The Strategy Pattern suggests that you take a class that does something specific in a lot of different ways and extract all these algorithms into separate classes called strategies. The original class, called context, must maintain a reference to a strategy object and delegate the work to this strategy, instead of implementing multiple versions of the algorithm.
4. Real-World Use Cases
1. Navigation apps providing different routes based on travel mode: driving, biking, walking.
2. Image compression applications where users can choose different compression algorithms.
3. Payment gateways in e-commerce applications allow multiple payment strategies.
5. Implementation Steps
1. Define a strategy interface common to all supported algorithms.
2. Implement concrete strategies for the various algorithms.
3. Define a context class to maintain a reference to a strategy object and switch between strategies.
6. Implementation in Kotlin Programming
// Step 1: Strategy Interface
interface RouteStrategy {
fun buildRoute(start: String, destination: String): String
}
// Step 2: Concrete Strategies
class DrivingStrategy : RouteStrategy {
override fun buildRoute(start: String, destination: String): String {
return "Driving route from $start to $destination"
}
}
class WalkingStrategy : RouteStrategy {
override fun buildRoute(start: String, destination: String): String {
return "Walking route from $start to $destination"
}
}
// Step 3: Context Class
class Navigator(private var strategy: RouteStrategy) {
fun setStrategy(newStrategy: RouteStrategy) {
strategy = newStrategy
}
fun buildRoute(start: String, destination: String): String {
return strategy.buildRoute(start, destination)
}
}
// Client Code
fun main() {
val navigator = Navigator(DrivingStrategy())
println(navigator.buildRoute("Point A", "Point B"))
navigator.setStrategy(WalkingStrategy())
println(navigator.buildRoute("Point A", "Point B"))
}
Output:
Driving route from Point A to Point B Walking route from Point A to Point B
Explanation:
1. RouteStrategy defines a contract that all strategies (algorithms) must follow.
2. Concrete strategies (DrivingStrategy, WalkingStrategy) encapsulate the specific algorithms.
3. Navigator, the context class, maintains a reference to a strategy object and can switch between strategies using setStrategy.
4. The client can use the Navigator class and easily switch between different routing strategies without changing the navigator's implementation.
7. When to use?
Use the Strategy pattern when:
1. Many related classes differ only in their behavior.
2. You need different variants of an algorithm.
3. The algorithm has clients that should be unaware of its data structures.
4. A class defines multiple behaviors, and these appear as multiple conditional statements. They can be moved to their strategy classes.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment