Skip to main content

5 class design principles [S.O.L.I.D.] in Java

Classes are the building blocks of your java application. If these blocks are not strong, your building (i.e. application) is going to face the tough time in future. This essentially means that not so well-written can lead to very difficult situations when the application scope goes up or application faces certain design issues either in production or maintenance.

On the other hand, set of well designed and written classes can speed up the coding process by leaps and bounds, while reducing the number of bugs in comparison.

In this post, I will list down 5 most recommended design principles, you should keep in mind, while writing your classes. These design principles are called SOLID, in short. They also form the best practices to be followed for designing your application classes.
  1. Single Responsibility Principle
  2. Open Closed Principle
  3. Liskov’s Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

Lets drill down all of them one by one.

Single Responsibility Principle

The name of the principle says it all:

"One class should have one and only one responsibility"

In other words, you should write, change and maintain a class for only one purpose. If it is model class then it should strictly represent only one actor/ entity. This will give you the flexibility to make changes in future without worrying the impacts of changes for another entity.

Similarly, If you are writing service/manager class then it should contain only that part of method calls and nothing else. Not even utility global functions related to module. Better separate them in another globally accessible class file. This will help in maintaining the class for that particular purpose, and you can decide the visibility of class to specific module only.


Open Closed Principle

This is second important rule which you should keep in mind while designing your application. It says:

"Software components should be open for extension, but closed for modification"

What does it mean?? It means that your classes should be designed such a way that whenever fellow developers wants to change the flow of control in specific conditions in application, all they need to extend your class and override some functions and that’s it.

If other developers are not able to design desired behavior due to constraints put by your class, then you should reconsider changing your class. I do not mean here that anybody can change the whole logic of your class, but he/she should be able to override the options provided by software in unharmful way permitted by software.

For example, if you take a look into any good framework like struts or spring, you will see that you can change their core logic and request processing, BUT you modify the desired application flow just by extending some classes and plugin them in configuration files.


Liskov’s Substitution Principle

This principle is a variation of previously discussed open closed principle. It says:

"Derived types must be completely substitutable for their base types"

It means that the classes fellow developer created by extending your class should be able to fit in application without failure. I.e. if a fellow developer poorly extended some
part of your class and injected into framework/ application then it should not break the application or should not throw fatal exceptions.

This can be insured by using strictly following first rule. If your base class is doing one thing strictly, the fellow developer will override only one feature incorrectly in worst case. This can cause some errors in one area, but whole application will not do down.


Interface Segregation Principle

This principle is my favorite one. It is applicable to interfaces as single responsibility principle holds to classes. It says:

"Clients should not be forced to implement unnecessary methods which they will not use"

Take an example. Developer Alex created an interface “Reportable” and added two methods generateExcel() and generatedPdf(). Now client ‘A’ wants to use this interface but he intend to use reports only in PDF format and not in excel. Will he achieve the functionality easily.

NO. He will have to implement two methods, out of which one is extra burden put on him by designer of software. Either he will implement another method or leave it blank. So are not desired cases, right??

So what is the solution? Solution is to create two interfaces by breaking the existing one. They should be like PdfReportable and ExcelReportable. This will give the flexibility to user to use only required functionality only.


Dependency Inversion Principle

Most of us are already familiar with the words used in principle’s name. It says:

"Depend on abstractions, not on concretions"

In other words. you should design your software in such a way that various modules can be separated from each other using an abstract layer to bind them together. The classical use of this principle of BeanFactory inspring framework. In spring framework, all modules are provided as separate components which can work together by simply injected dependencies in other module. They are so well closed in their boundaries that you can use them in other software modules apart from spring with same ease.

This has been achieved by dependency inversion and open closed principles. All modules expose only abstraction which is useful in extending the functionality or plugin in another module.

These were five class design principle which makes the best practices to be followed to design your application classes. Let me know of your thoughts.

Happy Learning !!

Comments

Popular posts from this blog

Ask a Question and get the best possible Answer!

What’s the best way to ask a question ? The easiest way to contact me about any computer problems or issues is via email. You can reach me via email at snehal [at] techproceed [dot] com. I will respond to as many emails as I can, but due to the large volume of emails that I recieve every day, there is no guarantee that I will get back to you.  If you have any other inquires, questions, comments, ideas, etc, feel free to fill the form given below and I will do my best to respond.  Submit an article to see it on Techproceed ! Are you interested to see your article on TechProceed for the benefit of IT community?  I believe that personally accessible technology is the foundation of humanity’s future. To that end I help people to understand and safely use personal computers and related technology so that they can do more, be more, grow more and connect more than ever before, and be an active participant in that future.  You could submit...

What's Hot

Ask Snehal AI The world's best virtual tarot This Virtual tarot that that can answer all kinds of questions. Questions about the present, the past, and your future. He can be a bit temperamental, requiring that each question be presented with a petition of "Snehal, please answer the following question" or "Snehal, please answer" before each question is asked. Failure to correctly petition will not bring results.    http://asksnehal.techproceed.com/        What's Hot ?                 This section provides a snapshot of what's on the public's collective mind by allowing users to view the fastest-rising searches for different points of time. It also highlights search terms that have suddenly become the most popular among the rest.  Following list is updated day by day, dynamically: Share Files of Any Size Online via Private Torrent in...

How To Read Medium Articles for Free

I’m a regular Medium user and I read Medium articles almost every day. A few years back I didn’t have Medium membership—so, I had to find ways how to read Medium articles for free. I was able to find some ways to bypass Medium’s paywall system and read an unlimited amount of articles every day. I’ll share exactly how to read Medium articles for free in this article. Medium is a great blogging platform. This platform allows anyone to publish and read articles, but some of the articles on Medium are behind a paywall, which means they require a paid membership to read. Medium offers every user 3 free articles to read every month. That means you can read up to 3 articles that are published behind the Medium paywall. Stories that aren’t behind the Medium paywall are forever free to read. Here’s how to read Medium articles for free: You can read Medium articles for free by using the incognito mode of your browser, using extensions of Chrome, using the Telegram instant view from...