Changed package naming across all examples.

This commit is contained in:
Ilkka Seppala
2015-05-31 11:55:18 +03:00
parent 703ebd3e20
commit 8524c75ba6
437 changed files with 1095 additions and 1402 deletions

View File

@ -0,0 +1,20 @@
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();
}
}

View File

@ -0,0 +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;
}
}

View File

@ -0,0 +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!");
}
}

View File

@ -0,0 +1,22 @@
package com.iluwatar.templatemethod;
/**
*
* StealingMethod defines skeleton for the algorithm.
*
*/
public abstract class StealingMethod {
protected abstract String pickTarget();
protected abstract void confuseTarget(String target);
protected abstract void stealTheItem(String target);
public void steal() {
String target = pickTarget();
System.out.println("The target has been chosen as " + target + ".");
confuseTarget(target);
stealTheItem(target);
}
}

View File

@ -0,0 +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.");
}
}