๐๏ธ Java Spring Boot์์ If-Else๋ฌธ ์ค์ด๊ธฐ
If-else ๋ฌธ์ ํํ์ง๋ง ๋จ์ฉ๋๋ฉด ๋ณต์กํ๊ณ ์ ์ง๋ณด์๊ฐ ์ด๋ ค์ด ์ฝ๋๋ก ์ด์ด์ง ์ ์์ต๋๋ค. ์ด ๊ธ์์๋ Java Spring Boot ํ๋ก์ ํธ์์ if-else ๋ฌธ์ ์ค์ด๊ธฐ ์ํ ๋ค์ํ ์ ๋ต์ ํ๊ตฌํ์ฌ ์ฝ๋๋ฅผ ๋ ๋ชจ๋ํํ๊ณ , ์ ์ง๋ณด์ํ๊ธฐ ์ฝ๊ณ , ์ฝ๊ธฐ ์ฝ๊ฒ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ํด ์ค๋ช ํ๊ฒ ์ต๋๋ค. ย If-Else ๋ฌธ์ ์ค์ด๋ ์ ๋ต * ์ ๋ต ํจํด(Strategy Pattern) * Enum ์ฌ์ฉ * ๋คํ์ฑ(Polymorphism) * ๋๋ค ํํ์๊ณผ ํจ์ํ ์ธํฐํ์ด์ค * ๋ช ๋ น ํจํด(Command Pattern) * ๊ฐ๋ ์ (Guard Clauses) 1. ์ ๋ต ํจํด ์ ๋ต ํจํด์ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์กฑ์ ์ ์ํ๊ณ , ๊ฐ๊ฐ์ ์บก์ํํ๋ฉฐ, ์ด๋ค์ ์ํธ ๊ตํ ๊ฐ๋ฅํ๊ฒ ๋ง๋ญ๋๋ค. ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ํน์ ์์ ์ ์ํํด์ผ ํ ๋ ์ ์ฉํฉ๋๋ค. ย ์์ : ๊ฒฐ์ ์ฒ๋ฆฌ ์์คํ ๋จผ์ PaymentStrategy ์ธํฐํ์ด์ค๋ฅผ ์ ์ํฉ๋๋ค: public interface PaymentStrategy { void pay(double amount); } ๊ทธ๋ฐ ๋ค์, ๋ค๋ฅธ ๊ฒฐ์ ์ ๋ต์ ๊ตฌํํฉ๋๋ค: @Component public class CreditCardPayment implements PaymentStrategy { @Override public void pay(double amount) { // ์ ์ฉ ์นด๋ ๊ฒฐ์ ์ฒ๋ฆฌ ๋ก์ง System.out.println("Paid " + amount + " using Credit Card."); } } @Component public class PaypalPayment implements PaymentStrategy { @Override public void pay(double amount) { // PayPal ๊ฒฐ์ ์ฒ๋ฆฌ ๋ก์ง System.out.println("Paid " + amount + " using PayPal."); } } ย 2. Enum ์ฌ์ฉ Enums๋ ๋ฏธ๋ฆฌ ์ ์๋ ์์ ์งํฉ๊ณผ ๊ทธ์ ๊ด๋ จ๋ ๋์์ ๋ํ๋ด๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ย ์์ : ์ฃผ๋ฌธ ์ํ ๊ด๋ฆฌ ๋ค์ํ ๋์์ ๊ฐ์ง OrderStatus enum์ ์ ์ํฉ๋๋ค: public enum OrderStatus { NEW { @Override public void handle() { System.out.println("Processing new order."); } }, SHIPPED { @Override public void handle() { System.out.println("Order shipped."); } }, DELIVERED { @Override public void handle() { System.out.println("Order delivered."); } }; public abstract void handle(); } ย 3. ๋คํ์ฑ (Polymorphism) ๋คํ์ฑ์ ๊ฐ์ฒด๋ฅผ ์ค์ ํด๋์ค๊ฐ ์๋ ๋ถ๋ชจ ํด๋์ค์ ์ธ์คํด์ค๋ก ์ทจ๊ธํ ์ ์๊ฒ ํด์ค๋๋ค. ์ด๋ฅผ ํตํด ๋ถ๋ชจ ํด๋์ค์ ์ฐธ์กฐ๋ฅผ ํตํด ํ์ ํด๋์ค์ ์ฌ์ ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ย ์์ : ์๋ฆผ ์์คํ Notification ์ธํฐํ์ด์ค์ ๊ทธ ๊ตฌํ์ ์ ์ํฉ๋๋ค: public interface Notification { void send(String message); } public class EmailNotification implements Notification { @Override public void send(String message) { // ์ด๋ฉ์ผ ์ ์ก ๋ก์ง System.out.println("Sending email: " + message); } } public class SmsNotification implements Notification { @Override public void send(String message) { // SMS ์ ์ก ๋ก์ง System.out.println("Sending SMS: " + message); } } ย 4. ๋๋ค ํํ์๊ณผ ํจ์ํ ์ธํฐํ์ด์ค ๋๋ค ํํ์์ ํนํ ๋จ์ผ ๋ฉ์๋ ์ธํฐํ์ด์ค๋ฅผ ๋ค๋ฃฐ ๋ ์ฝ๋๋ฅผ ๊ฐ์ํํ ์ ์์ต๋๋ค. ย ์์ : ํ ์ธ ์๋น์ค ๋๋ค ํํ์์ ์ฌ์ฉํ๋ ํ ์ธ ์๋น์ค๋ฅผ ์ ์ํฉ๋๋ค: import java.util.HashMap; import java.util.Map; import java.util.function.Function; public class DiscountService { private Map> discountStrategies = new HashMap(); public DiscountService() { discountStrategies.put("SUMMER_SALE", price -> price * 0.9); discountStrategies.put("WINTER_SALE", price -> price * 0.8); } public double applyDiscount(String discountCode, double price) { return discountStrategies.getOrDefault(discountCode, Function.identity()).apply(price); } } ย 5. ๋ช ๋ น ํจํด (Command Pattern) ๋ช ๋ น ํจํด์ ์์ฒญ์ ๊ฐ์ฒด๋ก ์บก์ํํ์ฌ ํด๋ผ์ด์ธํธ๋ฅผ ํ, ์์ฒญ, ์์ ์ผ๋ก ๋งค๊ฐ๋ณ์ํํ ์ ์๊ฒ ํฉ๋๋ค. ย ์์ : ํ์ผ ์์ Command ์ธํฐํ์ด์ค์ ๊ตฌ์ฒด์ ์ธ ๋ช ๋ น์ ์ ์ํฉ๋๋ค: public interface Command { void execute(); } public class OpenFileCommand implements Command { private FileSystemReceiver fileSystem; public OpenFileCommand(FileSystemReceiver fs) { this.fileSystem = fs; } @Override public void execute() { this.fileSystem.openFile(); } } public class CloseFileCommand implements Command { private FileSystemReceiver fileSystem; public CloseFileCommand(FileSystemReceiver fs) { this.fileSystem = fs; } @Override public void execute() { this.fileSystem.closeFile(); } } ย 6. ๊ฐ๋ ์ (Guard Clauses) ๊ฐ๋ ์ ์ ์กฐ๊ฑด์ ๋ฏธ๋ฆฌ ์ฒ๋ฆฌํ์ฌ ์ค์ฒฉ ๊ตฌ์กฐ๋ฅผ ์ค์ด๊ณ ์ฝ๋๋ฅผ ๋ ์ฝ๊ธฐ ์ฝ๊ฒ ๋ง๋ญ๋๋ค. ์์ : ์ฌ์ฉ์ ๊ฒ์ฆ ์ฌ์ฉ์ ์ ๋ ฅ์ ๊ฒ์ฆํ๊ธฐ ์ํด if-else ๋ฌธ์ ์ค์ฒฉํ๋ ๋์ , ๊ฐ๋ ์ ์ ์ฌ์ฉํ์ฌ ์๋ชป๋ ๊ฒฝ์ฐ๋ฅผ ๋ฏธ๋ฆฌ ์ฒ๋ฆฌํฉ๋๋ค: public class UserService { public void registerUser(User user) { if (user == null) { throw new IllegalArgumentException("User cannot be null"); } if (user.getName() == null || user.getName().isEmpty()) { throw new IllegalArgumentException("User name cannot be empty"); } if (user.getEmail() == null || user.getEmail().isEmpty()) { throw new IllegalArgumentException("User email cannot be empty"); } // ๋ฑ๋ก ์งํ System.out.println("Registering user: " + user.getName()); } } ย ๊ฒฐ๋ก ์ด๋ฌํ ์ ๋ต์ ์ ์ฉํจ์ผ๋ก์จ Java Spring Boot ํ๋ก์ ํธ์์ if-else ๋ฌธ์ ์ฌ์ฉ์ ํฌ๊ฒ ์ค์ผ ์ ์์ต๋๋ค. ์ด๋ ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ ์ ์ง๋ณด์์ฑ๊ณผ ํ์ฅ์ฑ์ ํฅ์์ํต๋๋ค. ์ด๋ฌํ ํจํด๊ณผ ๊ดํ์ ์ฑํํ์ฌ ๋ ๊นจ๋ํ๊ณ ํจ์จ์ ์ธ ์ฝ๋๋ฅผ ์์ฑํ์ธ์. ๋ฒ์ญ: [https://ducktopia.tistory.com/129] ์๋ฌธ: