Saturday, May 10, 2014

Design Patterns using Java


The design principles are set of guidelines which help us to avoid bad design.A design is said to be bad if it has any one or all the features listed below:
  • Rigid:   Hard to change and not flexible.If we have to change part of code, it forces us to change many other parts of the implementation.
  • Fragile:   Easily broken or damaged.Easily breaks the functionality of other parts. When we change part of code it breaks implementation in other parts.
  • Immobility: Not reusable.We cannot reuse the code in other parts as it is difficult to disentangle from the current implementation.
SOLID (Single responsibility, Open-closed, Liskov’s substitution, Interface segregation and Dependency inversion) is an acronym that stands for basic five principles of object oriented programming and design.The principles when applied together intend to make it more likely that a programmer will create a system that is easy to maintain and extend over time.It is part of an overall strategy of agile and adaptive programming.The basic principles are listed below.


  1. Single Responsibility Principle 
  2. Open Close Principle
  3. Liskov's Substitution Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle
Here we discuss just the principles which will help us to make the design better. In practice we have to put some extra effort to adopt these principles in our code by using the most appropriate pattern which fits our requirement.



Single Responsibility Principle 

In this context responsibility means a reason to change the class. This principle states that if we have two reasons to change the class then we have to split the class into two different classes. Each class will handle only one responsibility and if we need to change in functionality, only the class which implements the functionality will be changed.

                    A class should have one reason to change
If the class has more than one reason to change then we need to divide the class.


Problem :