public Service getService() {
if (service == null)
service = new MyServiceImpl(...);
return service;
}
public class Main {
public long sellFruit() {
Fruit fruit = FruitBuilder.getFruit("apple");
return new FruitService().sellFruit(fruit);
}
}
public interface Fruit {}
public class FruitBuilder {
public static Fruit getFruit(String fruitName) {
if (fruitName.equals("apple"))
return new Apple();
else if (fruitName.equals("grape"))
return new Grape();
else if (fruitName.equals("orange"))
return new Orange();
else
throw new IllegalArgumentException("없는 과일");
}
}
public class FruitService {
public long sellFruit(Fruit fruit) {
if (fruit.equals("apple"))
return 1000;
else if (fruit.equals("grape"))
return 2000;
else if (fruit.equals("orange"))
return 1500;
else
throw new IllegalArgumentException("없는 과일");
}
}
public class Main {
public long sellFruit() {
return new FruitService(new FruitFactoryImpl()).sellFruit("apple");
}
}
public interface Fruit {}
public interface FruitFactory {
Fruit getFruit(String fruitName);
}
public class FruitFactoryImpl implements FruitFactory {
public Fruit getFruit(String fruitName) {
if (fruitName.equals("apple"))
return new Apple();
else if (fruitName.equals("grape"))
return new Grape();
else if (fruitName.equals("orange"))
return new Orange();
else
throw new IllegalArgumentException("없는 과일");
}
}
public class FruitService {
private FruitFactory fruitFactory;
public FruitService(FruitFactoryImpl fruitFactory) {
this.fruitFactory = fruitFactory;
}
public long sellFruit(String fruitName) {
Fruit fruit = fruitFactory.getFruit(fruitName);
if (fruit.equals("apple"))
return 1000;
else if (fruit.equals("grape"))
return 2000;
else if (fruit.equals("orange"))
return 1500;
else
throw new IllegalArgumentException("없는 과일");
}
}