Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 1차원 DP
- 2차원 dp
- 99클럽
- @BeforeAll
- @BeforeEach
- @Builder
- @Entity
- @GeneratedValue
- @GenericGenerator
- @NoargsConstructor
- @Query
- @Table
- @Transactional
- Actions
- Amazon EFS
- amazon fsx
- Android Studio
- ANSI SQL
- ApplicationEvent
- assertThat
- async/await
- AVG
- AWS
- Azure
- bind
- builder
- button
- c++
- c++ builder
- c03
Archives
- Today
- Total
기록
디자인 패턴/java/Command Pattern 본문
커맨드 패턴(Command pattern)
요청을 객체의 형태로 캡슐화하여 사용자가 보낸 요청을 나중에 이용할 수 있도록 매서드 이름, 매개변수 등 요청에 필요한 정보를 저장 또는 로깅, 취소할 수 있게 하는 패턴
장점 | 작업을 수행하는 객체와 요청하는 객체를 분리하여 단일 책임 원칙에 부합 코드의 수정 없이 작업 수행 객체나 추가 구현이 가능하여 개방-폐쇄 원칙에 부합 |
단점 | 리시버 객체의 동작이 늘어날 때 마다 커맨드 클래스가 늘어나기 때문에 클래스가 많아진다. |
구성요소
- 명령(command)
- 발동자(invoker)
- 클라이언트(client)
- 수신자(receiver)
예제 : 만능 버튼
버튼으로 음악과 램프를 켤 수 있도록 하자.
- 구성
- Button
class Button {
Command command;
Button(Command command){
this.command = command;
}
String pressed() {return command.execute();}
}
- Command
interface Command{
String execute();
}
- Lamp
class LampCommand implements Command{
private Lamp lamp;
@Override
public String execute() {
return lamp.lampOn();
}
}
class Lamp{
String lampOn(){
return "Lamp turns on.";
}
}
- Music
class MusicCommand implements Command{
private Music music;
@Override
public String execute() {
return music.musicOn();
}
}
class Music{
String musicOn(){
return "Music turns on.";
}
}
- Client
LampCommand lampCommand = new LampCommand();
MusicCommand musicCommand = new MusicCommand();
Button lampButton = new Button(lampCommand);
Button musicButton = new Button(musicCommand);
assertEquals("Lamp turns on.", lampButton.pressed());
assertEquals("Music turns on.", musicButton.pressed());
- 결과
참고 자료
'Moblie > Android' 카테고리의 다른 글
AndroidStudio/java/ 멀티뷰 타입 Recyclerview (0) | 2022.05.29 |
---|---|
androidStudio/kotlin/preference (0) | 2022.05.26 |
[issue] ScrollView vs NestScrollView (0) | 2022.05.12 |
andriodStudio/다크 테마 지원하기 (0) | 2022.05.11 |
androidStudio/kotlin/cutomview (0) | 2022.04.25 |
Comments