Factory Method パターン
Step 1
MyMain
public class MyMain {
public static void main(String args[]) {
MyClassA classA = new MyClassA();
System.out.println(classA.getName());
System.out.println(classA.getAge());
MyClassB classB = new MyClassB();
System.out.println(classB.getAddress());
System.out.println(classB.getFloor());
}
}
MyClassA
public class MyClassA {
public String getName() {
return "Suzuki";
}
public int getAge() {
return 20;
}
}
MyClassB
public class MyClassB {
public String getAddress() {
return "Tokyo";
}
public int getFloor() {
return 2;
}
}
実行結果
L:\>java MyMain
Suzuki
20
Tokyo
2
Step 2
MyMain
public class MyMain {
public static void main(String args[]) {
MyClassA classA = new MyClassA1();
System.out.println(classA.getName());
System.out.println(classA.getAge());
MyClassB classB = new MyClassB1();
System.out.println(classB.getAddress());
System.out.println(classB.getFloor());
classA = new MyClassA2();
System.out.println(classA.getName());
System.out.println(classA.getAge());
classB = new MyClassB2();
System.out.println(classB.getAddress());
System.out.println(classB.getFloor());
}
}
MyClassA
public abstract class MyClassA {
public abstract String getName();
public abstract int getAge();
}
MyClassA1
public class MyClassA1 extends MyClassA {
public String getName() {
return "Suzuki";
}
public int getAge() {
return 20;
}
}
MyClassA2
public class MyClassA2 extends MyClassA {
public String getName() {
return "Tanaka";
}
public int getAge() {
return 30;
}
}
MyClassB
public abstract class MyClassB {
public abstract String getAddress();
public abstract int getFloor();
}
MyClassB1
public class MyClassB1 extends MyClassB {
public String getAddress() {
return "Tokyo";
}
public int getFloor() {
return 2;
}
}
MyClassB2
public class MyClassB2 extends MyClassB {
public String getAddress() {
return "Yokohama";
}
public int getFloor() {
return 3;
}
}
実行結果
L:\>java MyMain
Suzuki
20
Tokyo
2
Tanaka
30
Yokohama
3
Step 3 「Factory Method」
MyMain
public class MyMain {
public static void main(String args[]) {
MyClassA classA = new MyClassA1();
System.out.println(classA.getName());
System.out.println(classA.getAge());
MyClassB classB = classA.createMyClassB();
System.out.println(classB.getAddress());
System.out.println(classB.getFloor());
classA = new MyClassA2();
System.out.println(classA.getName());
System.out.println(classA.getAge());
classB = classA.createMyClassB();
System.out.println(classB.getAddress());
System.out.println(classB.getFloor());
}
}
MyClassA
public abstract class MyClassA {
public abstract String getName();
public abstract int getAge();
public abstract MyClassB createMyClassB();
}
MyClassA1
public class MyClassA1 extends MyClassA {
public String getName() {
return "Suzuki";
}
public int getAge() {
return 20;
}
public MyClassB createMyClassB() {
return new MyClassB1();
}
}
MyClassA2
public class MyClassA2 extends MyClassA {
public String getName() {
return "Tanaka";
}
public int getAge() {
return 30;
}
public MyClassB createMyClassB() {
return new MyClassB2();
}
}
MyClassB
public abstract class MyClassB {
public abstract String getAddress();
public abstract int getFloor();
}
MyClassB1
public class MyClassB1 extends MyClassB {
public String getAddress() {
return "Tokyo";
}
public int getFloor() {
return 2;
}
}
MyClassB2
public class MyClassB2 extends MyClassB {
public String getAddress() {
return "Yokohama";
}
public int getFloor() {
return 3;
}
}
実行結果
L:\>java MyMain
Suzuki
20
Tokyo
2
Tanaka
30
Yokohama
3