본문 바로가기

Programming/Java, Spring98

자바의 method reference method reference - 기존에 이미 선언되어 있는 메서드를 지정하고 싶을 때 - :: 오퍼레이터 사용 - ClassName::staticMethodName : 객체의 static method를 지정할 때 - ClassName::instanceMethodName : 객체의 instance method를 지정할 때 - ClassName::new : 객체의 constructor를 지정할 때 - objectName::instanceMethodName : 선언된 객체의 instance method를 지정할 때 ClassName::staticMethodName Function strToInt = Integer::parseInt; int five = strToInt.apply("5"); objectName.. 2023. 6. 3.
자바의 여러가지 functional interface + comparator 1. Supplier @FunctionalInterface public interface Supplier { T get(); } 사용 예시 public static void main(String[] args) { Supplier myStringSupplier = () -> {return "hello world!";}; myStringSupplier.get(); Supplier myRandomDoubleSupplier = () -> Math.random(); System.out.println("myRandomDoubleSupplier.get() = " + myRandomDoubleSupplier.get()); printRandomDoubles(myRandomDoubleSupplier, 5); } publi.. 2023. 6. 3.
자바의 Functional Interface와 andThen 메서드 이해하기 1. Function interface @FunctionalInterface public interface Function { R apply(T t); } input 타입은 T 이고 return 타입은 R인 메서드 이것을 Object 형태로 구현하면 어떻게 될까? 아래처럼 쓸 수 있다. public class Adder implements Function { @Override public Integer apply(Integer integer) { return integer + 10; } } public static void main(String[] args) { Function adder = new Adder(); Integer apply = adder.apply(5); System.out.println.. 2023. 6. 2.
자바와 함수형 프로그래밍 함수형 프로그래밍과 자바의 객체지향 프로그래밍은 문제에 대한 접근 방식이 다르다. 1. 함수형 프로그래밍 ? 함수란 ? f(x) => input을 입력할 때 output을 반환하는 것 => 자바에서는 method 함수는 역할을 고려할 때 동사에 가깝다. => method 명명을 동사로 짓는다. 반대로 객체는 명사 형태로 짓는다. 동사의 형태로 추상화하는 것이 쉬울 때가 있다. 명령형 프로그래밍 Imperative Programming 선언형 프로그래밍 Declarative Programming OOP 객체 지향 프로그래밍 How to do? 어떻게 해야 할까? Functional Programming What to do? 무엇을 해야 할까? 예를 들어, 유저 리스트가 주어졌을 때 검증되지 않은 유저들의 .. 2023. 6. 2.
[Spring Cloud] Kafka 를 사용한 마이크로서비스 간 데이터 전송, 단일 데이터베이스를 사용해 동기화 문제를 해결하기 Order MicroService -> Catalog MicroService - orders service에 요청된 주문의 수량 정보를 catlogs service에 반영 - orders service에서 kafka topic으로 메시지 전송 (producer) - catalogs service에서 kafka topic에 전송된 메시지 취득 (consumer) db - order service -> message Queing Service (Kafka) 데이터 동기화 문제 해결 - OrderService에 요청된 주문 정보를 DB가 아니라 Kafka Topic으로 전송 - Kafka Topic에 설정된 Kafka Sink Connect를 사용해 단일 Db에 저장 -> 데이터 동기화 즉, 데이터베이스 입장.. 2023. 6. 2.