-
2023.09.02 기록장TIL 2023. 9. 2. 01:49
연쇄 책임 패턴 vs list<class>
둘은 행위는 비슷하지만 구현하는 방법에 따라 목적이 달라질 수 있다.
체이닝은 request에 재귀적인 전처리 후처리를 할 수 있지만 list구현은 재귀적으로 하려면 복잡한 콜백등의 구현을 해야 한다.
또 체이닝은 명시적으로 절차를 설명할 수 있다.
list방식은 class끼리의 연관관계없이 request를 혼자서 처리할 수 있을 때 사용할 수 있다.
본인은 컬랙션 방식을 더 좋아하는데 이유는 체이닝 보다 코드가 더 간단하다는 점 때문이다.
딱히 재귀적 후처리가 필요하지 않고, 순서 보장이 필요 없다면 좋은 선택지이다.
다만 모든 list를 돌아도 실행을 할 수 있는 클래스가 없다면 default에서 실행하도록 또는 예외를 뱉도록 해줘야 한다.
체이닝에서는 가장 마지막 체인에서 할 수 있는 행위이다.
What are the advantages of chain-of-responsibility vs. lists of classes?
Recently, I was discussing with another programmer the best way to refactor a huge(1000 lines) method full of "if" statements. The code is written in Java, but I guess this issue could happen in o...
stackoverflow.com
Is Chain of Responsibility pattern just an overkill ? A List of Handlers can accomplish the same
In the 'Chain of Responsibility(COR)' pattern, we create a chain of handlers. Pass the request to the first in the chain. It tries to handle it. If it cannot, it forwards the request to the next in...
stackoverflow.com
'TIL' 카테고리의 다른 글
2023.09.21 기록장 (0) 2023.09.21 2023.09.20 기록장 (0) 2023.09.20 2023.08.31 기록장 (0) 2023.08.31 2023.08.30 기록장 (0) 2023.08.30 2023.08.12 기록장 (0) 2023.08.12