What are design patterns? What are the design patterns in Java with examples? What are the software design patterns? Why we should learn design patterns? What are the gang of four (GoF) and J2EE design patterns in Java? What is the prerequisite to learn the design pattern?
When we use regular language / technology / framework in a regular manner in product or application development then there may be a chance of getting some problems repeatedly. In order to prevent those problems, we need to write some additional code as the solution for getting a better result. These additional code acting as the best solution for the reoccurring problem is referred to as the design patterns.
Note:- Design patterns are not related to the designing of a project. It is purely related to coding and the programmer’s work to implement them. It doesn’t have any relation with the designing phase of the project, they are implemented in the development phase of the project. It is not a language/technology/framework. It is all about writing solutions to the reoccurring problems.
For one reoccurring problem, there may be multiple solutions, but all those solutions won’t be called a design pattern. The best solution for that reoccurring problem will be called a design pattern and the worst solution will be called an anti-pattern.
Difference between pattern vs anti-pattern:- A pattern is an idea of how to solve a problem of some class whereas an anti-pattern is an idea of how not to solve it because implementing that idea would result in bad design.
What is Design Pattern?
Technically we can define design pattern as,
- Design patterns are a set of rules that are given as the best solutions for reoccurring problems of application development.
- The design patterns are the best practices to develop software applications effectively.
- Design patterns are 3 parts rules having relation “A context, a problem, and a solution”.
- Context:- the environment/surrounding/situation that creates the problem.
- Problem:- for which we need a solution for memory/CPU/performance problem and e.t.c.
- Solution:- the solution code that solves the problem.
Design patterns are not specific to one language/technology/framework, it can be implemented anywhere. But for large-scale, complex, and high-end applications mainly Java language is used therefore Java programmers encounter many problems compared to other language developers. The problem may be a memory/performance/integration issue and therefore we can see more utilization of design patterns in the Java language.
History of Design Patterns
These all things were started with Christopher Wolfgang Alexander. He is an architect and design theorist. Alexander has designed and personally built over 100 buildings, both as an architect and a general contractor.
He felt some common problems in those construction works and decided to document them. Alexander is perhaps best known for his 1977 book “A Pattern Language”, a perennial seller some four decades after publication.
It had an influence in the 1960s and 1970s on programming language design, modular programming, object-oriented programming, software engineering, and other design methodologies. Alexander’s mathematical concepts and orientation were similar to Edsger Dijkstra’s influential A Discipline of Programming.
In software, Alexander is regarded as the father of pattern language movement. Alexander’s work has also influenced the development of agile software development. The first wiki – the technology behind Wikipedia – led directly from Alexander’s work, according to its creator, Ward Cunningham.
Gang of Four Design Patterns
Inspired by that book (“A pattern language”) four people belonging to the software industry documented the reoccurring problems and their solutions. The book’s name is Design Patterns: Elements of Reusable Object-Oriented Software written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. The authors are often referred to as the Gang of Four (GoF). It has been influential to the field of software engineering and is regarded as an important source for object-oriented design theory and practice.
Initially, the book was containing 100+ problems, and their solutions but most of those problems were looking related and very similar. Finally, they concluded 23 basic design patterns, and the remaining problems are extended from those basic 23 problems.
Still, today, related to standalone application development and OOP these 23 problems and their solutions are treated as the best practices. The GoF design pattern can be implemented in any OOP language.
The GoF design patterns are classified into three categories,
- Creational Patterns:- Concern the process of object creation.
- Structural Patterns:- Deal with the composition of classes and objects.
- Behavioral Patterns:- Deal with the interaction of classes and objects.
|GoF Design Patterns||Examples|
|Creational Patterns||Singleton, Factory, Abstract Factory, Prototype, Builder, and e.t.c.|
|Structural Patterns||Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy, and e.t.c.|
|Behavioral Patterns||Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Momento, Observer, State, Strategy, Template, Visitor.|
Learn more here:- Types of design patterns
J2EE/JEE Design Patterns
The GoF design pattern is solving only standalone application-related problems but not for layered applications. In the Layered application, we don’t write all logic in a single class, we write different logic in multiple classes talking with each other. Those classes are called layers.
Sun Microsystems noticed problems in layered applications and they released their solution as the J2EE pattern or JEE pattern. They give problems and solutions related to Jakarta EE (formerly Java Platform, Enterprise Edition i.e. Java EE) based on layered application development. Some quite regularly used J2EE/JEE design patterns are- DAO, FrontController, ApplicationController, CompositeView, and e.t.c.
JEE modules and their technologies are given to develop layered applications. A layer is a logical partition/physical partition that represents specific logic taking the support of one or more components. MVC is the best architecture to develop Java-based layered applications. A typical JEE application contains 5 layers:- Client tier, Presentation tier, Business tier, Integration tier, Resource tier. While developing layered-based applications there will be some problems. In order to solve those problems, J2EE patterns are used.
Need of Design Pattern
The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities. Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether it can be applied in view of other design constraints, and the consequences and trade-offs of its use.
Designing object-oriented software is hard and designing reusable object-oriented software is even harder. – Erich Gamma
- Design patterns are the best practices to use software technologies more effectively in project development.
- Learning design pattern speeds up our experience accumulation in OOA/OOD.
- Well-structured object-oriented systems have reoccurring patterns of classes and objects.
- A design name, abstracts, and identifies key aspects of a common design structure that makes it useful for creating a reusable object-oriented design.
The most regularly used GoF design patterns in Java are,
- Factory Method
- Abstract Factory
The most regularly used J2EE design patterns are,
- Data Access Object (DAO)
- Business Object (BO)
- Data Transfer Object (DTO)
- Value Object (VO)
Pre-requisite to learn design patterns in Java:-
- To learn GOF design pattern OOP knowledge is sufficient.
- To learn the J2EE pattern JEE knowledge (Servlet/JSP) is required.
FAQ on Design Patterns in Java
Q1) Is MVC a design pattern?
No. MVC is an architecture, not a design pattern.
Q2) The architecture uses Design Patterns or does Design Pattern uses Architecture to develop the applications?
The architecture uses design patterns to develop the application. Example:- MVC uses Singleton, Front Controller, and e.t.c.
Q3) What is the difference between Architecture and Design patterns?
Architecture speaks about the various components that should be involved in the application development and defines communication among those components. Example of architecture:- MVC architecture. Design patterns speak about problems in each component development and apply solutions for solving those problems. Example:- In each layer development of MVC architecture we use multiple design patterns like Singleton Java class, View Helper, Front Controller, and e.t.c.
Q4) What are the types of design patterns?
There are many pattern catalogs are available but mainly two of them are very popular. 1) GoF Design Patterns, 2) J2EE Design Patterns. The GoF design patterns are categorized into three types, they are:- a) Creational Patterns, b) Structural Patterns, c) Behavioral Patterns
Q5) How many design patterns are there?
In GOF (Gang of Four) design patterns currently, there is a total of 23 patterns are given to deal with the reoccurring problems of OOP. In J2EE/JEE design patterns more than 15 patterns are available for the problem and solution related to JEE (Java/Jakarta Platform Enterprise Edition) based layered application development.
Q6) What are the most commonly used design patterns?
The most commonly used GOF design patterns in Java are Singleton, Factory, Builder, Strategy, Template, FlyWeight, Proxy. In J2EE/JEE design patterns the most commonly used are:- Front Controller, View Helper, Composite View, Business Delegate, Service Locator, Session Facade, BO, VO, DAO.
Q7) What are the design patterns in Java?
Design patterns are a set of rules that are given as the best solutions for reoccurring problems of application development. The design patterns are the best practices to develop software applications effectively. Design patterns are 3 parts of rules having relation “A context, a problem, and a solution”. Some regularly used design patterns in Java are:- Singleton, Factory, Front Controller, and e.t.c.
Q8) What is the best design pattern in Java?
Different design patterns in Java are used in different situations. But the most common are Singleton, Factory, Builder, Strategy, Template, FlyWeight, Proxy, Front Controller, View Helper, Composite View.
Q9) What are the 23 design patterns?
There are a total of 23 GOF design patterns. They are categorized into three types.
a) Creational Patterns:- Singleton, Factory, Abstract Factory, Prototype, Builder.
b) Structural Patterns:- Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy.
c) Behavioral Patterns:- Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Momento, Observer, State, Strategy, Template, Visitor.
Q10) What is a pattern?
A pattern is an idea of how to solve a problem in some classes.
Q11) What is an anti-pattern?
An anti-pattern is an idea of how not to solve a problem in some classes because implementing that idea would result in bad design.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!