Home > CS > 2024 > πŸ’Ύ [CS] μ „λž΅ νŒ¨ν„΄(Strategy pattern)

πŸ’Ύ [CS] μ „λž΅ νŒ¨ν„΄(Strategy pattern)
CS

πŸ’Ύ [CS] μ „λž΅ νŒ¨ν„΄(Strategy pattern)

1️⃣ μ „λž΅ νŒ¨ν„΄(Strategy pattern)

  • μ „λž΅ νŒ¨ν„΄(Strategy pattern) 은 μ •μ±… νŒ¨ν„΄(Policy pattern) 이라고도 ν•˜λ©°, κ°μ±„μ˜ ν–‰μœ„λ₯Ό λ°”κΎΈκ³  싢은 경우 β€˜μ§μ ‘β€™ μˆ˜μ •ν•˜μ§€ μ•Šκ³  μ „λž΅μ΄λΌκ³  λΆ€λ₯΄λŠ” β€˜μΊ‘μŠν™”ν•œ μ•Œκ³ λ¦¬μ¦˜β€™ 을 μ»¨ν…μŠ€νŠΈ μ•ˆμ—μ„œ λ°”κΏ”μ£Όλ©΄μ„œ μƒν˜Έ ꡐ체가 κ°€λŠ₯ν•˜κ²Œ λ§Œλ“œλŠ” νŒ¨ν„΄μž…λ‹ˆλ‹€.

  • μ•„λž˜μ˜ μ˜ˆμ‹œ μ½”λ“œλŠ” μš°λ¦¬κ°€ μ–΄λ–€ 것을 μ‚΄ λ•Œ λ„€μ΄λ²„νŽ˜μ΄, 카카였페이 λ“± λ‹€μ–‘ν•œ λ°©λ²•μœΌλ‘œ κ²°μ œν•˜λ“―μ΄ μ–΄λ–€ μ•„μ΄ν…œμ„ μ‚΄ λ•Œ LUNACard둜 μ‚¬λŠ” 것과 KAKAOCard둜 μ‚¬λŠ” 것을 κ΅¬ν˜„ν•œ μ˜ˆμ œμž…λ‹ˆλ‹€.
    • 결제 λ°©μ‹μ˜ β€˜μ „λž΅β€™ 만 λ°”κΏ”μ„œ 두 가지 λ°©μ‹μœΌλ‘œ κ²°μ œν•˜λŠ” 것을 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
// PaymentStrategy - interface

public interface PaymentStrategy {
	void pay(int amount);
}

// KAKAOCardStrategy

public class KAKAOCardStrategy implements PaymentStrategy{
	private String name;
	private String cardNumber;
	private String cvv;
	private String dateOfExpiry;

	public KAKAOCardStrategy(String name, String cardNumber, String cvv, String dateOfExpiry) {
		this.name = name;
		this.cardNumber = cardNumber;
		this.cvv = cvv;
		this.dateOfExpiry = dateOfExpiry;
	}

	@Override
	public void pay(int amount) {
		System.out.println(amount + " paid using KAKAOCard.");
	}
}

// LUNACardStrategy

public class LUNACardStrategy implements PaymentStrategy {
	private String emailId;
	private String password;

	public LUNACardStrategy(String emailId, String password) {
		this.emailId = emailId;
		this.password = password;
	}

	@Override
	public void pay(int amount) {
		System.out.println(amount + " paid using LUNACard");
	}
}

// Item

public class Item {
	private String name;
	private int price;
	public Item(String name, int price) {
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public int getPrice() {
		return price;
	}
}

// ShoppingCart

import java.util.ArrayList;
import java.util.List;

public class ShoppingCart {
	List<Item> items;

	public ShoppingCart() {
		this.items = new ArrayList<>();
	}

	public void addItem(Item item) {
		this.items.add(item);
	}

	public void removeItem(Item item) {
		this.items.remove(item);
	}

	public int calculateTotal() {
		int sum = 0;
		for (Item item : items) {
			sum += item.getPrice();
		}
		return sum;
	}

	public void pay(PaymentStrategy pamentMethod) {
		int amount = calculateTotal();
		pamentMethod.pay(amount);
	}
}

// Main

import designPattern.strategy.Item;
import designPattern.strategy.KAKAOCardStrategy;
import designPattern.strategy.LUNACardStrategy;
import designPattern.strategy.ShoppingCart;

public class Main {

	public static void main(String[] args) {
		ShoppingCart cart = new ShoppingCart();

		Item A = new Item("A", 100);
		Item B = new Item("B", 300);

		cart.addItem(A);
		cart.addItem(B);

		// pay by LUNACard
		cart.pay(new LUNACardStrategy("kobe@google.com", "1234"));

		// pay by KAKAOCard
		cart.pay(new KAKAOCardStrategy("Minseong Kang", "123456789", "123", "12/01"));
	}
}

μ‹€ν–‰ κ²°κ³Ό

400 paid using LUNACard
400 paid using KAKAOCard.
  • μœ„ μ½”λ“œλŠ” μ‡Όν•‘ μΉ΄λ“œμ— μ•„μ΄ν…œμ„ λ‹΄μ•„ LUNACard λ˜λŠ” KAKAOCard λΌλŠ” 두 개의 μ „λž΅μœΌλ‘œ κ²°μ œν•˜λŠ” μ½”λ“œμž…λ‹ˆλ‹€.

μš©μ–΄ : μ»¨ν…μŠ€νŠΈ
ν”„λ‘œκ·Έλž˜λ°μ—μ„œμ˜ μ»¨ν…μŠ€νŠΈλŠ” 상황, λ§₯락, λ¬Έλ§₯을 μ˜λ―Έν•˜λ©° κ°œλ°œμžκ°€ μ–΄λ– ν•œ μž‘μ—…μ„ μ™„λ£Œν•˜λŠ” 데 ν•„μš”ν•œ λͺ¨λ“  κ΄€λ ¨ 정보λ₯Ό λ§ν•©λ‹ˆλ‹€.