diff --git a/factory-method/README.md b/factory-method/README.md index 4334b052353d..d45a6aa0cac7 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -31,6 +31,9 @@ Real-world example In plain words > It provides a way to delegate the instantiation logic to child classes. +> +> Imagine a messaging app that can send notifications by Email, SMS, or Push Notifications. +> The user only asks to send a message. The Factory Method chooses and creates the correct notification service Wikipedia says diff --git a/singleton/README.md b/singleton/README.md index 92505061399e..48cbc211aa24 100644 --- a/singleton/README.md +++ b/singleton/README.md @@ -37,6 +37,17 @@ Sequence diagram ![Singleton Pattern sequence diagram](./etc/singleton-sequence-diagram.png) +## Singleton Implementation Comparison (for eager and lazy initialization) + +| Feature | Eager Initialization | Thread-Safe Lazy Initialization | +|----------|----------|----------| +| Object Creation | During class loading | First call to `getInstance()` | +| Thread Safe | Yes | Yes | +| Synchronization Required | No | Yes | +| Memory Usage | Higher | Lower | +| Performance | Faster access | Slight synchronization overhead | +| Complexity | Simple | Moderate | + ## Programmatic Example of Singleton Pattern in Java Joshua Bloch, Effective Java 2nd Edition p.18 diff --git a/strategy/README.md b/strategy/README.md index 9f9148ccd842..d3238876d382 100644 --- a/strategy/README.md +++ b/strategy/README.md @@ -24,11 +24,11 @@ Define a family of algorithms in Java, encapsulate each one, and make them inter Real-world example -> A practical real-world example of the Strategy design pattern in Java is evident in car navigation systems, where algorithm flexibility is paramount. Different navigation algorithms (such as shortest route, fastest route, and scenic route) can be used to determine the best path from one location to another. Each algorithm encapsulates a specific strategy for calculating the route. The user (client) can switch between these algorithms based on their preferences without changing the navigation system itself. This allows for flexible and interchangeable navigation strategies within the same system. +> A practical real-world example of the Strategy design pattern in Java is evident in car navigation systems, where algorithm flexibility is paramount. Different navigation algorithms (such as shortest-route, fastest-route, and scenic-route) can be used to determine the best path from one location to another. Each algorithm encapsulates a specific strategy for calculating the route. The user (client) can switch between these algorithms based on their preferences without changing the navigation system itself. This allows for flexible and interchangeable navigation strategies within the same system. In plain words -> Strategy pattern allows choosing the best-suited algorithm at runtime. +> The strategy pattern allows the selection of the best-suited algorithm at runtime. Wikipedia says @@ -42,7 +42,7 @@ Flowchart Slaying dragons is a dangerous job. With experience, it becomes easier. Veteran dragonslayers have developed different fighting strategies against different types of dragons. -Let's explore how to implement the `DragonSlayingStrategy` interface in Java, demonstrating various Strategy pattern applications. +This example demonstrates how to implement the `DragonSlayingStrategy` interface in Java and illustrates several applications of the Strategy pattern. ```java @FunctionalInterface @@ -188,6 +188,26 @@ Use the Strategy pattern when: * An algorithm uses data that clients shouldn't know about. * A class defines many behaviors and these appear as multiple conditional statements in its operations. +## When not to use the Strategy Pattern in Java + +Avoid using the Strategy pattern when: + +* There is only one algorithm and no foreseeable need for alternative implementations. +* The behavior is unlikely to change, making additional strategy classes unnecessary. +* Runtime selection or switching of algorithms is not required. +* The added abstraction would increase complexity without providing meaningful flexibility. + +## Real-World Applications + +The Strategy Pattern is commonly used in: + +* Payment gateways that support multiple payment methods such as Credit Card, UPI, and PayPal. +* Route planning systems that offer shortest, fastest, or scenic route calculations. +* Sorting utilities that allow different sorting algorithms to be selected at runtime. +* Authentication systems that support multiple login mechanisms such as OAuth, SAML, or username/password authentication. +* E-commerce platforms that apply different discount calculation strategies based on customer type or promotional campaigns. +* Recommendation engines that use different recommendation algorithms depending on user preferences or business requirements. + ## Strategy Pattern Java Tutorials * [Strategy Pattern Tutorial (DigitalOcean)](https://www.digitalocean.com/community/tutorials/strategy-design-pattern-in-java-example-tutorial)