Skip to main content

SOLID- Design Principles



The SOLID Principles of programming are a set of rules which when followed during designing and developing an object oriented application, will reap the following benefits.
  • Loosely Coupled Application
  •  Easily Extensible application
  • Highly manageable code for maintenance

SOLID is an acronym that can be expanded as Below:
(I’ll first  get into theoretical aspect of it and then clarify it via code)


S: Single Responsibility Principle 
A class should do only one thing .
Logically speaking Separation of Concern and separation of responsibility is what we need to differentiate. This principle talks about responsibility not concern.

“My responsibility as a developer is to meet deadline and code as per the standards, if the client is not happy, that is none of my concerns (isn't it soothing…..)”

.Same goes with class, it should be doing only one thing at a time, if multiple responsibilities are entrusted to it(imagine you doing job of a co-coordinator, developer, designer, team leader all at one time…all you’ll become is jack of All, master of None).

In simpler terms, always make sure a class has ONE and ONLY ONE responsibility(it does not mean it should have only one functions though.)

O: Open / Closed Principle 
 A class should be open for extension but closed for modification. Classes should be designed so they can be inherited from without having to modify them.
While designing classes for any enterprise application, always keep in mind that the class should not be modified, however whenever there is a need, create another class and extend the base class.

L:Liskov Substitution Principle 
 Derived classes must be substitutable for their base class.
 When a class method accepts an object, the method should be able to accept children of that object.
If there is Base Class A and an inherited class B, wherever an object of A is expected and we instead pass an object of B, the code should work fine.(Simple OOPS right??...obviously the first thing that strikes the mind is Interfaces, don’t worry it’s next).

I:Interface Segregation Principle 
 Interfaces should be fine grained and only provide for current project needs. When building interfaces, never add more functionality than what is needed at the time.
Assume creating an Interface IMylife(that will sum up the gist of life.).Now let’s add some methods to it
Void childhoodmemories ();
Void MylifeAsTeenager();
Void MyYouth()
Void MyRetirement();
Void MyDeath()
Void MyNextLife()
Void MyNExtLifeChildhood()

Now let’s try to implement this interface, you’ll endup implementing your nextbirth(and all you wanted was implement your current).
In the current scenario, you’ll definitely throw not implemented exception in all the cases that does not hold true currently.
So the point is, just do the needful, planning next life is not such a good idea in the current life(am I getting boring..or too sarcastic?)

D: Dependency Inversion Principle 
High level modules should not depend on low level modules, but instead both should depend on abstractions. Interfaces should be used as placeholders and factory classes should be used to instantiate objects. This causes loose coupling between classes.

OOPs does it sound like Substitution principle?...well these principles does not work in isolation, they are used collectively to achieve the goals of designing.

I’ll start a series now to implement these principles in code starting form SOLID-Single Responsibility Principle.

Hope the information is useful,

Do remember to go to my next blog entry in this series.

Till next we connect…….
Happy Learning…..

Comments

Popular posts from this blog

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8...

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either ...

Advanced WCF

In this post, I am sharing the link of articles about  advanced topics in WCF. The List of articles is exhaustive and can serve as your repository for all WCF queries. Concurrency,Throttling & Callbacks  WCF Concurrency (Single, Multiple and Re entrant) and Throttling   WCF-Interop and BinarySecurityToken  WCF Callbacks  Creating Web Services From WSDL Link1 Link2 Link3 Link4 WCF-Security WCF over HTTPS   Transport Security(basic)/HTTPS UserNamePasswordValidator ServerCertificateValidationCallback 9 simple steps to enable X.509 certificates on WCF - CodeProject http://www.codeproject.com/KB/WCF/9StepsWCF.aspx?display=Print Message Security(Certificate)/PeerTrust Securing WCF Services with Certificates. - CodeProject http://www.codeproject.com/KB/WCF/wcf_certificates.aspx Message Security(Certificate)/ChainTrust How To Configure WCF Security Using Only X.509 Certificates - CodePr...