Notice
Recent Posts
Recent Comments
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

DeFacto-Standard IT

Factory Method Pattern과 Abstract Factory Pattern 공통점과 차이점 본문

Design Pattern/Creational Pattern

Factory Method Pattern과 Abstract Factory Pattern 공통점과 차이점

defacto standard 2017. 9. 26. 07:08

공통점 & 차이점

 

 

 

공통점 1. Template Method Pattern을 사용.

 

 

공통점 2. Factory 클래스를 사용하여 객체를 생성. 

 

 

공통점 3. Factory Method Pattern을 사용.

둘 다 추상적인 클래스와 Factory를 통해 실제 구현대상인 Concrete클래스와 Client간의 결합도를 낮춘다. 인자에 따라 생성되는 객체가 결정된다.

 

 

 

 

 

 

차이점 1. Factory 클래스에서 객체에 대한 생성을 지원하는 범위

Factory Method Pattern - 한 Factory 당 한 종류(create() 메서드가 Factory 클래스에 1개)

Abstract Factory Pattern - 한 Factory 에서 서로 연관된 여러 종류 모두 지원(create() 메서드가 Factory 클래스에 여러 개)

 

 

차이점 2. Factory Method 에서 만드는 객체의 종류

Factory Method Pattern의 Factory Method - 인자에 따라 객체의 종류가 결정됨.

Abstract Factory Pattern의 Factory Method - 인자에 따라 관련된 객체들을 생성하는 Factory의 종류가 결정됨.

 

 

차이점 3. 결합도를 낮추는 대상

Factory Method Pattern - ConcreteProduct와 Client간의 결합도를 낮출 때 사용

Abstract Factory Pattern - ConcreteFactory와 Client간의 결합도를 낮출 때 사용

 

 

차이점 4. 포커스

Factory Method Pattern - 메소드(Factory Method) 레벨에서 포커스를 맞춤으로서, Client의 ConcreteProduct 인스턴스 생성 및 구성에 대한 책임을 덜어줌

Abstract Factory Pattern - 클래스(Abstract Factory) 레벨에서 포커스를 맞춤으로써, 각 Product들이 다른 클래스와 함께 사용될 때의 제약사항을 강제할 수 있다. 새로운 ConcreteFactory를 추가할 때 많은 작업이 필요하다.

 

 

차이점 5. Method와 Object

Factory Method는 단일 Method.

Abstract Factory는 Object.

 

 

차이점 6. Inhritance, Composition

Factory Method Pattern - 상속을 사용하여, 객체의 인스턴스 생성에 대해서는 서브 클래스에 의존. 즉, 지역 레퍼런스 없이 바로 하위 클래스의 함수를 호출하여 객체를

class A {
   
public void createFoo() {
        Abstract
Foo foo = makeFoo();
        foo
.do();  
   
}

   
protected abstract AbstractFoo makeFoo();

}

class B extends A {
   
protected AbstractFoo makeFoo() {
       
//subclass is overriding the factory method
       
//to return something different
       
return new ConcreteFoo();
   
}
}

 

 

Abstract Factory Pattern - 지역 레퍼런스를 두어 외부로부터 Factory 객체를 DI받아서 위임

class A {
   
private Factory factory;

   
public A(Factory factory) {

         this.factory = factory;
   
}

   
public void do() {
       
//The concrete class of "foo" depends on the concrete class
       
//of the factory passed into the constructor. If you provide a
       
//different factory, you get a different Foo object.
       
Foo foo = factory.makeFoo();
        foo
.do();
   
}
}

interface Factory {
   
Foo makeFoo();
}

//need to make concrete factories that implement the "Factory" interface here

'Design Pattern > Creational Pattern' 카테고리의 다른 글

Builder Pattern  (0) 2018.02.16
Prototype Pattern / Cloneable Interface  (0) 2018.02.15
Abstract Factory Pattern  (0) 2017.09.26
Factory Method Pattern (기본)  (0) 2017.09.26
Singleton Pattern  (0) 2017.09.23
Comments