In software engineering, a design pattern is a repeatable solution to a commonly occurring problem in software design. Christopher Alexander, a civil engineer, is the one who came up with the idea of design patterns. While working on designing buildings and towns, he found that there are common design problems and certain design constructs can be used repeatedly to solve them. He documented and published his experiences on design patterns.
Around three decades ago, software professionals began to incorporate Alexander’s design principles. This early work led others to write on design patterns in software development. In 1995, Eric Gamma, Richard Helm, Ralph Johnson, and John Vlissides published their book Design Patterns: Elements of Reusable Object Oriented Software . This book became the most popular book on design patterns. The design patterns in this book are also called as “Gang of Four” (GOF) as there were four authors. These design patterns are still relevant and widely used. GOF design patterns are based on Object Oriented programming and are language agnostic. This blog post describes the purpose of all 23 GOF design patterns.
Along with GOF, there are other design patterns which are also used in software design. There are technology specific design patterns such as Core Java EE design patterns . There are design patterns that solve design problems in a particular type of application such as Workflow patterns . There are concurrency patterns  to address design issues in multithreaded applications, Enterprise Integration Patterns  that help in designing integrated applications. There are architecture patterns that help us in designing robust enterprise applications . There are also design patterns for SOAP and RESTful web services .
The design patterns also provide a vocabulary for communication and documentation of software designs.
Object oriented programming has helped in making code more maintainable with features such as encapsulation and inheritance as compared to procedural programming. A class defines attributes and behavior related to an entity, thus providing encapsulation or a boundary. Objects enable domain modeling.
A domain is a collection of objects and their relationships. If entities of an application are designed using domain modelling , they enable to define entities and their relationships. The domain models can be used to document complex domains.
Java Persistance API (JPA), a Java technology that maps objects to relational database and provides abstraction over the JDBC, uses domain modeling to define entities and their relationships.
SOLID design principles by Robert Martin are the object oriented principles that help to write easily readable and maintainable code. SOLID is an acronym that stands for S – Single Responsibility principle, O – Open Closed principle, L – Liskov Substitution principle, I – Interface Segregation principle and D – Dependency Injection.
This design principle (S in SOLID) states that a class should have only one reason to change. A class should have only one responsibility and there should be one class for one responsibility. Classes that adhere to this principle have high cohesion and are more maintainable then classes that don’t support this principle.
For example, if you have to parse different message formats then create a separate class for parsing each format instead of writing code in a single class. If a message format changes, then only the respective class will change. Classes for other formats will not change and they don’t need to be tested.
Composite design pattern which is a GOF design pattern uses Single Responsibility design principle.
The Open Closed design principle (the O in SOLID) states that a class should be open for extension but closed for modification. This principle restricts addition of new behavior in existing class hierarchy but allows extending the hierarchy by addition of subclasses. The Decorator and Strategy GOF design patterns follow Open Closed design principle. Decorator allows extension of behavior during runtime whereas Strategy allows implementation of behavior during compile time.
Dependency Inversion principle states that instead of instantiating referenced classes within a class, they should be instantiated outside the class definition and passed as references in the class. Dependency Inversion (D in SOLID) injects the reference of the class instance through setter methods of interface or abstract class type. Dependency Inversion relies on abstraction instead of concrete classes. This design principle promotes loose coupling. Since the objects are instantiated outside the class and not within the class this is also called as Inversion of Control.
As Dependency Inversion relies on abstraction, any concrete class instance of the abstract class type can be used. Since the objects are instantiated outside the referenced classes, changes from one object to another of same abstract class type can be done easily without changing the referenced classes. Also, this code change is done at one place instead of making change in every referenced class. This is the advantage of using Dependency Inversion.
The terms Inversion of Control, Dependency Injection, Dependency Inversion mean the same. The term Hollywood principle (don’t call us, we’ll call you) also means the same.
Spring framework provides Dependency Injection.
EJB 3.1 uses both Context and Dependency Injection.
The Factory Method, Template Method, Observer and Chain of Responsibility GOF design patterns follow Dependency Inversion principle.