DevEnjoy

One Step Closer

Factory Method

Factory Method 디자인 패턴

2015년 7월 7일 by YongPwi Leave a Comment
  • Template Method과 마찬가지로 상속을 통해 기능확장
  • 슈퍼클래스 코드에서는 서브클래스에서 구현할 메소드를 호출해서 필요한 타입의 오브젝트를 가져와 사용
  • 슈퍼클래스는 서브클래스가 어떤 클래스 오브젝트를 리턴할지 모름
  • 서브클래스는 다양한 방법으로 오브젝트 생성 메소드를 재정의
  • 서브클래스에서 오브젝트 생성 방법과 클래스를 결정할 수 있도록 미리 정의해둔 메소드 = Factory Method
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
31
32
33
34
// Book 클래스
public class Book {
    String title;
    String author;
    String price;

    public void print(){
        System.out.println("title = " + title);
        System.out.println("author = " + author);
        System.out.println("prive = " + price);
    }

    public String getTitle(){
        return title;
    }
}

// 여러가지 책
class ComicBook extends Book{
    public ComicBook(){
        this.title = "럭키짱";
        this.author = "김성모";
        this.price = "1000원";
    }
}

// 여러가지 책
class CookBook extends Book{
    public CookBook(){
        this.title = "한신포차";
        this.author = "백종원";
        this.price = "2000원";
    }
}
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
31
32
// Book 생성 클래스(추상클래스)
public abstract class BookStore {
    public BookStore(){

    }

    public Book orderBook(String type){
        Book book = createBook(type);
        book.print();
        return book;
    }

    abstract Book createBook(String type);
}

// 서브 클래스 실제 사용할 클래스 오브젝트 생성하여 리턴
class Yes24BookStore extends BookStore{

    @Override
    Book createBook(String type) {
        return new ComicBook();
    }
}

// 서브 클래스 실제 사용할 클래스 오브젝트 생성하여 리턴
class KangComBookStore extends BookStore{

    @Override
    Book createBook(String type) {
        return new CookBook();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 고객이 책 구매
public class UserOrder {
    public static void main( String[] args ) {
        BookStore yes24 = new Yes24BookStore();
        BookStore kangcom = new KangComBookStore();
        Book book = yes24.orderBook("bookType");
        System.out.println("yes24 order=" + book.getTitle());
        Book book2 = kangcom.orderBook("bookType");
        System.out.println("kangcom order=" + book2.getTitle());
    }
}

// 출력결과
title = 럭키짱
author = 김성모
prive = 1000원
yes24 order=럭키짱
title = 한신포차
author = 백종원
prive = 2000원
kangcom order=한신포차
Posted in: Java, Programing Tagged: Factory Method, JAVA, 디자인패턴

Calendar

6월 2025
일 월 화 수 목 금 토
« 4월    
1234567
891011121314
15161718192021
22232425262728
2930  

Recent Posts

  • ubuntu bastion 설정
  • Spring Boot properties 암호화
  • Git Repository Bitbucket과 Issue Tracker Redmine 연동 설정
  • Spring Security 동일 session 제어
  • Spring @Mock, @Mockbean, @InjectMock

Recent Comments

  • pzotov (Ubuntu 14.04에서 Sonarqube 6.7.1 service 등록)
  • cours de theatre paris (AWS ELB와 Auto Scaling 연동, nginx)
  • bayern munich (IntelliJ EAP Font rendering)
  • camiseta del chelsea (OS X에서 APP 아이콘 변경)
  • cheap football shirts replica (jQuery Ajax에서 json Array 직렬화)

Copyright © [the-year] [site-link].

Powered by [wp-link] and [theme-link].