Note that there are some explanatory texts on larger screens.

plurals
  1. POS.O.L.I.D Essentials missing points?
    text
    copied!<p><em>I've read so many articles about this but still I have 2 questions.</em></p> <p><strong>Question #1</strong> - Regarding <a href="http://www.remondo.net/solid-principles-csharp-dependency-inversion/" rel="nofollow noreferrer">Dependency Inversion</a> :</p> <blockquote> <p>It states that high-level classes should not depend on low-level classes. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.</p> </blockquote> <p>for example :</p> <pre><code>public class BirthdayCalculator { private readonly List&lt;Birthday&gt; _birthdays; public BirthdayCalculator() { _birthdays = new List&lt;Birthday&gt;();// &lt;----- here is a dependency } ... </code></pre> <p>The fix : put it in a ctor.</p> <pre><code>public class BirthdayCalculator { private readonly IList&lt;Birthday&gt; _birthdays; public BirthdayCalculator(IList&lt;Birthday&gt; birthdays) { _birthdays = birthdays; } </code></pre> <ul> <li><p>If it will be in ctor - I'll have to send it each and every time I use the class. So I will have to keep it when calling the <code>BirthdayCalculator</code> class. it it ok to do it like that ? </p></li> <li><p>I can argue that , after the fix , still - <code>IList&lt;Birthday&gt; _birthdays</code> should not by there ( the <code>Birthday</code> in <code>IList</code>) - but it should be as <code>IList&lt;IBirthday&gt;</code>. Am I right ? </p></li> </ul> <p><strong>Question #2</strong> - Regarding <a href="http://www.remondo.net/solid-principles-csharp-liskov-substitution/" rel="nofollow noreferrer">Liskov Substitution</a> :</p> <blockquote> <p>derived classes must be substitutable for their base classes</p> </blockquote> <p>or more accurate : </p> <blockquote> <p>Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.</p> </blockquote> <p><em>(already read <a href="https://stackoverflow.com/questions/1735137/liskov-substitution-principle-no-overriding-virtual-methods">this</a> )</em></p> <p>example :</p> <pre><code>public abstract class Account { public abstract void Deposit(double amount); } </code></pre> <p>I have a class : </p> <pre><code>public class CheckingAccount : Account { public override void Deposit(double amount) { ... _currentBalance += amount; } } </code></pre> <p>and the bank wants to open a Mortgage account - so : </p> <pre><code>public class MortgageAccount : Account { public override void Deposit(double amount) { _currentBalance -= amount; //&lt;-----notice the minus } } </code></pre> <p>The problem arises when there is a function which accepts mortage as <code>Account</code> and do deposit.</p> <pre><code>public class Bank { public void ReceiveMoney(Account account, double amount) { double oldBalance = account.CurrentBalance; account.Deposit(amount); //oopssss????? } } </code></pre> <p>so here , it violates the LSP.</p> <p><strong>But</strong> I don't understand.</p> <p>every overridden method will do different code when overridden so it will <em>never</em> be <em>100%</em> replaceable !</p> <p>the definition did NOT talk about " logic should continue as in base class ( always deposit(add) positive numbers)"</p> <p>example:</p> <p>What if both <code>CheckingAccount</code> class and <code>MortgageAccount</code> class were depositing positive numbers but <code>MortgageAccount</code> also log to db ? does it still breaks LSP ? What is the boundry for breaks/not-brakes LSP ?</p> <p>the definition should define whatis that boundry. and it isn't saying anything about it.</p> <p>what am i missing ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload