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