Skip to content

템플릿 패턴#8

Merged
guswns3371 merged 1 commit intomainfrom
feature/template
Jan 7, 2023
Merged

템플릿 패턴#8
guswns3371 merged 1 commit intomainfrom
feature/template

Conversation

@guswns3371
Copy link
Member

No description provided.

Comment on lines +3 to +31
public abstract class AbstractClass {
final public void templateMethod() {
primitiveOperation1();
primitiveOperation2();
concreteOperation();
hook();
}

/**
* 구상 클래스에서 구현할 메소드
*/
abstract void primitiveOperation1();

abstract void primitiveOperation2();

/**
* 추상 클래스 내에서 정의되는 메소드. (구상클래스에서 오버라이드 불가)
*/
final void concreteOperation() {

}

/**
* 템플릿 메소드 속 "후크"
* 구상 클래스에서 오버라이드가 필수는 아님.
*/
void hook() {
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

템플릿 메소드 패턴

알고리즘의 골격 (개요)를 정의하는 디자인 패턴이다.
알고리즘의 일부 단계를 서브클래스에서 구현 또는 재정의하면서 알고리즘의 골격을 그대로 유지한다.

후크

필수적이지 않은 알고리즘 단계의 일부를 서브클래스에서 구현하도록 만들고 싶을 때 사용한다.
필수가 아닌 부분을 후크로 구현하면 추상 클래스에서 구현할 메소드가 줄어 부담이 줄기도 한다.

할리우드 원칙

저수준 구성 요소가 시스템에 접속할 수 있지만, 해당 구성요소를 언제 어떻게 사용하지를 결정하는 건 고수준 구성 요소여야 한다는 원칙

  • 의존성 부패 (의존성이 복잡하게 꼬여있는 상황)을 방지하기 위한 디자인 원칙이다.
  • 템플릿 메소드 패턴도 할리우드 원칙을 따른다

image

Comment on lines +42 to +53
public static class Tea extends CaffeineBeverage {

@Override
void brew() {
System.out.println("찻잎을 우려내는 중");
}

@Override
void addCondiment() {
System.out.println("레몬을 추가하는 중");
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        @Override
        void brew() {
            boilWater(); // 저수준 구성요소에서 고수준 구성 요소의 메소드를 호출하는 경우도 있다. 
            System.out.println("찻잎을 우려내는 중");
        }

위와 같이 할리우드 법칙을 위배하게 되면 저수준 구성요소(Tea)와 고수준 구성요소(CaffeineBeverage) 사이에 순환 의존성이 생길 수 있다

Comment on lines +5 to +33
/**
* 탬플릿 메소드 활용 : AbstractList
* <p>
* subList 메소드에서 사용되는 get(), size() 메소드를 오버라이딩한다
*/
public class MyStringList extends AbstractList<String> {

private final String[] list;

public MyStringList(String[] list) {
this.list = list;
}

@Override
public String get(int index) {
return list[index];
}

@Override
public int size() {
return list.length;
}

public String set(int index, String item) {
String old = list[index];
list[index] = item;
return old;
}
}
Copy link
Member Author

@guswns3371 guswns3371 Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바 API 속 템플릿 메소드 패턴

Arrays의 sort() 메소드

sort() 메소드 + Comparable 인터페이스(compareTo() 구현)

정렬에 필요한 정보를 Comparable 인터페이스를 구현하여 전달해야하기에 온전한 템플릿 메소드라 부를 수 없지만 템플릿 메소드 패턴의 성격을 띤다.

Arrays의 sort() 메소드는 전략패턴이라고 볼 수 없는 이유

전략 패턴에서는 구성할 때 사용하는(즉, 객체 레퍼런스에 의해 참조되는) 클래스에서 알고리즘을
완전히 구현한다. 하지만 compareTo()를 다른 클래스에서 제공해 줘야하므로 Arrays 클래스에서 사용하는 알고리즘은 불완전하다.

AbstractList의 subList() 메소드

AbstractList에는 get()과 size() 추상 메소드에 의존하는 subList() 템플릿 메소드가 있다.

@guswns3371 guswns3371 merged commit 0d29818 into main Jan 7, 2023
@guswns3371 guswns3371 deleted the feature/template branch January 7, 2023 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant