Previously, we discussed what is design patterns, GoF, and J2EE patterns. See here:- Design Patterns. Now, let us see the different types of design patterns used in Java.
There are many pattern catalogs are available but mainly two of them are very popular.
- GoF Design Patterns:- The GoF design patterns deal with the reoccurring problems of OOP.
- J2EE Design Patterns:- It gives problem and solution related to JEE (Java Platform Enterprise Edition) based layered application development.
1. Types of GOF Design Patterns
The GoF design patterns are classified into three categories,
- Creational Patterns:- Concern the process of object creation. It talks about object creation-related problems and solutions.
- Structural Patterns:- Deal with the composition of classes and objects. It gives problems and solutions related to how to create complex objects by combining multiple small objects.
- Behavioral Patterns:- Deal with the interaction of classes and objects. It gives the problems and solutions related to communication between classes, and communication between objects.
1.1) Creational Patterns
The creational design patterns are the design patterns that deal with object creation mechanisms i.e. how an object can be created. It gives solutions to create objects in a manner suitable to the situation. The basic form of object creation (using the “new” keyword) could result in design problems or added complexity to the design. Creational design patterns solve those problems by somehow controlling this object creation.
All the creational patterns define the best possible way in which an object can be instantiated. These describe the best way to create object instances. According to GOF, the creational patterns can be categorized into five types:-
- Singleton Pattern
- Factory Pattern
- Abstract Factory Pattern
- Prototype Pattern
- Builder Pattern and e.t.c.
1.2) Structural Patterns
Structural design patterns are design patterns that ease the design by identifying a simple way to realize the relationship between entities. Structural patterns describe how objects and classes can be combined to form larger structures. According to GOF, the structural pattern can be realized in the following patterns:-
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- FlyWeight Pattern
- Proxy Pattern and e.t.c.
1.3) Behavioral Patterns
Behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication i.e. prescribes the way objects interact with each other. They help make complex behavior manageable by specifying the responsibilities of objects and the ways they communicate with each other. The 11 behavioral design patterns are:
- Chain of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Momento Pattern
- Observer Pattern
- State Pattern
- Strategy Pattern
- Template Pattern
- Visitor Pattern
2. J2EE/JEE Design Patterns and Their types
J2EE plateform/application are multitiered system. We view the system in terms of tiers. A tier is a logical partition of related concerns. Tiers are used for the separation of concerns. Each tier handles its unique responsibility in the system. Each tier is a logical separation and has a minimal dependency on other tiers.
A typical JEE application contains 5 layers,
- Client Tier:- Application clients, applets and e.t.c.
- Presentation Tier:- JSP, Servlet and e.t.c.
- Business Tier:- RMI, EJB’s, and other business objects.
- Integration Tier:- JMS, JDBC, Connectors and e.t.c.
- Resource Tier:- Database, external systems, and e.t.c.
In JEE pattern catalog based on the above-given layers, we have 3 categories of Design patterns,
- Presentation Tier Patterns
- Business Tier Patterns
- Integration Tier Patterns
2.1) Presentation Tier Patterns
This tier contains all the presentation tier logic required to service the clients that access the system. The presentation tier design patterns are,
- Front Controller
- Application Controller
- Intercepting Filter
- Context Object
- View Helper
- Composite View
- Service toWorker
- Dispathcer View
2.2) Business Tier Patterns
This tier provides the business service required by the application clients. The business tier design patterns are:-
- Business Delegate
- Service Locator
- Session Facade
- Application Service
- Business Object
- Composite Entity
- Transfer Object
- Transfer Object Assembler
- Value List Handler
2.3) Integration Tier Patterns
This tier is responsible for communicating with external resources and systems such as data stores e.t.c. The integration tier design patterns are:-
- Data Access Object (DAO)
- Service Activator
- Domain Store
- Web Service Broker
Regularly used Design Patterns
Among these all design patterns, some patterns are quite regularly used design patterns in projects.
Regularly used GoF patterns and their uses,
- Singleton:- In any situation it allows JVM to create only one object.
- Factory:- Provides abstraction on the object creation process.
- Factory Method:- Provides the set of rules and guidelines for multiple factories when they are creating objects for the same family classes.
- Abstract Factory:- It is a super factory or factory of factories. When we are working with multiple factories that create objects for different family classes, to make sure that all objects are created through the same factory go for the abstract factory.
- Template:- The class that contains the method of automating the sequence of operations to complete the task has the flexibility of customizing certain operations.
- Builder:- Allows to creation the complex object as the combination of multiple sub-objects having reusability of sub-objects.
- Decorator/Wrapper:- Useful to decorate the object with additional facilities without using inheritance.
- Adapter:- To make the class of one module compatible to work with another module class we need to use the adaptor.
- FlyWeight:- It makes the programmer utilize minimum objects for maximum utilization by identifying intrinsic (sharable) state and extrinsic state.
Regularly used J2EE patterns and their uses,
- Front Controller:- Entry and exit point for all requests having mandatory system services.
- Composite View:- Recommended to take common logics (Header and footer logics) in separate web components and to include their outputs in main web components.
- Service Locator:- The class that contains lookup code to get the reference of external service component and maintains cache having to get reusability.
- Business Delegate:- Helps the controller servlet to convert VO to BO, translate technology-specific exceptions into application-specific exceptions. And used to perform transaction management.
- Session Facade:- The special server-side external component talks to multiple other external components to complete the task.
- Message Facade:- Same as the session facade but gives Asynchronous communication.
- Data Access Objects (DAO):- The class that separates persistence logic from other logic.
- Intercepting Filter:- Web components have optional services that can be enabled disabled dynamically.
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!