#107 Template Method JavaDoc
This commit is contained in:
parent
934f99e2a9
commit
fde7f71704
@ -1,20 +1,24 @@
|
|||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Template Method defines a skeleton for an algorithm. The algorithm subclasses
|
* Template Method defines a skeleton for an algorithm. The algorithm subclasses
|
||||||
* provide implementation for the blank parts.
|
* provide implementation for the blank parts.
|
||||||
*
|
* <p>
|
||||||
* In this example HalflingThief contains StealingMethod that can be changed.
|
* In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed.
|
||||||
* First the thief hits with HitAndRunMethod and then with SubtleMethod.
|
* First the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
* Program entry point
|
||||||
thief.steal();
|
* @param args command line args
|
||||||
thief.changeMethod(new SubtleMethod());
|
*/
|
||||||
thief.steal();
|
public static void main(String[] args) {
|
||||||
}
|
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
||||||
}
|
thief.steal();
|
||||||
|
thief.changeMethod(new SubtleMethod());
|
||||||
|
thief.steal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Halfling thief uses StealingMethod to steal.
|
* Halfling thief uses {@link StealingMethod} to steal.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HalflingThief {
|
public class HalflingThief {
|
||||||
|
|
||||||
private StealingMethod method;
|
private StealingMethod method;
|
||||||
|
|
||||||
public HalflingThief(StealingMethod method) {
|
public HalflingThief(StealingMethod method) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void steal() {
|
public void steal() {
|
||||||
method.steal();
|
method.steal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeMethod(StealingMethod method) {
|
public void changeMethod(StealingMethod method) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* HitAndRunMethod implementation of StealingMethod.
|
* HitAndRunMethod implementation of {@link StealingMethod}.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HitAndRunMethod extends StealingMethod {
|
public class HitAndRunMethod extends StealingMethod {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String pickTarget() {
|
protected String pickTarget() {
|
||||||
return "old goblin woman";
|
return "old goblin woman";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void confuseTarget(String target) {
|
protected void confuseTarget(String target) {
|
||||||
System.out.println("Approach the " + target + " from behind.");
|
System.out.println("Approach the " + target + " from behind.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void stealTheItem(String target) {
|
protected void stealTheItem(String target) {
|
||||||
System.out.println("Grab the handbag and run away fast!");
|
System.out.println("Grab the handbag and run away fast!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* SubtleMethod implementation of StealingMethod.
|
* SubtleMethod implementation of {@link StealingMethod}.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SubtleMethod extends StealingMethod {
|
public class SubtleMethod extends StealingMethod {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String pickTarget() {
|
protected String pickTarget() {
|
||||||
return "shop keeper";
|
return "shop keeper";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void confuseTarget(String target) {
|
protected void confuseTarget(String target) {
|
||||||
System.out.println("Approach the " + target
|
System.out.println("Approach the " + target
|
||||||
+ " with tears running and hug him!");
|
+ " with tears running and hug him!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void stealTheItem(String target) {
|
protected void stealTheItem(String target) {
|
||||||
System.out.println("While in close contact grab the " + target
|
System.out.println("While in close contact grab the " + target
|
||||||
+ "'s wallet.");
|
+ "'s wallet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
package com.iluwatar.templatemethod;
|
package com.iluwatar.templatemethod;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.templatemethod.App;
|
import com.iluwatar.templatemethod.App;
|
||||||
|
|
||||||
public class AppTest {
|
/**
|
||||||
|
*
|
||||||
@Test
|
* Application test
|
||||||
public void test() {
|
*
|
||||||
String[] args = {};
|
*/
|
||||||
App.main(args);
|
public class AppTest {
|
||||||
}
|
|
||||||
}
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user