Grammar fixes for strategy
This commit is contained in:
parent
b014dc2f00
commit
808df54dc0
@ -16,18 +16,18 @@ Policy
|
|||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets
|
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets
|
||||||
the algorithm vary independently from clients that use it.
|
the algorithm vary independently from the clients that use it.
|
||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
Real world example
|
Real-world example
|
||||||
|
|
||||||
> Slaying dragons is a dangerous job. With experience it becomes easier. Veteran
|
> Slaying dragons is a dangerous job. With experience, it becomes easier. Veteran
|
||||||
> dragonslayers have developed different fighting strategies against different types of dragons.
|
> dragonslayers have developed different fighting strategies against different types of dragons.
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
> Strategy pattern allows choosing the best suited algorithm at runtime.
|
> Strategy pattern allows choosing the best-suited algorithm at runtime.
|
||||||
|
|
||||||
Wikipedia says
|
Wikipedia says
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ Wikipedia says
|
|||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
Let's first introduce the dragon slaying strategy interface and its implementations.
|
Let's first introduce the dragon-slaying strategy interface and its implementations.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
@ -73,7 +73,7 @@ public class SpellStrategy implements DragonSlayingStrategy {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
And here is the mighty dragonslayer, who is able to pick his fighting strategy based on the
|
And here is the mighty dragonslayer, who can pick his fighting strategy based on the
|
||||||
opponent.
|
opponent.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@ -95,7 +95,7 @@ public class DragonSlayer {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Finally here's the dragonslayer in action.
|
Finally, here's the dragonslayer in action.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
LOGGER.info("Green dragon spotted ahead!");
|
LOGGER.info("Green dragon spotted ahead!");
|
||||||
@ -120,7 +120,7 @@ Program output:
|
|||||||
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
|
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
|
||||||
```
|
```
|
||||||
|
|
||||||
What's more, the new feature Lambda Expressions in Java 8 provides another approach for the implementation:
|
What's more, the lambda expressions in Java 8 provides another approach for the implementation:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class LambdaStrategy {
|
public class LambdaStrategy {
|
||||||
@ -163,7 +163,7 @@ And here's the dragonslayer in action.
|
|||||||
dragonSlayer.goToBattle();
|
dragonSlayer.goToBattle();
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output is the same as above one.
|
The program output is the same as the above one.
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||
@ -175,8 +175,8 @@ Use the Strategy pattern when
|
|||||||
|
|
||||||
* Many related classes differ only in their behavior. Strategies provide a way to configure a class either one of many behaviors
|
* Many related classes differ only in their behavior. Strategies provide a way to configure a class either one of many behaviors
|
||||||
* You need different variants of an algorithm. for example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms
|
* You need different variants of an algorithm. for example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms
|
||||||
* An algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures
|
* An algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex algorithm-specific data structures
|
||||||
* A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class
|
* A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move the related conditional branches into their own Strategy class
|
||||||
|
|
||||||
## Tutorial
|
## Tutorial
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
* enables an algorithm's behavior to be selected at runtime.</p>
|
* enables an algorithm's behavior to be selected at runtime.</p>
|
||||||
*
|
*
|
||||||
* <p>Before Java 8 the Strategies needed to be separate classes forcing the developer
|
* <p>Before Java 8 the Strategies needed to be separate classes forcing the developer
|
||||||
* to write lots of boilerplate code. With modern Java it is easy to pass behavior
|
* to write lots of boilerplate code. With modern Java, it is easy to pass behavior
|
||||||
* with method references and lambdas making the code shorter and more readable.</p>
|
* with method references and lambdas making the code shorter and more readable.</p>
|
||||||
*
|
*
|
||||||
* <p>In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing
|
* <p>In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing
|
||||||
|
Loading…
x
Reference in New Issue
Block a user