JavaBookStudy / JavaBook

책읽기 스터디
https://javabookstudy.github.io/
Apache License 2.0
19 stars 2 forks source link

[Effective Java] Item 42. 전략 패턴 #27

Closed kjsu0209 closed 3 years ago

kjsu0209 commented 3 years ago

전략 패턴에서는 익명 클래스를 함수 객체처럼 사용했다고 하는데, 예를 들어 sort 메서드에서 Comparator 인터페이스는 추상 전략이고, 구체적인 전략은 익명 클래스로 구현합니다.

전략 패턴은 전략을 추상화된 부분과 구현 부분을 나누는 패턴이라고 생각하면 될까요? 그렇게만 생각하면 기능과 구현을 분리하는 브리지 패턴이랑 헷갈리는데 다른 좋은 예시가 있는지 궁금합니다 👀

daebalprime commented 3 years ago

브릿지 패턴은 처음 들어보는데 굉장히 흥미롭네요. 이미 많은 사람들이 궁금했던 주제인 것 같습니다.

The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

요약하자면, UML 클래스 다이어그램(클래스 간의 관계를 표현하는 그림)에서는 똑같다고 하지만, 전략 패턴에서 Comparable의 추상적인 전략(나는 비교를 할거야!)와 구체적인 구현(나의 특정 필드와 파라미터 인스턴스의 특정 필드를 비교해서 디테일하게 blahblah...)은 브릿지 패턴의 추상화와 구현부의 차이보다 더 강하게 결합이 되어있다고 하네요.

디자인 패턴쪽은 진짜 방대해서 압도당하는 기분입니다.

https://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern