* fix checkstlye errors - visitor pattern * fix checkstlye errors - strategy pattern * fix checkstlye errors - singleton pattern
This commit is contained in:
committed by
Ilkka Seppälä
parent
1eb1961f1b
commit
3b1a28149b
@ -28,15 +28,15 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Strategy pattern (also known as the policy pattern) is a software design pattern that enables
|
||||
* an algorithm's behavior to be selected at runtime.
|
||||
* <p>
|
||||
* Before Java 8 the Strategies needed to be separate classes forcing the developer
|
||||
* <p>The Strategy pattern (also known as the policy pattern) is a software design pattern that
|
||||
* 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
|
||||
* 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>
|
||||
* In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing object
|
||||
* ({@link DragonSlayer}) can alter its behavior by changing its strategy.
|
||||
* with method references and lambdas making the code shorter and more readable.</p>
|
||||
*
|
||||
* <p>In this example ({@link DragonSlayingStrategy}) encapsulates an algorithm. The containing
|
||||
* object ({@link DragonSlayer}) can alter its behavior by changing its strategy.</p>
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
@ -44,7 +44,7 @@ public class App {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.strategy;
|
||||
|
||||
/**
|
||||
*
|
||||
* DragonSlayer uses different strategies to slay the dragon.
|
||||
*
|
||||
*/
|
||||
public class DragonSlayer {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.strategy;
|
||||
|
||||
/**
|
||||
*
|
||||
* Strategy interface.
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DragonSlayingStrategy {
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Melee strategy.
|
||||
*
|
||||
*/
|
||||
public class MeleeStrategy implements DragonSlayingStrategy {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Projectile strategy.
|
||||
*
|
||||
*/
|
||||
public class ProjectileStrategy implements DragonSlayingStrategy {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Spell strategy.
|
||||
*
|
||||
*/
|
||||
public class SpellStrategy implements DragonSlayingStrategy {
|
||||
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.strategy;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
* Application test.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
|
@ -23,21 +23,21 @@
|
||||
|
||||
package com.iluwatar.strategy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/29/15 - 10:50 PM
|
||||
* Date: 12/29/15 - 10:50 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class DragonSlayerTest {
|
||||
|
||||
/**
|
||||
* Verify if the dragon slayer uses the strategy during battle
|
||||
* Verify if the dragon slayer uses the strategy during battle.
|
||||
*/
|
||||
@Test
|
||||
public void testGoToBattle() {
|
||||
@ -50,7 +50,7 @@ public class DragonSlayerTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the dragon slayer uses the new strategy during battle after a change of strategy
|
||||
* Verify if the dragon slayer uses the new strategy during battle after a change of strategy.
|
||||
*/
|
||||
@Test
|
||||
public void testChangeStrategy() {
|
||||
|
@ -23,45 +23,48 @@
|
||||
|
||||
package com.iluwatar.strategy;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Date: 12/29/15 - 10:58 PM
|
||||
* Date: 12/29/15 - 10:58 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class DragonSlayingStrategyTest {
|
||||
|
||||
/**
|
||||
* Assembles test parameters.
|
||||
*
|
||||
* @return The test parameters for each cycle
|
||||
*/
|
||||
static Collection<Object[]> dataProvider() {
|
||||
return List.of(
|
||||
new Object[]{
|
||||
new MeleeStrategy(),
|
||||
"With your Excalibur you sever the dragon's head!"
|
||||
},
|
||||
new Object[]{
|
||||
new ProjectileStrategy(),
|
||||
"You shoot the dragon with the magical crossbow and it falls dead on the ground!"
|
||||
},
|
||||
new Object[]{
|
||||
new SpellStrategy(),
|
||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"
|
||||
}
|
||||
return List.of(
|
||||
new Object[]{
|
||||
new MeleeStrategy(),
|
||||
"With your Excalibur you sever the dragon's head!"
|
||||
},
|
||||
new Object[]{
|
||||
new ProjectileStrategy(),
|
||||
"You shoot the dragon with the magical crossbow and it falls dead on the ground!"
|
||||
},
|
||||
new Object[]{
|
||||
new SpellStrategy(),
|
||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@ -79,7 +82,7 @@ public class DragonSlayingStrategyTest {
|
||||
|
||||
|
||||
/**
|
||||
* Test if executing the strategy gives the correct response
|
||||
* Test if executing the strategy gives the correct response.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("dataProvider")
|
||||
|
Reference in New Issue
Block a user