서로 관련있는 여러 객체를 만들어주는 인터페이스.
public interface Phone {
void complete();
}
public class ApplePhone implements Phone {
@Override
public void complete() {
System.out.println("apple phone complete");
}
}
public class SamsungPhone implements Phone {
@Override
public void complete() {
System.out.println("samsung phone complete");
}
}
public class SimplePhoneFactory {
public Phone orderPhone(String type) {
Phone phone = createPhone(type);
phone.complete();
return phone;
}
private Phone createPhone(String type) {
Phone phone;
switch (type){
case "APPLE":
phone = new ApplePhone();
break;
case "SAMSUNG":
phone = new SamsungPhone();
break;
default:
throw new IllegalArgumentException("Illegal type");
}
return phone;
}
}
public class Client {
public static void main(String[] args) {
SimplePhoneFactory simplePhoneFactory = new SimplePhoneFactory();
Phone apple = simplePhoneFactory.orderPhone("APPLE");
Phone samsung = simplePhoneFactory.orderPhone("SAMSUNG");
}
}
public interface Phone {
void complete();
}
public class ApplePhone implements Phone {
@Override
public void complete() {
System.out.println("apple phone complete");
}
}
public class SamsungPhone implements Phone {
@Override
public void complete() {
System.out.println("samsung phone complete");
}
}
public interface PhoneFactory {
default Phone orderPhone() {
Phone phone = createPhone();
phone.complete();
return phone;
}
Phone createPhone();
}
public class AppleFactory implements PhoneFactory {
@Override
public Phone createPhone() {
return new ApplePhone();
}
}
public class SamsungFactory implements PhoneFactory {
@Override
public Phone createPhone() {
return new SamsungPhone();
}
}
public class Client {
public static void main(String[] args) {
PhoneFactory appleFactory = new AppleFactory();
Phone applePhone = appleFactory.orderPhone();
PhoneFactory samsungFactory = new SamsungFactory();
Phone samsungPhone = samsungFactory.orderPhone();
}
}