-
[정보처리기사 필기] 디자인 패턴 (Design Pattern), 행위 패턴(2)CS/정보처리기사 2023. 8. 16. 14:07반응형
행위패턴
① 비지터(Visitor)
- 각 클래스 데이터 구조로부터 처리 기능을 분리하여 별도의 클래스를 만들어 놓고 해당 클래스의 메서드가 각 클래스를 돌아다니며 특정 작업을 수행하도록 만드는 패턴으로, 객체의 구조는 변경하지 않으면서 기능만 따로 추가하거나 확장할 때 사용하는 디자인 패턴
- 특정 구조를 이루는 복합 객체의 원소 특성에 따라 동작을 수행할 수 있도록 지원하는 행위
▶ Visitor 패턴의 클래스 다이어그램
▶ 등장역할
등장인물 역할 Visitor(방문자)의 역할 - 데이터 구조의 구체적인 요소(ConcreteElement 역할)마다 'xxxx을 방문했다'라는 visit(xxxx) 메서드를 선언
- visit(xxxx)는 xxxx을 처리하기 위한 메서드이고, 실제의 코드는 ConcreteVisitor 역할에 기술되어 있음ConcreteVisitor(구체적인 방문자)의 역할 - Visitor 역할의 인터페이스(API)를 구현 Element(요소)의 역할 - Visitor 역할의 방문할 곳을 나타내는 역할
- 방문자를 받아들이는 accept 메서드를 선언ConcreteElement(구체적 요소)의 역할 - Element 역할의 인터페이스(API)를 구현하는 역할 ObjectStructure(오브젝트의 구조)의 역할 - Element 역할의 집합을 취급하는 역할 ② 커맨드(Command)
- 실행될 기능을 캡슐화함으로써 주어진 여러 기능을 실행할 수 있는 재사용성이 높은 클래스를 설계하는 패턴으로 하나의 추상 클래스에 메서드를 만들어 각 명령이 들어오면 그에 맞는 서브 클래스가 선택되어 실행되는 특징을 갖는 디자인 패턴
- 요구사항을 객체로 캡슐화
▶ Command 패턴의 클래스 다이어그램
▶ Command 패턴의 시퀀스 다이어그램
▶ 등장역할
등장인물 역할 Command(명령)의 역할 - 명령의 인터페이스(API)를 정의하는 역할 ConcreteCommand(구체적 명령)의 역할 - Command 역할의 인터페이스(API)를 실제로 구현하고 있는 역할 Receiver(수신자)의 역할 - Command 역할이 명령을 실행할 때 대상이 되는 역할
- 명령을 받아들이는 수신자Client(의뢰자)의 역할 - ConcreteCommand 역할을 생성하고, 그 사이에 Receiver 역할을 할당 Invoke(기동자)의 역할 - 명령의 행동을 개시하는 역할 ③ 전략(Strategy)
- 알고리즘 군을 정의하고(추상 클래스) 같은 알고리즘을 각각 하나의 클래스로 캡슐화한 다음, 필요할 때 서로 교환해서 사용할 수 있게 하는 패턴으로, 행위를 클래스로 캡슐화해 동적으로 행위를 자유롭게 바꿀 수 있게 해주는 디자인 패턴
- 행위 객체를 클래스로 캡슐화해 동적으로 행위를 자유롭게 변환
▶ Strategy 패턴의 클래스 다이어그램
▶ 등장역할
등장인물 역할 Strategy(전략)의 역할 - Strategy는 전략을 이용하기 위한 인터페이스(API)를 결정 ConcreteStrategy(구체적인 전략)의 역할 - Strategy의 인터페이스(API)를 실제로 구현
- 구체적인 전략(작전, 방책, 방법, 알고리즘)을 실제로 프로그래밍Context(문맥)의 역할 - Strategy을 이용하는 역할
- ConcreteStrategy의 인스턴스를 가지고 있으며 필요에 따라 그것을 이용④ 메멘토(Memento)
- 클래스 설계 관점에서 객체의 정보를 저장할 필요가 있을 때 적용하는 디자인 패턴으로 Undo 기능을 개발할 때 사용하는 디자인 패턴
- 객체를 이전 상태로 복구시켜야 하는 경우, '작업취소(Undo)' 요청가능
▶ Memento 패턴의 클래스 다이어그램
▶ 등장역할
등장인물 역할 Originator(작성자)의 역할 - 자신의 현재 상태를 저장하고 싶을 때 Memento 역할을 만듦
- 또한, 이전의 Memento 역할을 전달받으면 그 Memento 역할을 만든 시점의 상태로 돌리는 처리를 실행Memento(기념품)의 역할 - Originator 역할의 내부 정보를 정리
- Originator 역할의 내부 정보를 가지고 있지만, 그 정보를 누구에게도 공개하지 않음Caretaker(관리인)의 역할 - 현재의 Originator 역할의 상태를 저장하고 싶을 때, 그것을 Originator 역할에 전함 ⑤ 책임 연쇄(Chain of Responsibility)
- 정적으로 어떤 기능에 대한 처리의 연결이 하드코딩 되어 있을 때 기능 처리의 연결 변경이 불가능한데, 이를 동적으로 연결되어 있는 경우에 따라 다르게 처리될 수 있도록 연결한 디자인 패턴
- 한 요청을 2개 이상의 객체에서 처리
▶ Chain of Responsibility 패턴의 클래스 다이어그램
Reference
반응형'CS > 정보처리기사' 카테고리의 다른 글
[정보처리기사 실기] 과목11 - 응용 SW 기초 기술 활용, 오답노트 (0) 2023.10.03 [정보처리기사 실기] 과목10 - 애플리케이션 테스트 관리, 오답노트 (1) 2023.10.01 [정보처리기사 필기] 디자인 패턴 (Design Pattern), 행위 패턴(1) (0) 2023.08.16 [정보처리기사 필기] 디자인 패턴 (Design Pattern), 구조 패턴 (0) 2023.08.16 [정보처리기사 필기] 디자인 패턴 (Design Pattern), 개념및 유형 + 생성 패턴 (0) 2023.08.16