Posts

Showing posts with the label solid-principles

Exception member class OOP

Exception member class OOP I have met the following concept in the production code: class A { public: class Exception : public std::exception {/* ... */}; //... }; Nobody can give me a clear answer why it is like this. My question is whether this approach is in line with SOLID rules? I think that would be better if this exception class is located outside the class A and is injected while creating the instance of A. Design principles are important, but they aren't followed "because they are design principles". They are followed because they accomplish something. What would injecting the exception object upon construction accomplish? Now the class should manage this objects lifetime. What if an error state is never encountered, and it's never thrown? – StoryTeller Jul 2 at 6:16 ...

Inject a call automatically

Inject a call automatically I have a scenario which is based on the Open Closed principle. I have a Cart class, which has a method CalculateTotal() . This method takes MODE and QTY as parameters. Cart CalculateTotal() MODE QTY Based on the MODE , the amount is calculated. MODE public class Cart { ICalculator calculator; public Cart() { calculator = new Calculator(); } public decimal CalculateTotal(string MODE,decimal QTY) { return calculator.CalculateTotal(MODE, QTY); } } public interface ICalculator { decimal CalculateTotal(string MODE, decimal QTY); } public class Calculator : ICalculator { private readonly List<IRule> rules; public Calculator() { rules = new List<IRule>(); rules.Add(new OrdinaryRule()); rules.Add(new SpecialRule()); rules.Add(new OfferRule()); } public decimal CalculateTotal(string MODE, decimal QTY) { return rules.First(x => x.IsMatch(MO...