rainsister
  • 신비한 비 Blog
  • Design Pattern
    • Adapter Pattern
    • Proxy Pattern
    • Mediator Pattern
    • Visitor Pattern
    • State Pattern
    • Memento Pattern
    • Factory Pattern
    • Template Pattern
    • Strategy Pattern
    • Bridge Pattern
  • springboot 2.x
    • 메시징 - 큐
      • RabbitMQ 기본설정
      • RabbitMQ 지연 큐(delayed queue)
    • Log
      • 기본 Log 설정 및 Logback 설정
      • Log4j2 사용해보기
      • tinylog 모듈 사용해보기
    • 기타 Database 에 대한 기본가이드
      • PostgreSQL
      • InfluxDB
      • MongoDB
    • 수행속도 UP! 각종 cache 어노테이션 사용법
      • Redis를 사용하여 Cache 관리하기
      • EhCache 사용해보기
      • Thread내캐쉬사용 및 Cache 어노테이션 사용법
    • Database Connection
      • Springboot 2.5 이후 data init script 초기화에 대한 변경
      • JTA 로 JPA 다중 DataSource 트랜잭션 처리 하기
      • Flyway 로 DataBase 형상 관리해보자
      • 트랜잭션 기초읽기
      • MyBatis 의 다중 DataSource
      • Spring Data JPA 다중 DataSource
      • JdbcTemplate 다중 DataSource
      • XML 로 Mybatis 설정하기
      • Mybatis 로 Mysql 연결하기
      • ORM(Spring data jpa)
      • Druid datasource 연결
      • Hikari 설정
      • JdbcTemplate 로 db 접근
    • rest api
      • XML에 대한 요청 및 응답 처리
      • SpringFox 3 및 Swagger 설정
      • 프로젝트 구동 시 RequestMappingHandler 로그 설정
      • Swagger 의 api들을 분류하는 법
      • 간단한 Restful API 만들고 테스트 코드 작성
      • Swagger2 구성하여 API 문서 자동화하기
      • JSR-303 그리고 validation
    • 설정
      • 시작
      • 멀티환경구성에 대한 새로운 방법
      • 멀티환경구성에 대한 새로운 include
      • 프로젝트 설정파일
      • 민감한 정보에 대한 암호화
  • java버전별차이
    • JAVA18
    • JAVA9
    • JAVA10
    • JAVA11
    • JAVA14
    • JAVA15
    • JAVA17
    • JAVA16
  • spring노하우
    • BeanUtils 권장하지 않는이유
    • 개인정보 암호화
    • Springboot 3가지 CROS 설정
    • Springboot 내장된 유용한 Utils
    • Spring Security WebSecurityConfigurerAdapter 가 deprecated 된 이슈해결하기
    • 아직도 HttpUtil ? SpringBoot 3.0의 HTTP Client Util 을 사용해보라
    • JDBC 소스를 뽀개기
    • spring-boot-configuration-processor 는 뭐하는놈임?
    • Apache BeanUtils vs Spring BeanUtils 성능비교
  • Effetive Java 3th
    • Finalizer & Cleaner
Powered by GitBook
On this page
  • SimpleFactory 방식
  • FactoryMethod 방식
  • Abstractor Factory 방식
  1. Design Pattern

Factory Pattern

SimpleFactory 방식

public interface IProuduct {
    void doSomeThing();
}
public class ProductA implements IProuduct {
    @Override
    public void doSomeThing() {
        System.out.println("ProductA 입니다!");
    }
}
public class ProductB implements IProuduct {
    @Override
    public void doSomeThing() {
        System.out.println("ProductB 입니다!");
    }
}
public class SimpleFactory {
    static IProuduct makeProduct(String productName) {
        if ("ProductA".equals(productName)) {
            return new ProductA();
        } else if ("ProductB".equals(productName)) {
            return new ProductB();
        } else {
            return null;
        }
    }
}
public class Client {
    public static void main(String[] args) {
        // ProductB 만든다.
        IProuduct product = SimpleFactory.makeProduct("ProductB");
        product.doSomeThing();
    }
}

장점

SimpleFactory 패턴은 모델은 객체 생성 로직을 캡슐화하고 비즈니스 코드 로직과 객체 생성 로직을 분리 한다. 객체가 생성되는 로직을 변경할 때는 여러 service 파일에서 이 부분의 코드를 찾아서 수정해야 하지만 SimpleFactory패턴을 사용한 하면 SimpleFactory 에서 생성된 객체의 논리만 수정하면 되고, 업무 코드를 수정할 필요가 없음.

단점

특정 제품 카테고리의 추상 제품 카테고리가 증가할 때마다 SimpleFactory 카테고리에 새로운 제품 카테고리 객체 생성 방법을 추가해야 함. 추상 제품 종류가 많을 때, 추상 공장은 매우 비대해진다. 그리고 이 경우 SimpleFactory도 개폐 원칙에 부합하지 않음.

FactoryMethod 방식

public interface IFactory {
    IProduct makeProduct();
}
public interface IProduct {
    void doSomeThing();
}
public class ProductA implements IProduct {
    @Override
    public void doSomeThing() {
        System.out.println("ProductA 입니다!");
    }
}
public class ProductB implements IProduct {
    @Override
    public void doSomeThing() {
        System.out.println("ProductA 입니다!");
    }
}
public class FactoryA implements IFactory {
    @Override
    public IProduct makeProduct() {
        return new ProductA();
    }
}
public class FactoryB implements IFactory {
    @Override
    public IProduct makeProduct() {
        return new ProductB();
    }
}
public class Client {
    public static void main(String[] arges) {
        // ProductA 만듬.
        FactoryA factoryA = new FactoryA();
        factoryA.makeProduct().doSomeThing();
    }
}

장점

ProductC를 추가할 때 특정 제품 클래스 ProductC와 ProductC 생산을 담당하는 특정 공장 FactoryC를 생성하기만 하면 됨.개폐 원칙'에 부합하고 확장이 용이함.

단점

Abstractor Factory 방식

public interface AbstractFactory {
    Basketball makeBasketball();
    Football makeFootball();
}
public interface Basketball {
    void sayBasketball();
}
public interface Football {
    void sayFootball();
}
public class NikeBasketball implements Basketball {
    @Override
    public void sayBasketball() {
        System.out.println("나이키 농구공!");
    }
}
public class NikeFootball implements Football {
    @Override
    public void sayFootball() {
        System.out.println("나이키 축구공!");
    }
}
public class AdidasBasketball implements Basketball {
    @Override
    public void sayBasketball() {
        System.out.println("adidas 농구공!");
    }
}
public class AdidasFootball implements Football {
    @Override
    public void sayFootball() {
        System.out.println("adidas 농구공!");
    }
}
public class NikeFactoy implements AbstractFactory {
    @Override
    public Basketball makeBasketball() {
        return new NikeBasketball();
    }

    @Override
    public Football makeFootball() {
        return new NikeFootball();
    }
}
public class AdidasFactory implements AbstractFactory {
    @Override
    public Basketball makeBasketball() {
        return new AdidasBasketball();
    }

    @Override
    public Football makeFootball() {
        return new AdidasFootball();
    }
}
public class Client {
    public static void main(String[] args){
        // nike농구공 , adidas축구공 생산!
        NikeFactoy nikeFactoy = new NikeFactoy();
        AdidasFactory adidasFactory = new AdidasFactory();

        nikeFactoy.makeBasketball().sayBasketball();
        adidasFactory.makeFootball().sayFootball();
    }
}
PreviousMemento PatternNextTemplate Pattern

Last updated 8 months ago

종류의 개수가 너무 많으면 복잡도가 증가한다. 추상적 공장 인터페이스를 실현하는 구체적인 공장은 하나의 제품만 생산할 수 있다( 방식으로 해결할 수 있다)

소스코드
abstractor factory