본문 바로가기

강의/Design pattern

(21)
방문자 (Visitior) 패턴 Visitor Pattern 기존 코드를 건드리지 않고 새로운 패턴을 추가. Code Example Triangle public class Triangle implements Shape { @Override public void printTo(Device device) { if(Device instanceof Phone) { //log } else if(device instanceof Watch) { //log } } } Rectangle public class Rectangle implements Shape { @Override public void printTo(Device device) { if(Device instanceof Phone) { //log } else if(device instance..
템플릿 메소드 (Template method) 패턴 Template Method Pattern 알고리즘의 구조를 템플릿으로 제공하는 방법. 각각의 내부 구현을 서브 클래스가 구현하도록 한다. 상속을 주로 사용한다. Code Example Client public class Client { public static void amin(String[] args) { FileProcessor fileProcessor = new FileProcessor("number.txt"); int reuslt = fileProcessor.process(); } } FileProcessor public class fileProcessor { private String path; public fileProcessor(String path) { this.path = path; }..
전략(Strategy) 패턴 Strategy Pattern 여러 알고리즘을 개별 클래스로 추상화하고 공통 인터페이스로 로직을 수행하도록 코드를 바꾸지 않고 알고리즘을 변경하는 방법 Comparator가 대표적이다. 여러 알고리즘을 캡슐화하고 상호 교환 가능하게 하는 패턴. Context에서 사용할 알고리즘을 클라이언트가 선택한다. Code Example BlueLightRedLight public class BluLightRedLight { private int speed; public BlueLightRedLight(int speed) { this.speed = speed; } public void blueLight() { if(speed == 1) { //log } else if(speed = 2) { //log } } publ..
상태(State) 패턴 State Pattern 상태에 따라 행동이 달라지는 객체를 위한 패턴. 상태에 특화된 행동들을 분리할 수 있고, 새로운 행동을 추가하더라도 다른 행동에 영향을 주지 않음. Code Example Client public class Client { public static void main(String[] args) { Student student = new Student("student"); OnlineCourse onlineCourse = new OnlineCourse(); Student s = new Student("s"); s.addPrivateCourse(onlineCourse); onlineCourse.addStudent(student); onlineCourse.changeState(Onlin..
관찰자(Observer) 패턴 Observer pattern 상태의 변경을 감지하고, 지켜보는 패턴. 여러 개의 객체들이 한개의 상태변화를 감지하고 반응할 때 적용한다. pubslish - subscribe 를 구현하기에 적합하다. Code Example Client public class Client { public static void main(String[] args) { ChatServer chatServer = new ChatServer(); User user1 = new User(chatServer); user1.sendMessage("design pattern", "observer"); user1.sendMessage("2022", "test"); User user2 = nwe User(chatServer); user1...
중재자(Mediator) 패턴 Mediator Pattern 여러 객체들 간의 의사소통을 하는 역할을 추상화시키는 패턴 직접 의사소통을 하고 있는 컴포넌트들은 결합도가 높아 코드를 수정하거나 테스트 하기 어렵다. Code Example Guest public class Guest { private Restaurant restaurant = new Restaurant(); private CleanService cleanService = new CleanService(); public void dinner() { restaurant.dinner(this); } public void getTower(int numberOfTower) { cleaningService.getTower(this, numberOfTower); } } Hotel p..
이터레이터(Iterator) 패턴 Iterator Pattern 집합 객체를 순회하는 패턴. 내부 구현을 드러내지 않고 클라이언트가 내부의 요소 아이템을 순회한다. 자바의 Iterator도 인터페이스 중 하나다. Code Example Iterator를 쓰면 객체가 List라는 것을 알게 되는 단점이 있다. Client public class Client { public static void main(String[] args) { Board board = new Board(); board.addPost("111"); board.addPost("222"); board.addPost("333"); // TODO 들어간 순서대로 순회하기 List posts = board.getPosts(); for(int i = 0; i < posts.siz..
인터프리터(Interpreter) 패턴 Interpreter 통역 : 원본이 되는 것을 다른 형태로 변경함. 자주 사용하는 문제, 문법 등을 정의하고 이 문법에 맞춰 대입하여 문제를 해결한다. 정규 표현식 등이 해당된다. Code Example PostfixNotation public class PostfixNotation { private final String expression; public PostfixNotation (String expression) { this.expression = expression; } public static void main(String[] args) { PostfixNotation postfixNotation = new PostfixNotation("123+-"); postfixNotation.calcu..

LIST