* 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
@ -24,19 +24,18 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Visitor pattern defines mechanism to apply operations on nodes in hierarchy. New operations can
|
||||
* be added without altering the node interface.
|
||||
* <p>
|
||||
* In this example there is a unit hierarchy beginning from {@link Commander}. This hierarchy is
|
||||
* <p>Visitor pattern defines mechanism to apply operations on nodes in hierarchy. New operations
|
||||
* can be added without altering the node interface.</p>
|
||||
*
|
||||
* <p>In this example there is a unit hierarchy beginning from {@link Commander}. This hierarchy is
|
||||
* traversed by visitors. {@link SoldierVisitor} applies its operation on {@link Soldier}s,
|
||||
* {@link SergeantVisitor} on {@link Sergeant}s and so on.
|
||||
* {@link SergeantVisitor} on {@link Sergeant}s and so on.</p>
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Commander
|
||||
*
|
||||
* Commander.
|
||||
*/
|
||||
public class Commander extends Unit {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* CommanderVisitor
|
||||
*
|
||||
* CommanderVisitor.
|
||||
*/
|
||||
public class CommanderVisitor implements UnitVisitor {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Sergeant
|
||||
*
|
||||
* Sergeant.
|
||||
*/
|
||||
public class Sergeant extends Unit {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* SergeantVisitor
|
||||
*
|
||||
* SergeantVisitor.
|
||||
*/
|
||||
public class SergeantVisitor implements UnitVisitor {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Soldier
|
||||
*
|
||||
* Soldier.
|
||||
*/
|
||||
public class Soldier extends Unit {
|
||||
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* SoldierVisitor
|
||||
*
|
||||
* SoldierVisitor.
|
||||
*/
|
||||
public class SoldierVisitor implements UnitVisitor {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Interface for the nodes in hierarchy.
|
||||
*
|
||||
*/
|
||||
public abstract class Unit {
|
||||
|
||||
@ -37,7 +35,7 @@ public abstract class Unit {
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept visitor
|
||||
* Accept visitor.
|
||||
*/
|
||||
public void accept(UnitVisitor visitor) {
|
||||
for (var child : children) {
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
/**
|
||||
*
|
||||
* Visitor interface.
|
||||
*
|
||||
*/
|
||||
public interface UnitVisitor {
|
||||
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.visitor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
* Application test.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
|
@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 19:45 PM
|
||||
* Date: 12/30/15 - 19:45 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class CommanderTest extends UnitTest<Commander> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given {@link Commander}
|
||||
* Create a new test instance for the given {@link Commander}.
|
||||
*/
|
||||
public CommanderTest() {
|
||||
super(Commander::new);
|
||||
|
@ -26,14 +26,14 @@ package com.iluwatar.visitor;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:43 PM
|
||||
* Date: 12/30/15 - 18:43 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class CommanderVisitorTest extends VisitorTest<CommanderVisitor> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given visitor
|
||||
* Create a new test instance for the given visitor.
|
||||
*/
|
||||
public CommanderVisitorTest() {
|
||||
super(
|
||||
|
@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 19:45 PM
|
||||
* Date: 12/30/15 - 19:45 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SergeantTest extends UnitTest<Sergeant> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given {@link Sergeant}
|
||||
* Create a new test instance for the given {@link Sergeant}.
|
||||
*/
|
||||
public SergeantTest() {
|
||||
super(Sergeant::new);
|
||||
|
@ -26,14 +26,14 @@ package com.iluwatar.visitor;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:36 PM
|
||||
* Date: 12/30/15 - 18:36 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SergeantVisitorTest extends VisitorTest<SergeantVisitor> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given visitor
|
||||
* Create a new test instance for the given visitor.
|
||||
*/
|
||||
public SergeantVisitorTest() {
|
||||
super(
|
||||
|
@ -27,14 +27,14 @@ import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 19:45 PM
|
||||
* Date: 12/30/15 - 19:45 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SoldierTest extends UnitTest<Soldier> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given {@link Soldier}
|
||||
* Create a new test instance for the given {@link Soldier}.
|
||||
*/
|
||||
public SoldierTest() {
|
||||
super(Soldier::new);
|
||||
|
@ -26,14 +26,14 @@ package com.iluwatar.visitor;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:59 PM
|
||||
* Date: 12/30/15 - 18:59 PM.
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SoldierVisitorTest extends VisitorTest<SoldierVisitor> {
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given visitor
|
||||
* Create a new test instance for the given visitor.
|
||||
*/
|
||||
public SoldierVisitorTest() {
|
||||
super(
|
||||
|
@ -23,18 +23,18 @@
|
||||
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:59 PM
|
||||
* Date: 12/30/15 - 18:59 PM.
|
||||
* Test related to Units
|
||||
* @param <U> Type of Unit
|
||||
* @author Jeroen Meulemeester
|
||||
@ -42,12 +42,12 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
public abstract class UnitTest<U extends Unit> {
|
||||
|
||||
/**
|
||||
* Factory to create new instances of the tested unit
|
||||
* Factory to create new instances of the tested unit.
|
||||
*/
|
||||
private final Function<Unit[], U> factory;
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given unit type {@link U}
|
||||
* Create a new test instance for the given unit type {@link U}.
|
||||
*
|
||||
* @param factory Factory to create new instances of the tested unit
|
||||
*/
|
||||
@ -74,7 +74,7 @@ public abstract class UnitTest<U extends Unit> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the correct visit method is called on the mock, depending on the tested instance
|
||||
* Verify if the correct visit method is called on the mock, depending on the tested instance.
|
||||
*
|
||||
* @param unit The tested unit instance
|
||||
* @param mockedVisitor The mocked {@link UnitVisitor} who should have gotten a visit by the unit
|
||||
|
@ -23,6 +23,8 @@
|
||||
|
||||
package com.iluwatar.visitor;
|
||||
|
||||
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;
|
||||
@ -35,10 +37,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Date: 12/30/15 - 18:59 PM
|
||||
* Date: 12/30/15 - 18:59 PM.
|
||||
* Test case for Visitor Pattern
|
||||
* @param <V> Type of UnitVisitor
|
||||
* @author Jeroen Meulemeester
|
||||
@ -58,34 +58,36 @@ public abstract class VisitorTest<V extends UnitVisitor> {
|
||||
}
|
||||
|
||||
/**
|
||||
* The tested visitor instance
|
||||
* The tested visitor instance.
|
||||
*/
|
||||
private final V visitor;
|
||||
|
||||
/**
|
||||
* The optional expected response when being visited by a commander
|
||||
* The optional expected response when being visited by a commander.
|
||||
*/
|
||||
private final Optional<String> commanderResponse;
|
||||
|
||||
/**
|
||||
* The optional expected response when being visited by a sergeant
|
||||
* The optional expected response when being visited by a sergeant.
|
||||
*/
|
||||
private final Optional<String> sergeantResponse;
|
||||
|
||||
/**
|
||||
* The optional expected response when being visited by a soldier
|
||||
* The optional expected response when being visited by a soldier.
|
||||
*/
|
||||
private final Optional<String> soldierResponse;
|
||||
|
||||
/**
|
||||
* Create a new test instance for the given visitor
|
||||
* Create a new test instance for the given visitor.
|
||||
*
|
||||
* @param commanderResponse The optional expected response when being visited by a commander
|
||||
* @param sergeantResponse The optional expected response when being visited by a sergeant
|
||||
* @param soldierResponse The optional expected response when being visited by a soldier
|
||||
*/
|
||||
public VisitorTest(final V visitor, final Optional<String> commanderResponse,
|
||||
final Optional<String> sergeantResponse, final Optional<String> soldierResponse) {
|
||||
public VisitorTest(final V visitor,
|
||||
final Optional<String> commanderResponse,
|
||||
final Optional<String> sergeantResponse,
|
||||
final Optional<String> soldierResponse) {
|
||||
|
||||
this.visitor = visitor;
|
||||
this.commanderResponse = commanderResponse;
|
||||
|
Reference in New Issue
Block a user