Index
TemplateMethodパターン@Java言語で学ぶデザインパターン入門
書き換え前
1 2 3 4 5 6 7 8 9 10 11 |
public abstract class AbstractCalculater { public abstract void start(); public abstract void calculate(); public abstract void end(); public final void show() { start(); calculate(); end(); } } |
書き換え後
1 2 3 4 5 6 7 8 9 10 11 |
public interface AbstractCalculater { public abstract void start(); public abstract void calculate(); public abstract void end(); public default void show() { start(); calculate(); end(); } } |
書き換え前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class IntCalculater implements AbstractCalculater { private int m_num; public IntCalculater(int num) { this.m_num = num; } @Override public void start() { System.out.println("start"); } @Override public void calculate() { System.out.println(m_num * m_num); } @Override public void end() { System.out.println("end"); } } |
書き換え前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class DoubleCalculater extends AbstractCalculater { private double m_num; DoubleCalculater(double num) { this.m_num = num; } @Override public void start() { System.out.println("+++start+++"); } @Override public void calculate() { System.out.println(m_num * m_num * m_num); } @Override public void end() { System.out.println("++++end++++"); } } |
書き換え後
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class DoubleCalculater implements AbstractCalculater { private double m_num; DoubleCalculater(double num) { this.m_num = num; } @Override public void start() { System.out.println("+++start+++"); } @Override public void calculate() { System.out.println(m_num * m_num * m_num); } @Override public void end() { System.out.println("++++end++++"); } } |
特に書き換えは必要ありません。
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { AbstractCalculater intCal = new IntCalculater(10); AbstractCalculater doubleCal = new DoubleCalculater(10.1); intCal.show(); doubleCal.show(); } } |