Discover how to effectively implement the `Chain of Responsibility` design pattern in C+ + and troubleshoot common pointer issues affecting discount applications.
---
This video is based on the question https://stackoverflow.com/q/67608905/ asked by the user 'Aldomond' ( https://stackoverflow.com/u/14486133/ ) and on the answer https://stackoverflow.com/a/67609080/ provided by the user 'Wander3r' ( https://stackoverflow.com/u/1694593/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Chain of responsibility going back by pointers for no reason
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Chain of Responsibility Pattern in C+ + : Solving the Pointers Issue
In the world of programming, designing systems that are flexible and maintainable is a crucial aspect. The Chain of Responsibility (CoR) is a behavioral design pattern that facilitates this by allowing multiple objects to handle requests without the sender needing to know which object will handle the request. However, implementing this pattern can sometimes lead to challenges, particularly concerning pointer management in languages like C+ + . In this post, we will dissect a problem involving the Chain of Responsibility pattern applied to a discount system, discussing both the setup and the issues surrounding it.
The Problem: Unwanted Output Due to Pointers
Imagine you have a shopping system consisting of different classes, such as Product, Basket, and Discount. You have configured discounts to be applied in a chain-like mechanism. However, an unexpected behavior occurs during execution where the system returns a price after applying a discount but then incorrectly returns the original price again. This issue can be infuriating for developers as it disrupts the expected flow.
Debugging the Code
Let’s explore a snippet of the relevant classes involved in this issue:
[[See Video to Reveal this Text or Code Snippet]]
Here we define a base class Discount, holding a pointer to the next discount in the chain and a pure virtual function countDiscount() that needs to be overridden by derived classes. Now, moving on to one of the derived classes, a fixed discount:
[[See Video to Reveal this Text or Code Snippet]]
The countDiscount method applies the discount if the conditions are met but makes use of the next pointer to continue through potential other discounts.
The Core of the Issue
When you test the application, a specific segment of the countDiscount() function causes confusion. Although the discount is applied correctly, the method appears to return to the previous discount in the chain:
[[See Video to Reveal this Text or Code Snippet]]
Here lies the primary error—countDiscount(b) is called on the next discount in the chain, but the result is not captured. Hence, upon leaving the function, the default totalPrice (which remains unaltered) is returned instead of the modified price after discount:
Correcting the Oversight
To fix this, you should modify the return logic. By assigning the result of the next discount back to totalPrice, we keep track of the final discounted price correctly. Here is how the revised code should look:
[[See Video to Reveal this Text or Code Snippet]]
Expected Result
With this adjustment, if you have discounts like:
FixatedDiscount(150, 20)
FixatedDiscount(100, 15)
FixatedDiscount(50, 10)
And your basket totals 120, the expected output should now seamlessly reflect the correct price (105 after applying the 15 discount).
Conclusion
Navigating through complexities in pointer management in languages like C+ + , especially when implementing design patterns like the Chain of Responsibility, can be daunting. This particular case highlights the importance of capturing returned values correctly in a chain to avoid unintended results. The Chain of Responsibility may prove powerful, but it demands careful consideration regarding the flow of data through its handlers.
Understanding and utilizing the Chain of Responsibility pattern effectively can significantly enhance the flexibility and extensibility of your applications.
Информация по комментариям в разработке