반응형
디자인 패턴이란?
특정 문맥에서 공통적으로 발생하는 문제에 대해 재사용 가능한 해결책이다. 소스나 기계 코드로 바로 전환될 수 있는 완성된 디자인은 아니며, 다른 상황에 맞게 사용될 수 있는 문제들을 해결하는데에 쓰이는 서술이나 템플릿이다. 디자인 패턴은 프로그래머가 어플리케이션이나 시스템을 디자인할 때 공통된 문제들을 해결하는데에 쓰이는 형식화 된 가장 좋은 관행이다. - 위키백과
왜 디자인 패턴을 설명하는가?
- 디자인 패턴 암기 X
- 모범 사례(Best Practice)를 통해 시야확장
- 유연한 설계를 지닌 객체지향의 장점을 살펴보는 것
- (가장 중요한 것) 디자인 패턴의 이름으로 명명된 클래스들이 다수 존재
SOLID 원칙
- SRP(Single Responsibility Principle) - 단일 책인 원칙
- OCP(Open/Closed Principle) - 개방-폐쇄 원칙
- LSP(Liskov Subsitution Principle) - 리스코프 치환 원칙
- ISP(Interface Segregation Principle) - 인터페이스 분리 원칙
- DIP(Dependency Inversion Principle) - 의존 관계 역전 원칙
LSP - 리스코프 치환원칙의 예제
- Square 가 Rectangle을 상속받고 있지만 대체하지 못하는 경우가 발생
import java.awt.*;
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(4);
rectangle.setHeight(5);
System.out.println(rectangle.getArea());
// Rectangle square = new Square();
Square square = new Square();
rectangle.setWidth(4);
rectangle.setHeight(5);
System.out.println(square.getArea());
}
}
public class Rectangle {
private int width;
private int height;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() {
return width * height;
}
}
public class Square extends Rectangle {
@Override
public void setWidth(int width) {
super.setWidth(width);
super.setHeight(width);
}
@Override
public void setHeight(int height) {
super.setWidth(height);
super.setHeight(height);
}
}
ISP - 인터페이스 분리 원칙 예제
public class Main {
public static void main(String[] args) {
//마린, 메딕
//unit
Movable marine = new Marine();
Attackable attackMarine = new Marine();
marine.move();
attackMarine.attack();
Movable medic = new Medic();
medic.move();
}
}
public interface Attakable {
void attack();
}
public interface Moveable {
void move();
}
public class Marin implements Movable, Attackable {
@Override
public void move() {
System.out.println("마린이 이동합니다.");
}
@Override
public void attack() {
System.out.println("마린이 공격합니다.");
}
}
public class Medic implements Movable {
@Override
public void move() {
System.out.println("메딕이 이동합니다.");
}
}
디자인 패턴 종류
GoF(Gang of Four) 디자인 패턴 구분
생성(Creational) 패턴
- 객체 생성 혹은 조합에 관한 패턴
- 추상 팩토리(Abstract Factory)
- 빌더(Builder)
- 팩토리 메서드(Factory Method)
- 프로토타입(Prototype)
- 싱글톤(Singleton)
구조(Structural) 패턴
- 객체를 조합해 더 큰 구조를 만드는 패턴
- 어댑터(Adapter)
- 브릿지(Bridge)
- 컴포지트(Composite)
- 데코레이터(Decorator)
- 퍼사드(Facade)
- 플라이웨이트(Flyweight)
- 프록시(Proxy)
행동(Behavioral) 패턴
- 객체 사이의 책임분배나 커뮤니케이션에 관한 패턴
- 책임 연쇄(Chain of Responsibility)
- 커맨트(Command)
- 인터프리터(Interpreter)
- 이터레이터(Iterator)
- 미디테이터(Mediator)
- 메멘토(Memento)
- 옵저버(Observer)
- 스테이트(State)
- 전략(Strategy)
- 템플릿 메서드(Template Method)
- 비지터(Visitor)
정리
- 디자인 패턴이 무엇인가?
- SOLID 5원칙
- GoF 디자인 패턴에 종류
- 암기 X, 코드의 이해도 ⬆️
반응형
'cs > java-spring-boot' 카테고리의 다른 글
[Zero-base] 11-3. 빌더(Builder) (0) | 2022.03.15 |
---|---|
[Zero-base] 11-2. 싱글턴(Singleton) (0) | 2022.03.15 |
[Zero-base] 10-6. 의존성 다루기 (0) | 2022.03.12 |
[Zero-base] 8-7. 강좌 관리 - 파일 업로드 구현 (0) | 2022.03.03 |
[Zero-base] 8-6. 강좌 관리 - 강좌별 강좌 신청 내역 구현 (0) | 2022.03.03 |