DevEnjoy

One Step Closer

Day: 2015년 7월 7일

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, 디자인패턴

Template Method 디자인 패턴

2015년 7월 7일 by YongPwi Leave a Comment
  • 변하지 않는 기능은 슈퍼클래스에 생성하고 자주 변경되며 확장할 기능은 서브클래스에 만든다.
  • 슈퍼클래스에 추상 메소드 또는 오버라이드 가능한 메소드를 정의
  • 코드의 기본 알고리즘을 담고 있는 템플릿 메소드 생성
  • 서브클래스에서 추상 메소드 구현 또는 훅 메소드 오버라이드하여 기능 확장
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
abstract class Work {
    // 1. 추상 메소드 또는 오버라이드 가능한 메소드를 정의
    public void workFlow() {
        stepOne();
        stepTwo();
        stepThr();
        stepFor();
    }
    // 2. 일반적인 구현은 기본 클래스에 정의
    protected void stepOne() { System.out.println( "Work.stepOne" ); }
    // 3. 변경 또는 기능확장이 필요한 메소드는 추상 메소드로 선언
    abstract protected void stepTwo();
    abstract protected void stepThr();
    protected void stepFor() { System.out.println( "Work.stepFor" ); }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
abstract class Teacher extends Work{
    // 4. 서브 클래스는 메소드 구현 또는 오버라이드하여 기능 확장
    // 1. 추상 메소드 또는 오버라이드 가능한 메소드를 정의
    protected void stepThr() {
        step3_1();
        step3_2();
        step3_3();
    }
    // 2. 일반적인 구현은 기본 클래스에 정의
    protected void step3_1() {
        System.out.println( "Teacher.step3_1" );
    }
    // 3. 변경 또는 기능확장이 필요한 메소드는 추상 메소드로 선언
    abstract protected void step3_2();
    protected void step3_3() {
        System.out.println( "Teacher.step3_3" );
    }
}
1
2
3
4
5
6
7
8
9
10
class MathTeacher extends Teacher {
    // 4. 서브 클래스는 메소드 구현 또는 오버라이드하여 기능 확장
    protected void stepTwo() { System.out.println( "MathTeacher   .stepTwo" ); }
    protected void step3_2() { System.out.println( "MathTeacher   .step3_2" ); }

    protected void stepFor() {
        System.out.println( "MathTeacher   .stepFor" );
        super.stepFor();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class TemplateMethodDemo {
   public static void main( String[] args ) {
       Work work = new MathTeacher();
       work.workFlow();
   }
}

// 결과
Work.stepOne
MathTeacher   .stepTwo
Teacher.step3_1
MathTeacher   .step3_2
Teacher.step3_3
MathTeacher   .stepFor
Work.stepFor
Posted in: Java, Programing Tagged: JAVA, Template Method, 디자인패턴

Calendar

7월 2015
일 월 화 수 목 금 토
« 2월   9월 »
 1234
567891011
12131415161718
19202122232425
262728293031  

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].