mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-09 16:24:34 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
+20
-18
@@ -1,28 +1,30 @@
|
||||
---
|
||||
title: Strategy
|
||||
title: "Strategy Pattern in Java: Streamlining Object Behaviors with Interchangeable Algorithms"
|
||||
shortTitle: Strategy
|
||||
description: "Explore the Strategy design pattern in Java with a detailed guide and practical examples. Learn how to implement flexible and interchangeable algorithms effectively in your Java applications for enhanced design and maintenance."
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Extensibility
|
||||
- Gang of Four
|
||||
- Interface
|
||||
- Polymorphism
|
||||
- Decoupling
|
||||
- Extensibility
|
||||
- Gang of Four
|
||||
- Interface
|
||||
- Polymorphism
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Policy
|
||||
|
||||
## Intent
|
||||
## Intent of Strategy Design Pattern
|
||||
|
||||
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently of the clients that use it.
|
||||
Define a family of algorithms in Java, encapsulate each one, and make them interchangeable to enhance software development using the Strategy design pattern. Strategy lets the algorithm vary independently of the clients that use it.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Strategy Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
> An analogous real-world example of the Strategy design pattern is the navigation systems in cars. 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
|
||||
|
||||
@@ -32,11 +34,11 @@ Wikipedia says
|
||||
|
||||
> In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Strategy Pattern in Java
|
||||
|
||||
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 first introduce the `DragonSlayingStrategy` interface and its implementations.
|
||||
Let's explore how to implement the `DragonSlayingStrategy` interface in Java, demonstrating various Strategy pattern applications.
|
||||
|
||||
```java
|
||||
@FunctionalInterface
|
||||
@@ -173,7 +175,7 @@ Program output:
|
||||
13:06:36.637 [main] INFO com.iluwatar.strategy.LambdaStrategy -- You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Strategy Pattern in Java
|
||||
|
||||
Use the Strategy pattern when:
|
||||
|
||||
@@ -182,16 +184,16 @@ 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.
|
||||
|
||||
## Tutorial
|
||||
## Strategy Pattern Java Tutorials
|
||||
|
||||
* [Strategy Pattern Tutorial (DigitalOcean)](https://www.digitalocean.com/community/tutorials/strategy-design-pattern-in-java-example-tutorial)
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Strategy Pattern in Java
|
||||
|
||||
* Java's `java.util.Comparator` interface is a common example of the Strategy pattern.
|
||||
* In GUI frameworks, layout managers (such as those in Java's AWT and Swing) are strategies.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Strategy Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -205,12 +207,12 @@ Trade-offs:
|
||||
* Clients must be aware of different Strategies.
|
||||
* Increase in the number of objects.
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Enhances an object without changing its interface but is more concerned with responsibilities than algorithms.
|
||||
* [State](https://java-design-patterns.com/patterns/state/): Similar in structure but used to represent state-dependent behavior rather than interchangeable algorithms.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)
|
||||
|
||||
Reference in New Issue
Block a user