Design Patterns

반응형
반응형
Design Patterns/행동(Behavioral)

[Visitor]방문자 패턴

Visitor Pattern 기존 코드를 변경하지 않고 새로운 기능을 추가하는 방법 더블 디스패치(Double dispatch)를 활용할 수 있다. BEFORE - 예시를 위해 지저분하지만 아래와 같이 코드를 써본다. Client public class Client { public static void main(String[] args) { Shape rectangle = new Rectangle(); Device device = new Phone(); rectangle.printTo(device); } } Shape - Rectangle, Triangle public interface Shape { void printTo(Device device); } public class Rectangle impl..

Design Patterns/행동(Behavioral)

[Template method]템플릿메소드패턴

Template Method Pattern 알고리즘 구조를 서브 클래스가 확장할 수 있도록 템플릿으로 제공하는 방법 추상 클래스는 템플릿을 제공하고 하위 클래스는 구체적인 알고리즘을 제공한다. - 콜백으로 상속 대신 위임을 사용하는 템플릿 패턴 - 상속 대신 익명 내부 클래스 또는 람다 표현식을 사용할 수 있다. BEFORE Client public class Client { public static void main(String[] args) { FileProcessor fileProcessor = new FileProcessor("number.txt"); int result = fileProcessor.process(); System.out.println(result); } } FileProcesso..

Design Patterns/행동(Behavioral)

[Strategy]전략패턴

Strategy Pattern 여러 알고리즘을 캡슐화하고 상호교환 가능하게 만드는 패턴 컨텍스트에서 사용할 알고리즘을 클라이언트에서 선택한다. BEFORE Client public class Client { public static void main(String[] args) { BlueLightRedLight blueLightRedLight = new BlueLightRedLight(3); blueLightRedLight.blueLight(); blueLightRedLight.redLight(); } } BlueLightRedLight public class BlueLightRedLight { private int speed; public BlueLightRedLight(int speed) { this..

Design Patterns/행동(Behavioral)

[State]상태 패턴

State Pattern 객체 내부 상태 변경에 따라 객체의 행동이 달라지는 패턴 상태에 특화된 행동들을 분리해 낼 수 있으며, 새로운 행동을 추가하더라도 다른 행동에 영향을 주지 않는다. BEFORE Client public class Client { public static void main(String[] args) { Student student = new Student("whiteship"); OnlineCourse onlineCourse = new OnlineCourse(); Student keesun = new Student("keesun"); keesun.addPrivateCourse(onlineCourse); onlineCourse.addStudent(student); onlineCour..

Design Patterns/행동(Behavioral)

[Observer]옵저버패턴

Observer Pattern 다수의 객체가 특정 객체 상태 변화를 감지하고 알림을 받는 패턴 발행(publish)-구독(subscribe) 패턴을 구현할 수 있다. BEFORE Client public class Client { public static void main(String[] args) { ChatServer chatServer = new ChatServer(); User user1 = new User(chatServer); user1.sendMessage("디자인패턴", "이번엔 옵저버 패턴입니다."); user1.sendMessage("롤드컵2021", "LCK 화이팅!"); User user2 = new User(chatServer); System.out.println(user2.get..

Design Patterns/행동(Behavioral)

[Memento]메멘토패턴

Memento Pattern 캡슐화를 유지하면서 객체 내부 상태를 외부에 저장하는 방법 객체 상태를 외부에 저장했다가 해당 상태로 다시 복구할 수 있다. BEFORE Client public class Client { public static void main(String[] args) { Game game = new Game(); game.setRedTeamScore(10); game.setBlueTeamScore(20); int blueTeamScore = game.getBlueTeamScore(); int redTeamScore = game.getRedTeamScore(); Game restoredGame = new Game(); restoredGame.setBlueTeamScore(blueTeam..

emojiyeon
'Design Patterns' 카테고리의 글 목록