'디자인패턴' 태그의 글 목록 (4 Page)
본문 바로가기
728x90
반응형

디자인패턴70

2022-05-12-인터프리터패턴-2부-패턴적용하기 01.소스코드 01.1 App import java.util.Map; public class App { public static void main(String[] args) { PostfixExpression expression = PostfixParser.parse("xyz+-a+"); int result = expression.interpret(Map.of('x', 1, 'y', 2, 'z', 3, 'a', 4)); System.out.println(result); } } 01.2 PostfixExpression import java.util.Map; public interface PostfixExpression { int interpret(Map context); } 01.3 VariableExpr.. 2022. 5. 13.
2022-05-12-인터프리터패턴-1부-패턴소개 01.인터프리터패턴 정규 표현식 같은 것도 일종의 인터프리터 문서에서 어떤 문자를 찾는것 같은 , 키워드, 특정단어 ? 이런것 import java.util.Stack; 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.calculate(); } private void calculate() { .. 2022. 5. 12.
2022-05-11-커맨드패턴-3부-장점과단점 01.장점 기존의 코드 변경 없이 새로운 커맨드 만들 수 있음 변경에는 닫혀있고 확장에는 열려있음 수신자의 코드 즉, 게임이나 라이트 코드 바껴도 버튼이나 마이앱같은 요청하는 코드가 변경되지 않음 각각의 커맨드 자기 할일만 함 단일 책임원칙을 준수 커맨드 객체를 로깅, DB에 저장, 네트워크로 전송 하는 등 다양한 방법으로 활용할 수도 있음 01.1 undo 기능이 있다면? before public interface Command{ void execute(); } after public interface Command{ void execute(); void undo(); } before public class GameEndCommand implements Command{ private Game game;.. 2022. 5. 12.
2022-05-11-커맨드패턴-2부-패턴적용하기 01.커맨드 패턴 적용하기 01.1 Command public interface Command{ void execute(); } 01.2 Button public class Button{ private Command command; pulic Button(command command){ this.command = command; } public void press(){ command,execute(); } public static void main(String[] args){ Button button = new Button(new Command(){ @Override public void execute(){ } }); button.press(); button.press(); } } 02.Concreate.. 2022. 5. 12.
728x90
반응형