자바의 전략 패턴

    자바

    코드 위치: ch1/7.java

    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.List;
    interface PaymentStrategy { 
        public void pay(int amount);
    }
    
    class KAKAOCardStrategy implements PaymentStrategy {
        private String name;
        private String cardNumber;
        private String cvv;
        private String dateOfExpiry;
        
        public KAKAOCardStrategy(String nm, String ccNum, String cvv, String expiryDate) {
            this.name = nm;
            this.cardNumber = ccNum;
            this.cvv = cvv;
            this.dateOfExpiry = expiryDate;
        }
    
        @Override
        public void pay(int amount) {
            System.out.println(amount + " paid using KAKAOCard.");
        }
    } 
    
    class LUNACardStrategy implements PaymentStrategy {
        private String emailId;
        private String password;
        
        public LUNACardStrategy(String email, String pwd) {
            this.emailId = email;
            this.password = pwd;
        }
      
        @Override
        public void pay(int amount) {
            System.out.println(amount + " paid using LUNACard.");
        }
    }
    
    class Item { 
        private String name;
        private int price; 
        public Item(String name, int cost) {
            this.name = name;
            this.price = cost;
        }
    
        public String getName() {
            return name;
        }
    
        public int getPrice() {
            return price;
        }
    } 
    
    class ShoppingCart { 
        List<Item> items;
        
        public ShoppingCart() {
            this.items = new ArrayList<Item>();
        }
        
        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 paymentMethod) {
            int amount = calculateTotal();
            paymentMethod.pay(amount);
        }
    }  
    
    public class HelloWorld {
        public static void main(String[] args) {
            ShoppingCart cart = new ShoppingCart();
            
            Item A = new Item("kundolA", 100);
            Item B = new Item("kundolB", 300);
            
            cart.addItem(A);
            cart.addItem(B);
            
            // pay by LUNACard
            cart.pay(new LUNACardStrategy("kundol@example.com","pukubababo"));
            
            // pay by KAKAOCard
            cart.pay(new KAKAOCardStrategy("Ju hongchul", "123456789","123","12/01"));
        }
    }
    /*
    400 paid using LUNACard.
    400 paid using KAKAOCard.
    */

    앞의 코드는 쇼핑 카트에 아이템을 담아 LUNACard 또는 KAKAOCard라는 두 개의 전략으로 결제하는 코드입니다.

     

    용어

    ––– 컨텍스트

    프로그래밍에서의 컨텍스트는 상황, 맥락, 문맥을 의미하며 개발자가 어떠한 작업을 완료하는 데 필요한 모든 관련 정보를 말한다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.