<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Software Rockstar &#187; Design Principles</title>
	<atom:link href="http://www.softwarerockstar.com/tag/design-principles/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.softwarerockstar.com</link>
	<description>Coaching and mentoring on a journey from a Developer to an IT Leader</description>
	<lastBuildDate>Thu, 12 Nov 2009 18:12:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The Castle Project</title>
		<link>http://www.softwarerockstar.com/2007/08/the-castle-project/</link>
		<comments>http://www.softwarerockstar.com/2007/08/the-castle-project/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 20:37:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2007/08/17/the-castle-project/</guid>
		<description><![CDATA[In the last couple of years I have noticed a steadily growing interest in the .NET community around the&#160;Inversion of Control (IoC) and Dependency Injection (DI) patterns.&#160; This is partly due to the fact that a number of excellent frameworks have emerged and gained popularity that allow us to use these designs in our applications [...]]]></description>
			<content:encoded><![CDATA[<div class="imageleft"><img src="http://softwarerockstar.com/wp-content/uploads/2007/08/castleinabox1.gif" alt="Castle Project" title="Castle Project" width="191" height="108" /></div>
<p>In the last couple of years I have noticed a steadily growing interest in the .NET community around the&nbsp;<a href="http://en.wikipedia.org/wiki/Inversion_of_control">Inversion of Control (IoC)</a> and <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection (DI)</a> patterns.&nbsp; This is partly due to the fact that a number of excellent frameworks have emerged and gained popularity that allow us to use these designs in our applications relatively painlessly.&nbsp; One such framework is the open source <a href="http://www.castleproject.org/">Castle Project</a>.&nbsp; In my opinion <a href="http://www.castleproject.org/">Castle</a> is the best implementation of IoC and DI among many others that I have come across including <a href="http://www.springframework.net/">Spring.NET</a>, <a href="http://ninject.org/">NInject</a>, and&nbsp;<a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=ObjectBuilder">ObjectBuilder</a>.&nbsp; This is however my opinion; which framework you use largely depends upon your requirements and your preferences.</p>
<p>If you are just getting started with Castle or are curious about why the heck one should use IoC and DI, I strongly suggest you check out the <a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx">3-part article series</a> by&nbsp;<a href="http://dotnetslackers.com/community/blogs/simoneb">Simone Busoli</a>.&nbsp; I just came across <a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx">these articles</a> and I can&#8217;t stress enough how well they are written.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2007/08/the-castle-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISP: Interface Segregation Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/#comments</comments>
		<pubDate>Tue, 29 Aug 2006 00:10:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/28/isp-interface-segregation-principle/</guid>
		<description><![CDATA[The Interface Segregation Principle (ISP) deals with cohesion and is closely related to the Single Responsibility Principle (SRP). In Uncle Bob&#8217;s terms, the ISP states that: Clients should not be forced to depend on methods that they do not use. In other words, this principle states that if a class performs multiple functions that may [...]]]></description>
			<content:encoded><![CDATA[<p>The Interface Segregation Principle (ISP) deals with <a href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)">cohesion</a> and is closely related to the <a href="http://mfharoon.blogspot.com/2006/08/srp-single-responsibility-principle.html">Single Responsibility Principle (SRP)</a>. In <a href="http://www.objectmentor.com/resources/articles/isp.pdf">Uncle Bob&#8217;s terms</a>, the ISP states that:</p>
<blockquote><p>Clients should not be forced to depend on methods that they do not use.</p></blockquote>
<p>In other words, this principle states that if a class performs multiple functions that may be used by different client classes, then those functions should be seperated out into different interfaces according to their usage.</p>
<p>The idea behind this principle is to confine the aount of rework to only clients that use a certain interface, should the interface of a depended-upon class changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/isp-interface-segregation-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DIP: Dependency Inversion Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/#comments</comments>
		<pubDate>Sun, 27 Aug 2006 13:58:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/27/dip-dependency-inversion-principle/</guid>
		<description><![CDATA[The Dependency Inversion Principle deals with how to correctly design classes such that their dependency on one another causes the least amount of work in case of a change. Uncle Bob&#8217;s definition of DIP states: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details [...]]]></description>
			<content:encoded><![CDATA[<p>The Dependency Inversion Principle deals with how to correctly design classes such that their dependency on one another causes the least amount of work in case of a change. <a href="http://www.objectmentor.com/resources/articles/dip.pdf">Uncle Bob&#8217;s definition</a> of DIP states:</p>
<blockquote>
<ul>
<li>High-level modules should not depend on low-level modules. Both should depend on abstractions.</li>
<li>Abstractions should not depend on details. Details should depend on abstractions.</li>
</ul>
</blockquote>
<p>Essentially what this principle means is that in a tiered design, higher level modules and lower level modules should not directly depend on each other; instead they should only depend on <a href="http://en.wikipedia.org/wiki/Abstraction">abstractions</a>. Moreover, abstractions should not have any knowledge of details (other classes in the system).</p>
<p>This principle closely relates to <a href="http://mfharoon.blogspot.com/search?label=Design+Principles">some of the other design principles</a> I discussed previously in that it yields a design that is highly decoupled ensuring that minor changes in one part of the application do not cause a domino effect in other parts of the application. Moreover such a design is extensible &#8212; as new business entities are added, usually such as design scales well by requiring only new code to be added rather than existing code to be modified.  ASP.NET 2.0&#8242;s <a href="http://aspnet.4guysfromrolla.com/articles/101905-1.aspx">Provider Model</a> is a great example of such a design that provides a default implementation for various sub-systems such as Membership, Personalization, Navigation, etc. but also allows developers to modify the default behavior by extending certain classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/dip-dependency-inversion-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LSP: Liskov Substitution Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/#comments</comments>
		<pubDate>Thu, 24 Aug 2006 00:23:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/23/lsp-liskov-substitution-principle/</guid>
		<description><![CDATA[The Liskov Substitution Principle dictates when and where it is correct to derive a subtype from an existing supertype. As defined by Barbara Liskov and Jeannette Wing, it essentially states that: 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 [...]]]></description>
			<content:encoded><![CDATA[<p>The Liskov Substitution Principle dictates when and where it is correct to derive a <a href="http://en.wikipedia.org/wiki/Subtype">subtype</a> from an existing supertype.  As defined by <a href="http://www.pmg.csail.mit.edu/~liskov/">Barbara Liskov</a> and Jeannette Wing, it essentially states that:</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>What it means is that if S is a subtype of T, then a function q(x) must behave in the same manner irrespective of the type of x whether it be S or T.  In other words, if a piece of code behaves differently for a subtype than a supertype, then that code violates the LSP (and consequently the OCP).</p>
<p>Let&#8217;s say that we work for a bank and that we have a simple teller application.  Currently the teller application works with checking and savings account types.</p>
<pre class="brush: csharp">
public abstract class Account
{
    public abstract double CurrentBalance { get; }
    public abstract void Deposit(double amount);
}

public class CheckingAccount : Account
{
    private double _currentBalance;

    public override double CurrentBalance
    {
        get { return _currentBalance; }
    }

    public override void Deposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class SavingsAccount : Account
{
    /* Savings account implementation */
}
</pre>
<p>Our bank just entered the mortgage business so we need to modify our teller application to support this new account type.  During the short analysis phase we decide that since mortgage account is type of an account (IS-A relationship), we can simply create a new mortgage class deriving from the Account abstract class, override a method and a property, and we should be in business.  We add a new class as follows:</p>
<pre class="brush: csharp">
public class MortgageAccount : Account
{
    private double _currentBalance;

    public override double CurrentBalance
    {
        get { return _currentBalance; }
    }

    public override void Deposit(double amount)
    {
        _currentBalance -= amount;
    }
}
</pre>
<p>Everything sounds logical, that is until we come across this code in the teller application:</p>
<pre class="brush: csharp">
public class Bank
{
    public void ReceiveMoney(Account account, double amount)
    {
        double oldBalance = account.CurrentBalance;
        account.Deposit(amount);
        Debug.Assert(account.CurrentBalance = oldBalance + amount);
    }
}
</pre>
<p>Based on our pre-existing code, the ReceiveMoney() method is making a reasonable assumption that the new balance should be equal to the old balance plus the newly deposited amount.  This assumption is, however, violated if we pass an instance of the Mortgage class to this method.  This is a clear violation of the LSP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/lsp-liskov-substitution-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OCP Open-Closed Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/#comments</comments>
		<pubDate>Tue, 22 Aug 2006 00:04:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/21/ocp-open-closed-principle/</guid>
		<description><![CDATA[Another fundemental principle of object-oriented software design is the Open-Closed Principle. According to Uncle Bob (Robert Martin), this principle states that: Software entities (classes, modules, functions, etc.) should beopen for extension, but closed for modification. Essentially, what this means is that software&#8217;s design should be such that it&#8217;s behavior can be modified by extending the [...]]]></description>
			<content:encoded><![CDATA[<p>Another fundemental principle of object-oriented software design is the Open-Closed Principle. According to <a href="http://www.objectmentor.com/publications/ocp.pdf#search=%22Object%20mentor%20OCP%22">Uncle Bob (Robert Martin)</a>, this principle states that:</p>
<blockquote><p>Software entities (classes, modules, functions, etc.) should be<br />open for extension, but closed for modification.</p></blockquote>
<p>Essentially, what this means is that software&#8217;s design should be such that it&#8217;s behavior can be modified by <em>extending</em> the existing source code rather than <em>modifying</em> it. Consider the following example:</p>
<pre class="brush: csharp">
public class CheckingAccount
{
    private double _currentBalance;

    public void ProcessDeposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class MortgageAccount
{
    private double _currentBalance;

    public void ProcessPayment(double amount)
    {
        _currentBalance -= amount;
    }
}

public class Bank
{
    public void ReceiveMoney(object account, double amount)
    {
        if (account.GetType() == typeof(CheckingAccount))
        {
            CheckingAccount checkingAccount = (CheckingAccount)account;
            checkingAccount.ProcessDeposit(amount);
        }
        else if (account.GetType() == typeof(MortgageAccount))
        {
            MortgageAccount mortgageAccount = (MortgageAccount)account;
            mortgageAccount.ProcessPayment(amount);
        }
    }
}
</pre>
<p>Notice that the ReceiveMoney() method of the Bank class has to determine the type of account passed in. Only based on that information it can perform the appropriate business logic. Later if we add another account type then we may need to modify this code in order for it to work properly. The above code is, hence, in violation of the Open-Closed principle.</p>
<p>So how can we revise our code to conform to the Open-Closed Principle? Abstraction is the answer. Let&#8217;s take a look:</p>
<pre class="brush: php">
public abstract class Account
{
    public abstract void Deposit(double amount);
}

public class CheckingAccount : Account
{
    private double _currentBalance;

    public override void Deposit(double amount)
    {
        _currentBalance += amount;
    }
}

public class MortgageAccount : Account
{
    private double _currentBalance;

    public override void Deposit(double amount)
    {
        _currentBalance -= amount;
    }
}

public class Bank
{
    public void ReceiveMoney(Account account, double amount)
    {
        account.Deposit(amount);
    }
}
</pre>
<p>Notice how we got rid of all conditional logic in the ReceiveMoney() method. Also notice that if we later add a new account type, the ReceiveMoney() method will work without any modifications. The revised version of our code above, hence, conforms to the Open-Closed Principle.</p>
<p>Let&#8217;s now consider what happens to our code if a new business rule is added that requires an e-mail notification to be sent to the account holder whenever a deposit is made. Obviously, we&#8217;ll have to modify the ReceiveMoney() method to perform that logic. Since we can&#8217;t account for all possible scenarios, we can never acheive <em>perfect closure</em>. With that in mind, design the simplest software that will do the job, and conform to OCP or other such design principles where you see the need.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/ocp-open-closed-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SRP: Single Responsibility Principle</title>
		<link>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/</link>
		<comments>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/#comments</comments>
		<pubDate>Sun, 20 Aug 2006 13:42:00 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[OOA/OOD]]></category>

		<guid isPermaLink="false">http://mharoon.wordpress.com/2006/08/20/srp-single-responsibility-principle/</guid>
		<description><![CDATA[SRP is one of the fundamental object oriented software design principles, first introduced by Tom DeMarco in 1979. In simple terms, SRP states that every class should have a single well defined goal, and that all members of that class (properties, methods, etc.) should work together to achieve that goal. Robert Martin puts it in [...]]]></description>
			<content:encoded><![CDATA[<p>SRP is one of the fundamental object oriented software design principles, first introduced by <a href="http://www.systemsguild.com/GuildSite/TDM/Tom_DeMarco.html">Tom DeMarco</a> in 1979.</p>
<p>In simple terms, SRP states that every class should have a single well defined goal, and that all members of that class (properties, methods, etc.) should work together to achieve that goal. <a href="http://www.objectmentor.com/resources/articles/srp">Robert Martin</a> puts it in slightly different terms:</p>
<blockquote><p>There should never be more than one reason for a class to change.</p></blockquote>
<p>Essentially SRP enforces the concept of high <a href="http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29">cohesion</a> that should not only be applied to classes but also internals of a class, such as methods.</p>
<p>So why is SRP such an important design principle? It is because a class with multiple responsibilities is more difficult to change and more difficult to reuse (design smells: <a href="http://blogs.netindonesia.net/zeddy/archive/2006/07/31/10428.aspx">fragility and immobility</a>).</p>
<p>While SRP is quite easy to understand, it&#8217;s not <em>always</em> logical to implement. Consider the following class:</p>
<p><img style="display:block;cursor:hand;text-align:center;margin:0 auto 10px;" alt="" src="http://photos1.blogger.com/blogger2/5599/106541226764272/400/SRP-Image1.jpg" border="0" /></p>
<p>Given the convenience of having a customer class that is smart enough to know how to load and save itself, would you rather break it out like this?:</p>
<p><img style="display:block;width:400px;cursor:hand;text-align:center;margin:0 auto 10px;" alt="" src="http://photos1.blogger.com/blogger2/5599/106541226764272/400/SRP-Image2.jpg" border="0" />
<p>According to SRP, you should, because the combined customer class shares multiple responsibilities: customer business logic, and customer persistence logic. In fact if you use <a href="http://en.wikipedia.org/wiki/Test_driven_development">TDD</a>, you may even be <em>forced</em> to separate these two responsibilities.</p>
<p>In practicality, however, while we should keep this principle in mind and try to apply it wherever it makes sense, we should never get so carried away that we start introducing other design smells in our architecture, such as <a href="http://blogs.netindonesia.net/zeddy/archive/2006/07/31/10428.aspx">Rigidity, Viscosity, and Needless Complexity</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2006/08/srp-single-responsibility-principle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->