[프로젝트 구성]
- 새 프로젝트 생성
- IntelliJ -> File -> New Project
- Gradle, Java
- Name 셋팅 완료
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
Gradle 업데이트
./gradlew wrapper --gradle-version 8.1.1
작업 폴더 구성
- src/main/java -> Project Structure에서 Sources 구성
- src/test/java -> Test 구성
일반적인 도메인 서비스 구현 예)
주식 거래 생성 : Order
거래 : Trade (매수/매도, 금액, 수량, 주식)
주식 : Stock (상장기관, 상장사)
import java.util.ArrayList;
import java.util.List;
public class Order {
private String customer;
private List<Trade> trades = new ArrayList<>();
public void addTrade(Trade trade) {
trades.add(trade);
}
public String getCustomer() {
return customer;
}
public void setCustomer(final String customer) {
this.customer = customer;
}
public double getValue() {
return trades.stream().mapToDouble(Trade::getValue).sum();
}
}
public class Trade {
public enum Type {BUY, SELL}
private Type type;
private Stock stock;
private int quantity;
private double price;
public Type getType() {
return type;
}
public void setType(final Type type) {
this.type = type;
}
public Stock getStock() {
return stock;
}
public void setStock(final Stock stock) {
this.stock = stock;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(final int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(final double price) {
this.price = price;
}
public double getValue() {
return quantity * price;
}
}
public class Stock {
private String symbol;
private String market;
public String getSymbol() {
return symbol;
}
public void setSymbol(final String symbol) {
this.symbol = symbol;
}
public String getMarket() {
return market;
}
public void setMarket(final String market) {
this.market = market;
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TradeStockTest {
@Test
void 도메인객체를이용한주식거래() {
Order order = new Order();
order.setCustomer("BigBank");
Stock stock1 = new Stock();
stock1.setSymbol("IBM");
stock1.setMarket("NYSE");
Trade trade1 = new Trade();
trade1.setType(Trade.Type.BUY);
trade1.setStock(stock1);
trade1.setPrice(125.00);
trade1.setQuantity(80);
order.addTrade(trade1);
Stock stock2 = new Stock();
stock2.setSymbol("GOOGLE");
stock2.setMarket("NASDAQ");
Trade trade2 = new Trade();
trade2.setType(Trade.Type.BUY);
trade2.setStock(stock2);
trade2.setPrice(375.00);
trade2.setQuantity(50);
order.addTrade(trade2);
Assertions.assertEquals(28750.0, order.getValue());
}
}
#DSL 구현 실습
Live Step by Step 따라하기...
NestedFunction DSL Test case
package trading.dsl.nestedfunction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import trading.domain.Order;
public class NestedFunctionOrderBuilderTest {
// @Test
// void builderTest1() {
// Order order = order("Customer"
// , buy(90, 100.0, stock("Apple","NASDAQ"))
// , sell(50, 100.0, stock("GOOGLE","KOSPI"))
// );
//
// Assertions.assertEquals("Customer", order.getCustomer());
//
// Assertions.assertEquals(9000.0, order.getValue());
// }
}
ChaningMethod DSL Test case
public class ChainingMethodOrderBuilderTest {
@Test
void builderTest() {
ChainingMethodOrderBuilder builder = ChainingMethodOrderBuilder.customer("Customer");
Assertions.assertEquals("Customer", builder.getCustomer());
}
@Test
void builderTestByTrade() {
TradeBuilder builder = ChainingMethodOrderBuilder.customer("Customer")
.buy(100);
Assertions.assertNotNull(builder);
}
@Test
void builderTestByStockBuilder() {
StockBuilder builder = ChainingMethodOrderBuilder.customer("Customer")
.buy(100)
.price(99.00)
.stock("Apple")
;
Assertions.assertNotNull(builder);
}
@Test
void builderTestByMarket() {
ChainingMethodOrderBuilder builder = ChainingMethodOrderBuilder.customer("Customer")
.buy(100)
.price(99.00)
.stock("Apple")
.market("NASDAQ")
;
Assertions.assertNotNull(builder);
}
@Test
void builderTestForOrder() {
Order order = ChainingMethodOrderBuilder.customer("Customer")
.buy(100)
.price(99.00)
.stock("Apple")
.market("NASDAQ")
.end();
Assertions.assertEquals(9900.0, order.getValue());
}
@Test
void builderTestForMultifulTrades() {
Order order = ChainingMethodOrderBuilder.customer("Justin")
.buy(100)
.price(100.0)
.stock("IBM")
.market("NASDAQ")
.sell(50)
.price(100.0)
.stock("APPLE")
.market("NASDAQ")
.end();
Assertions.assertEquals(14900.0, order.getValue());
}
}
'프로그래밍 > OOP_Pattern_TDD' 카테고리의 다른 글
Mocks Aren't Stubs - Martin Fowler 02 Jan 2007 (0) | 2024.04.01 |
---|---|
PROJECT REACTOR 소개 (0) | 2024.03.29 |
Java로 DSL(Domain Specific Language) 개발 (0) | 2023.08.09 |
ThreadPool로 초당 API 호출 건수 제한 - ExecutorService (0) | 2023.08.02 |
Flowable.crete 구조 파악 (0) | 2023.07.17 |