【问题标题】:Strategy design pattern Example?策略设计模式示例?
【发布时间】:2021-03-21 15:43:57
【问题描述】:

以下是来自here 的策略设计模式示例。 首先,我们将为我们的策略创建接口,在我们的例子中,支付作为参数传递的金额。

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

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");
    }
 
}



 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.");
        }
     
    }


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;
    }
 
}

    public class ShoppingCart {
     
   

 //List of items
    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 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"));
    }
 
}

我想问这里策略模式有什么用?一旦我们在main中创建了策略。我们现在可以访问Strategy类。我们可以直接从那里调用pay()方法?为什么我们需要接口,所有哪个是调用方法?

【问题讨论】:

    标签: java oop design-patterns strategy-pattern


    【解决方案1】:
    1.我想问这里策略模式有什么用?

    答案是拥有 ShoppingCart (ShoppingCart cart = new ShoppingCart();) 的用户

    2.我们可以直接从那里调用pay()方法吗?

    我不知道你的意思

    3. 为什么我们需要接口,它所做的只是调用一个方法?

    我们需要PaymentStrategy接口,因为我们需要使用Polymorphism来实现多种支付方式(paypal或信用卡),让我们稍微改变一下来源,你会看得更清楚:

    public class ShoppingCart {
        // other functions
    
        public void pay(PaymentStrategy paymentMethod, int amount){
            paymentMethod.pay(amount);
        }
    
        public void payWithStrategy() {
            int amount = calculateTotal();
            if (amount > 10000) { // because your credit card limit is 10000$ so you must use paypal
                pay(new PaypalStrategy("myemail@example.com", "mypwd"), amount);
            }
            else { // you really like credit card so when the money lower than the card limit, you always choose it
                pay(new CreditCardStrategy("Pankaj Kumar", "1234567890123456", "786", "12/15"), amount);
            }
        }
    }
    
    
    
    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);
    
            cart.payWithStrategy();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多