욱'S 노트

Abstract Factory - 공장 자체를 제공하자 본문

Methdology/Design Pattern

Abstract Factory - 공장 자체를 제공하자

devsun 2012. 4. 9. 18:51
When Using It

생성이 복잡한 부품들을 제조하는 공장 자체를 만들어 제공하고 싶을때

Class Diagram



Sample Code



public class AbstractProduct {

}


public class ConcreteProduct extends AbstractProduct {

}


public class ConcreteProduct2 extends AbstractProduct {

}



public abstract class AbstractFactory {

public AbstractFactory getFactory(String name) {

if (name.equals("A")) {

return new ConcreteFactory();

} else {

return null;

}

}

protected abstract AbstractProduct createProduct();


protected abstract AbstractProduct createProduct2();

}

 

public class ConcreteFactory extends AbstractFactory {

@Override

public AbstractProduct createProduct() {

return new ConcreteProduct();

}

@Override

public AbstractProduct createProduct2() {

return new ConcreteProduct2();

}


}



Caution


특별한 주의사항은 없다.
Comments