繁体   English   中英

无缘无故通过指针返回的责任链

[英]Chain of responsibility going back by pointers for no reason

所以我的任务是开一家商店 class,产品 class 一个篮子 class bla bla bla,我做了所有这些,经过测试,一切正常,与问题无关。 现在我要做的是创建折扣和一系列责任,比如我创建了 3 个折扣,它会通过指针检查哪个是合适的。 所以我做到了,它确实有效,当我调试时我检查过,它停止了它需要的折扣,返回给我应用折扣的价格但是它以某种方式通过指针返回并再次返回我没有的东西'不需要。 在我展示代码后,我会更清楚地说明这一点,并提供一个示例。 所以这是我的Discount.h ,它包含一个指向下一个 Discount 和主要虚拟countDiscount(); function 在这一切中扮演着重要角色。

#ifndef DISCOUNT_HEADER
#define DISCOUNT_HEADER

#include "Basket.h"

class Discount
{
public:
    Discount():next(NULL){}
    virtual int countDiscount(Basket b) = 0;
    void nextDiscount(Discount* nextDisc) { next = nextDisc; }
protected:
    Discount* next;
};

#endif

所以,然后我不得不做很多折扣类型,所以我从固定折扣开始,假设我有FixatedDiscount(100, 15) ,所以当这个人的购物篮内容价值超过 100 时,就会应用折扣并且它只是从他的总价值中减去 15。 因此,如果他以 110 的价格购物,则应用折扣,结果为 110 - 15= 95。这是FixatedDiscount.h

#ifndef FIXATED_HEADER
#define FIXATED_HEADER

#include "Discount.h"

class FixatedDiscount:public Discount
{
public:
    FixatedDiscount(int l, int d) : limit{ l }, discount{ d } {}
    int countDiscount(Basket b);
private:
    int limit;
    int discount;
};

#endif

这是FixatedDiscount.cpp

#include "FixatedDiscount.h"
#include <iostream>

int FixatedDiscount::countDiscount(Basket b) {
    int totalPrice = b.countPrice(); //this method just counts the value of the basket which for our example is 120
    if (totalPrice >= limit) {
        totalPrice -= discount;
        std::cout << "Handled when limit is " << limit << " and discount is: " << discount << " returning total price: " << totalPrice << std::endl;
    }
    else if (next != NULL) {
        next->countDiscount(b);
    }
    else {
        std::cout << "I am the last handler" << std::endl;
    }
    return totalPrice;
}

所以假设我有 3 个折扣, FixatedDiscount(150, 20); , FixatedDiscount(100, 15) , FixatedDiscount(50, 10) 在我的示例中,我将制作一个价值 120 的篮子,因此它应该跳过第一个折扣并继续进行第二个折扣,因为他购物超过 100 个并且应用了 15 的折扣但他没有达到 150 以获得折扣20.

#include <iostream>
#include "Product.h"
#include "Basket.h"
#include "Discount.h"
#include "FixatedDiscount.h"
#include <vector>

int main()
{
    Product* p1 = new Product("banana", 20, "fruit");
    Product* p2 = new Product("coconut", 35, "fruit");
    Product* p3 = new Product("watermelon", 45, "vegetable");
    Product* p4 = new Product("cucumber", 20, "vegetable");
    Product* p5 = new Product("butter", 30, "dairy");
    Product* p6 = new Product("milk", 40, "dairy");
    std::vector<Product*> catalog{ p1, p2, p3, p4, p5, p6 };

    Basket basket;
    basket.insert(catalog[1]);
    basket.insert(catalog[2]);
    basket.insert(catalog[5]); //All of these 3 products make up to value of 120

    Discount* fixated1 = new FixatedDiscount(150, 20);
    Discount* fixated2 = new FixatedDiscount(100, 15);
    Discount* fixated3 = new FixatedDiscount(50, 10); //made these 3 discounts
    fixated1->nextDiscount(fixated2);
    fixated2->nextDiscount(fixated3); //linked by pointers these 3 discounts
    std::cout << "Price with the discount: " << fixated1->countDiscount(basket) << std::endl;
}

所以这里发生的事情是,正如您在FixatedDiscount.cpp中看到的那样,我做了一个简单的cout来检查达到了哪个折扣,它说Handled when limit is 100 and discount is: 15, returning total price: 105应该是,但是,它以某种方式再次通过countDiscount function 并返回120 ,这是没有应用折扣的价格,这怎么可能? 它没有 go 到第三个折扣,但不知何故,它再次通过 function 并返回 120,所以我的理论是,当它达到第二个折扣时,它返回了我想要的105 ,因为应用了 15 的折扣,然后它回到第一个指针,即第一个折扣,由于 120 不超过 150,它不应用折扣并返回 120。为什么它不只是返回我想要的并停止? 为什么它 go 通过 function 再次可能通过指针返回并再次应用已经检查且不应再检查的折扣。

int FixatedDiscount::countDiscount(Basket b) {
    int totalPrice = b.countPrice(); //this method just counts the value of the basket which for our example is 120
    if (totalPrice >= limit) {
        totalPrice -= discount;
        std::cout << "Handled when limit is " << limit << " and discount is: " << discount << " returning total price: " << totalPrice << std::endl;
    }
    else if (next != NULL) {
        next->countDiscount(b);
    }
    else {
        std::cout << "I am the last handler" << std::endl;
    }
    return totalPrice;
}

在调用next->countDiscount(b); ,function 不返回,它返回应用折扣后未修改的totalPrice 您应该在此处分配返回值以使该折扣生效。

totalPrice = next->countDiscount(b); 应该返回 105。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM