디자인 모델 (3) 단순 공장 모델

/**
 *       (Simple Factory):                  ,
 *                   
 */
public class SimpleFactory {

	public static void main(String[] args) {
		//              ,                 
		IFruit fruit = Factory.getFruit("  ");
		if (fruit == null) {
			System.out.println("     ");
		} else {
			System.out.println(fruit.get());
		}
	}
}

interface IFruit {
	public String get();
}

class Factory {
	public static IFruit getFruit(String name) {
		if (name.equals("  ")) {
			return new Apple();
		} else if (name.equals("  ")) {
			return new Banana();
		} else {
			return null;
		}
	}
}

class Apple implements IFruit {

	@Override
	public String get() {
		return "  ";
	}

}

class Banana implements IFruit {

	@Override
	public String get() {
		return "  ";
	}

}

좋은 웹페이지 즐겨찾기