[Java] Decorator Pattern
·
JVM/Java
요구사항FileRepository 인터페이스는 파일 스토리지로부터 파일키를 입력받아 파일 InputStream을 반환한다./** * Get the file contents from a file storage (e.g. LocalFile System, S3, GCS, Azure Blob Storage, etc.) */public interface FileRepository { InputStream getFile(FileKey fileKey) throws IOException;}LocalFile 은 LocalFileRepository,AWS S3 File 은 S3FileRepository 클래스로 파일을 가져와 InputStream 을 반환한다.기존 기능에 다음과 같은 스팩이 추가되어야 한다.".zip, ..
[Java] JUnit Inner class 를 정의하는 팁
·
JVM/Java
PurposeInner class 로 static class 로 선언하는 방식과 class 로 선언하는 방식의 차이를 안다.static inner classclass Car { public static final Duration fixDuration; private final String name; static class Painting { private final String color; public void paint(){ }; }}ProsOutter 인스턴스 생성없이 바로 인스턴스 생성이 가능하다.Painting painting = new Painting("RED");painting.paint();ConsOutter 인스턴스의 내부 변수에 접근 불가능하다.오로지 static ..
[Java] Primitive 타입보다 VO 를 의존하라
·
JVM/Java
Kafka Record 에 들어갈 Record Key, Record Value 를 OffsetRecord 인터페이스로 추상화했다.OffsetRecord 는 오로지 오프셋을 저장하는 토픽에 쓰인다.예를 들면 이런식이다.Record KeyC:\\Users\M_Faclon\path\a.txtRecord value30/** * Domain interface used by OffsetManager and SourceConnector Producer. * Consists of object unique identifier and offset. * Stored in the Offset topic partition. */public interface OffsetRecord { /** * The unique ke..
[Java] Clean Code - index, array 대신 Iterator
·
JVM/Java
class Args { private String[] args; int currentIndex;}args 를 하나씩 파싱하는 메소드가 필요하다고 해보자,인자를 넘길때마다 이런 형식이 된다.Object parseArgument(String[] args, int index);인자 수는 적으면 적을수록 좋다. 인자가 많을수록 복잡하다. - 클린코드Args 클래스를 Iterator 로 리팩터링해보자.class Args { List args; Iterator currentElement;}이제 args 속 원소를 하나씩 넘길 수 있다.그저 currentElement 만 넘겨주면 된다.Ob..
[Java] Closeable vs AutoCloseable
·
JVM/Java
개요앞에 Auto 라는 접두사 차이가 있다.언제 무슨 인터페이스를 쓰는게 적절한지 선택할 수 있도록 차이점을 알아본다.Closeablepublic interface Closeable extends AutoCloseable { public void close() throws IOException;}스트림을 닫고 리소스 사용을 중단하는 행위를 정의한다.이 인터페이스를 구현하는 클래스에는FileInputStream, FileOutputStream 등이 있다.IOExceptionIO 동작이 실패하거니 인터럽트가 발생함을 알리는 예외FileNotFoundException, UnsupportedEncodingException 등이 있다.IdempotentClose 는 반드시 멱등성을 보장해야한다.즉, 여러번 ..
[Java] 제네릭과 동적 타입 캐스팅
·
JVM/Java
개요Java 의 Json 데이터를 다루는 Jackson 라이브러리에서는런타임에 동적인 형 변환을 지원하고자 다음과 같은 메소드를 지원한다.public class ObjectMapper { public T convertValue(Object fromValue, Class toValueType) throws IllegalArgumentException { return (T) _convert(fromValue, _typeFactory.constructType(toValueType)); } public T convertValue(Object fromValue, TypeReference toValueTypeRef) throws IllegalArgume..