Skip to content

Commit f139335

Browse files
committed
2 parents 63aa2a7 + 2ea6b9e commit f139335

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

src/main/java/com/gazbert/patterns/behavioural/template/AmericanGrowthFund.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ this software and associated documentation files (the "Software"), to deal in
2222
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
2323

2424
import java.math.BigDecimal;
25+
import java.util.Arrays;
26+
import java.util.List;
2527

2628
/**
29+
* The American Fund and its implementation of the template method hooks.
2730
*
2831
* @author gazbert
2932
*
3033
*/
3134
public class AmericanGrowthFund extends FundInfoCollectionTemplate
3235
{
33-
3436
@Override
3537
protected String getFundName()
3638
{
@@ -60,4 +62,10 @@ protected String getFundCurrency()
6062
return "USD";
6163
}
6264

65+
@Override
66+
protected List<String> getHoldings()
67+
{
68+
final String[] holdings = { "IBM", "Bank Of America", "Oracle", "Chevron"};
69+
return Arrays.asList(holdings);
70+
}
6371
}

src/main/java/com/gazbert/patterns/behavioural/template/FundInfoCollectionTemplate.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ this software and associated documentation files (the "Software"), to deal in
2323

2424
import java.math.BigDecimal;
2525
import java.text.DecimalFormat;
26+
import java.util.List;
2627

2728
/**
2829
* This is the main abstract template class.
@@ -40,6 +41,7 @@ public abstract class FundInfoCollectionTemplate
4041
protected abstract String getFundType();
4142
protected abstract String getFundGoal();
4243
protected abstract BigDecimal calculateManagementFee();
44+
protected abstract List<String> getHoldings();
4345

4446
// Here we have a hook with a default impl.
4547
protected String getFundCurrency()
@@ -60,8 +62,15 @@ public final void collectFundInformationForCatalogue()
6062
System.out.println("Fund Type: " + getFundType());
6163
System.out.println("Fund Goal: " + getFundGoal());
6264
System.out.println("Fund Currency: " + getFundCurrency());
63-
System.out.println("Fund Management Fee: " + new DecimalFormat("#.###").format(calculateManagementFee()) + "%");
65+
System.out.println("Fund Management Fee: " + new DecimalFormat("#.###").format(calculateManagementFee()) + "%");
6466

65-
System.out.println("END of main template method.\n");
67+
System.out.print("Fund Holdings: ");
68+
final List<String> fundHoldings = getHoldings();
69+
for (final String stock : fundHoldings)
70+
{
71+
System.out.print(stock + ", ");
72+
}
73+
74+
System.out.println("\nEND of main template method.\n");
6675
}
6776
}

src/main/java/com/gazbert/patterns/behavioural/template/JapanSpecialSituationsFund.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ this software and associated documentation files (the "Software"), to deal in
2222
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
2323

2424
import java.math.BigDecimal;
25+
import java.util.Arrays;
26+
import java.util.List;
2527

2628
/**
29+
*
30+
* The Japanese Special Sits Fund and its implementation of the template method hooks.
2731
*
2832
* @author gazbert
2933
*
3034
*/
3135
public class JapanSpecialSituationsFund extends FundInfoCollectionTemplate
3236
{
33-
3437
@Override
3538
protected String getFundName()
3639
{
@@ -59,4 +62,11 @@ protected String getFundCurrency()
5962
{
6063
return "JPY";
6164
}
65+
66+
@Override
67+
protected List<String> getHoldings()
68+
{
69+
final String[] holdings = { "Mitsuba", "Sinko", "Nissei", "Hazama"};
70+
return Arrays.asList(holdings);
71+
}
6272
}

src/main/java/com/gazbert/patterns/behavioural/template/UkAllShareTrackingFund.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ this software and associated documentation files (the "Software"), to deal in
2222
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
2323

2424
import java.math.BigDecimal;
25+
import java.util.Arrays;
26+
import java.util.List;
2527

2628
/**
29+
* The UK FTSE All Share Tracker Fund and its implementation of the template method hooks.
2730
*
2831
* @author gazbert
2932
*
3033
*/
3134
public class UkAllShareTrackingFund extends FundInfoCollectionTemplate
3235
{
33-
3436
@Override
3537
protected String getFundName()
3638
{
@@ -54,4 +56,11 @@ protected BigDecimal calculateManagementFee()
5456
{
5557
return new BigDecimal(0.01);
5658
}
59+
60+
@Override
61+
protected List<String> getHoldings()
62+
{
63+
final String[] holdings = { "BP", "HSBC", "John Lewis", "Virgin"};
64+
return Arrays.asList(holdings);
65+
}
5766
}

src/main/java/com/gazbert/patterns/behavioural/template/UkMidCapEquityFund.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ this software and associated documentation files (the "Software"), to deal in
2222
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
2323

2424
import java.math.BigDecimal;
25+
import java.util.Arrays;
26+
import java.util.List;
2527

2628
/**
29+
* The UK Mid Caps (FTSE 250) Fund and its implementation of the template method hooks.
2730
*
2831
* @author gazbert
2932
*
3033
*/
3134
public class UkMidCapEquityFund extends FundInfoCollectionTemplate
3235
{
33-
3436
@Override
3537
protected String getFundName()
3638
{
@@ -54,4 +56,11 @@ protected BigDecimal calculateManagementFee()
5456
{
5557
return new BigDecimal(0.014);
5658
}
59+
60+
@Override
61+
protected List<String> getHoldings()
62+
{
63+
final String[] holdings = { "Aston Martin", "Harrods", "Telegraph Media Group", "Ocado"};
64+
return Arrays.asList(holdings);
65+
}
5766
}

src/main/java/com/gazbert/patterns/behavioural/template/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* flow of the behaviour and call down to the subclasses to provide specific behaviour. The template
99
* pattern <em>must</em> be final in order to prevent subclasses from changing the overall behaviour.
1010
* <p>
11-
* Some of the hooks can have default behaviour - in case the subclasses choose not to override them - but
11+
* It makes use of the 'Hollywood Principle' - don't call us, we'll call you. Some of the hooks can have default behaviour - in case the subclasses choose not to override them - but
1212
* there should always be at least 1 abstract hook to in a Template pattern.
1313
* <p>
1414
* One final thing, the hook methods should have protected access - only subclasses should be able to use them. They are

0 commit comments

Comments
 (0)