예시 1. 가장 단순한 형태의 팩토리 매서드 패턴 - 푸트코트 도시락
public class FoodCourt {
public static void main(String[] args) {
Dosirak defaultDosirak = DosirakFactory.order("Default", "spam");
Dosirak spamDosirak = DosirakFactory.order("SpamDosirak", "spam");
Dosirak tunaDosirak = DosirakFactory.order("TunaDosirak", "tuna");
System.out.println("defaultDosirak = " + defaultDosirak);
System.out.println("spamDosirak = " + spamDosirak);
System.out.println("tunaDosirak = " + tunaDosirak);
}
}
abstract class Dosirak {
public abstract String mainIngredient();
@Override
public String toString() {
return "Here is " + this.mainIngredient();
}
}
class DosirakFactory {
public static Dosirak order(String type, String mainIngredient){
if ("SpamDosirak".equalsIgnoreCase(type)) {
return new SpamDosirak(mainIngredient);
}
if ("TunaDosirak".equalsIgnoreCase(type)) {
return new TunaDosirak(mainIngredient);
}
return new DefaultDosirak();
}
}
class SpamDosirak extends Dosirak {
private final String main;
public SpamDosirak(String main) {
this.main = main;
}
@Override
public String mainIngredient() {
return this.main;
}
}
class TunaDosirak extends Dosirak {
private final String main;
public TunaDosirak(String main) {
this.main = main;
}
@Override
public String mainIngredient() {
return this.main;
}
}
class DefaultDosirak extends Dosirak {
private final String main;
public DefaultDosirak() {
this.main = "김치";
}
@Override
public String mainIngredient() {
return this.main;
}
}
/**
* defaultDosirak = Here is 김치
* spamDosirak = Here is spam
* tunaDosirak = Here is tuna
* */
예시 2. 조금 더 복잡한 형태의 패턴 - 배 만들기
public class SimpleFactory {
public Object createProduct(String name) {
if (name.equals("whiteship")) {
return new Whiteship();
} else if (name.equals("blackship")) {
return new Blackship();
}
throw new IllegalArgumentException();
}
}
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
...
private static Calendar createCalendar(TimeZone zone, Locale aLocale) {
CalendarProvider provider = LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale).getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException var7) {
}
}
Calendar cal = null;
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
byte var6 = -1;
switch(caltype.hashCode()) {
case -1581060683:
if (caltype.equals("buddhist")) {
var6 = 0;
}
break;
case -752730191:
if (caltype.equals("japanese")) {
var6 = 1;
}
break;
case 283776265:
if (caltype.equals("gregory")) {
var6 = 2;
}
}
switch(var6) {
case 0:
cal = new BuddhistCalendar(zone, aLocale);
break;
case 1:
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case 2:
cal = new GregorianCalendar(zone, aLocale);
}
}
}
if (cal == null) {
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja" && aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return (Calendar)cal;
}
}
public class SpringBeanFactoryExample {
public static void main(String[] args) {
BeanFactory xmlFactory = new ClassPathXmlApplicationContext("config.xml");
String hello = xmlFactory.getBean("hello", String.class);
System.out.println(hello);
BeanFactory javaFactory = new AnnotationConfigApplicationContext(Config.class);
String hi = javaFactory.getBean("hello", String.class);
System.out.println(hi);
}
}