Skip to content

Commit fde7f71

Browse files
committed
iluwatar#107 Template Method JavaDoc
1 parent 934f99e commit fde7f71

File tree

5 files changed

+118
-109
lines changed

5 files changed

+118
-109
lines changed
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
package com.iluwatar.templatemethod;
2-
3-
/**
4-
*
5-
* Template Method defines a skeleton for an algorithm. The algorithm subclasses
6-
* provide implementation for the blank parts.
7-
*
8-
* In this example HalflingThief contains StealingMethod that can be changed.
9-
* First the thief hits with HitAndRunMethod and then with SubtleMethod.
10-
*
11-
*/
12-
public class App {
13-
14-
public static void main(String[] args) {
15-
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
16-
thief.steal();
17-
thief.changeMethod(new SubtleMethod());
18-
thief.steal();
19-
}
20-
}
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
*
5+
* Template Method defines a skeleton for an algorithm. The algorithm subclasses
6+
* provide implementation for the blank parts.
7+
* <p>
8+
* In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed.
9+
* First the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
10+
*
11+
*/
12+
public class App {
13+
14+
/**
15+
* Program entry point
16+
* @param args command line args
17+
*/
18+
public static void main(String[] args) {
19+
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
20+
thief.steal();
21+
thief.changeMethod(new SubtleMethod());
22+
thief.steal();
23+
}
24+
}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
package com.iluwatar.templatemethod;
2-
3-
/**
4-
*
5-
* Halfling thief uses StealingMethod to steal.
6-
*
7-
*/
8-
public class HalflingThief {
9-
10-
private StealingMethod method;
11-
12-
public HalflingThief(StealingMethod method) {
13-
this.method = method;
14-
}
15-
16-
public void steal() {
17-
method.steal();
18-
}
19-
20-
public void changeMethod(StealingMethod method) {
21-
this.method = method;
22-
}
23-
}
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
*
5+
* Halfling thief uses {@link StealingMethod} to steal.
6+
*
7+
*/
8+
public class HalflingThief {
9+
10+
private StealingMethod method;
11+
12+
public HalflingThief(StealingMethod method) {
13+
this.method = method;
14+
}
15+
16+
public void steal() {
17+
method.steal();
18+
}
19+
20+
public void changeMethod(StealingMethod method) {
21+
this.method = method;
22+
}
23+
}
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package com.iluwatar.templatemethod;
2-
3-
/**
4-
*
5-
* HitAndRunMethod implementation of StealingMethod.
6-
*
7-
*/
8-
public class HitAndRunMethod extends StealingMethod {
9-
10-
@Override
11-
protected String pickTarget() {
12-
return "old goblin woman";
13-
}
14-
15-
@Override
16-
protected void confuseTarget(String target) {
17-
System.out.println("Approach the " + target + " from behind.");
18-
}
19-
20-
@Override
21-
protected void stealTheItem(String target) {
22-
System.out.println("Grab the handbag and run away fast!");
23-
}
24-
25-
}
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
*
5+
* HitAndRunMethod implementation of {@link StealingMethod}.
6+
*
7+
*/
8+
public class HitAndRunMethod extends StealingMethod {
9+
10+
@Override
11+
protected String pickTarget() {
12+
return "old goblin woman";
13+
}
14+
15+
@Override
16+
protected void confuseTarget(String target) {
17+
System.out.println("Approach the " + target + " from behind.");
18+
}
19+
20+
@Override
21+
protected void stealTheItem(String target) {
22+
System.out.println("Grab the handbag and run away fast!");
23+
}
24+
25+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
package com.iluwatar.templatemethod;
2-
3-
/**
4-
*
5-
* SubtleMethod implementation of StealingMethod.
6-
*
7-
*/
8-
public class SubtleMethod extends StealingMethod {
9-
10-
@Override
11-
protected String pickTarget() {
12-
return "shop keeper";
13-
}
14-
15-
@Override
16-
protected void confuseTarget(String target) {
17-
System.out.println("Approach the " + target
18-
+ " with tears running and hug him!");
19-
}
20-
21-
@Override
22-
protected void stealTheItem(String target) {
23-
System.out.println("While in close contact grab the " + target
24-
+ "'s wallet.");
25-
}
26-
27-
}
1+
package com.iluwatar.templatemethod;
2+
3+
/**
4+
*
5+
* SubtleMethod implementation of {@link StealingMethod}.
6+
*
7+
*/
8+
public class SubtleMethod extends StealingMethod {
9+
10+
@Override
11+
protected String pickTarget() {
12+
return "shop keeper";
13+
}
14+
15+
@Override
16+
protected void confuseTarget(String target) {
17+
System.out.println("Approach the " + target
18+
+ " with tears running and hug him!");
19+
}
20+
21+
@Override
22+
protected void stealTheItem(String target) {
23+
System.out.println("While in close contact grab the " + target
24+
+ "'s wallet.");
25+
}
26+
27+
}
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
package com.iluwatar.templatemethod;
2-
3-
import org.junit.Test;
4-
5-
import com.iluwatar.templatemethod.App;
6-
7-
public class AppTest {
8-
9-
@Test
10-
public void test() {
11-
String[] args = {};
12-
App.main(args);
13-
}
14-
}
1+
package com.iluwatar.templatemethod;
2+
3+
import org.junit.Test;
4+
5+
import com.iluwatar.templatemethod.App;
6+
7+
/**
8+
*
9+
* Application test
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

0 commit comments

Comments
 (0)