美文网首页
Strategy Design Pattern

Strategy Design Pattern

作者: zheting | 来源:发表于2018-03-22 10:45 被阅读7次
    package com.sheting.design.pattern.demo11;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:33
     */
    public interface PaymentStrategy {
    
        void pay(int amount);
    
    }
    
    
    package com.sheting.design.pattern.demo11;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:34
     */
    public class CreditCardStrategy implements PaymentStrategy {
    
        private String name;
        private String cardNumber;
        private String cvv;
        private String dateOfExpiry;
    
        public CreditCardStrategy(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 with credit/debit card");
        }
    
    }
    
    
    package com.sheting.design.pattern.demo11;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:35
     *
     * Paypal 类似支付宝
     */
    public class PaypalStrategy implements PaymentStrategy {
    
        private String emailId;
        private String password;
    
        public PaypalStrategy(String email, String pwd) {
            this.emailId = email;
            this.password = pwd;
        }
    
        @Override
        public void pay(int amount) {
            System.out.println(amount + " paid using Paypal.");
        }
    
    }
    
    
    package com.sheting.design.pattern.demo11;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:35
     */
    public class Item {
    
        private String upcCode; //商品统一代码
        private int price;
    
        public Item(String upc, int cost) {
            this.upcCode = upc;
            this.price = cost;
        }
    
        public String getUpcCode() {
            return upcCode;
        }
    
        public int getPrice() {
            return price;
        }
    }
    
    
    package com.sheting.design.pattern.demo11;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:35
     */
    public class ShoppingCart {
    
        //List of items
        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 paymentMethod) {
            int amount = calculateTotal();
            paymentMethod.pay(amount);
        }
    
    }
    
    
    package com.sheting.design.pattern.demo11;
    
    /**
     * @Author SheTing
     * @Time 2018/3/22 10:36
     */
    public class ShoppingCartTest {
    
        public static void main(String[] args) {
            ShoppingCart cart = new ShoppingCart();
    
            Item item1 = new Item("1234", 10);
            Item item2 = new Item("5678", 40);
    
            cart.addItem(item1);
            cart.addItem(item2);
    
            //pay by paypal
            cart.pay(new PaypalStrategy("myemail@example.com", "mypwd"));
    
            //pay by credit card
            cart.pay(new CreditCardStrategy("Pankaj Kumar", "1234567890123456", "786", "12/15"));
    
        }
    
    }
    
    
    JDK中的使用

    One of the best example of strategy pattern is Collections.sort() method that takes Comparator parameter. Based on the different implementations of Comparator interfaces, the Objects are getting sorted in different ways.

    相关文章

      网友评论

          本文标题:Strategy Design Pattern

          本文链接:https://www.haomeiwen.com/subject/pwooqftx.html