zoukankan      html  css  js  c++  java
  • strategy模式

    讲的很清楚http://www.cnblogs.com/graphicsme/archive/2011/12/03/2274918.html

     1 #include <iostream>
     2 #include <string>
     3 #include <vector>
     4 #include <queue>
     5 #include <set>
     6 #include <algorithm>
     7 #include <map>
     8 #include <stack>
     9 using namespace std;
    10 
    11 class SaleStrategy {
    12 public:
    13     virtual float calculate(float price, int amount) = 0;
    14 };
    15 
    16 class RegularSaleStrategy: public SaleStrategy {
    17 public:
    18     virtual float calculate(float price, int amount) {
    19         return price * amount;
    20     }
    21 };
    22 
    23 class StudentSaleStrategy: public SaleStrategy {
    24 public:
    25     virtual float calculate(float price, int amount) {
    26         const float DISCOUNT = 0.5f; //学生半价
    27 
    28         return (1 - DISCOUNT) * price * amount;
    29     }
    30 };
    31 
    32 class BulkSaleStrategy: public SaleStrategy {
    33 public:
    34     virtual float calculate(float price, int amount) {
    35         const float DISCOUNT = 0.3f; //批发折扣
    36         const int BULK_LIMIT = 100; //>=100件按批发价
    37 
    38         if (amount >= BULK_LIMIT)
    39             return (1 - DISCOUNT) * price * amount;
    40         return price * amount;
    41     }
    42 };
    43 
    44 class SaleManager {
    45 public:
    46     SaleManager(SaleStrategy* pSaleStrategy = NULL) :
    47             m_pSaleStrategy(pSaleStrategy) {
    48     }
    49 
    50 public:
    51     void setStrategy(SaleStrategy* pSaleStrategy) {
    52         m_pSaleStrategy = pSaleStrategy;
    53     }
    54 
    55     float calculateTotal(float price, int amount) {
    56         float calResult = m_pSaleStrategy->calculate(price, amount);
    57         cout << "total price is: " << calResult << endl;
    58         return calResult;
    59     }
    60 
    61 private:
    62     SaleStrategy* m_pSaleStrategy;
    63 };
    64 int main(int argc, char** argv) {
    65     SaleStrategy* pSaleStrategy = new RegularSaleStrategy();
    66     SaleStrategy* pStudentSaleStrategy = new StudentSaleStrategy();
    67     SaleStrategy* pBulkSaleStrategy = new BulkSaleStrategy();
    68 
    69     SaleManager saleManager(pSaleStrategy);
    70     cout << "Regular Sale Strategy:\n";
    71     saleManager.calculateTotal(2.5f, 100);
    72 
    73     saleManager.setStrategy(pStudentSaleStrategy);
    74     cout << "Student Sale Strategy:\n";
    75     saleManager.calculateTotal(2.5f, 100);
    76 
    77     saleManager.setStrategy(pBulkSaleStrategy);
    78     cout << "Bulk Sale Strategy:\n";
    79     saleManager.calculateTotal(2.5f, 100);
    80 
    81     delete pBulkSaleStrategy;
    82     delete pStudentSaleStrategy;
    83     delete pSaleStrategy;
    84 
    85     return 0;
    86 }
  • 相关阅读:
    python 3字符编码
    python 数据类型 datatype
    部署python django程序
    linux centos7安装python3
    关于mysql数据库优化
    关于bottle WEB框架中签名cookie的一点理解
    1111
    bottle框架剖析
    the difference __str__ and __repr__
    property getitem setitem
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2610701.html
Copyright © 2011-2022 走看看