Types of Design Patterns

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.

  1. GoF Design Patterns:- The GoF design patterns deal with the reoccurring problems of OOP.
  2. 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,

  1. Creational Patterns:- Concern the process of object creation. It talks about object creation-related problems and solutions.
  2. 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.
  3. 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:-

  1. Singleton Pattern
  2. Factory Pattern
  3. Abstract Factory Pattern
  4. Prototype Pattern
  5. 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:-

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. FlyWeight Pattern
  7. 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:

  1. Chain of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Momento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Pattern
  11. 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,

  1. Client Tier:- Application clients, applets and e.t.c.
  2. Presentation Tier:- JSP, Servlet and e.t.c.
  3. Business Tier:- RMI, EJB’s, and other business objects.
  4. Integration Tier:- JMS, JDBC, Connectors and e.t.c.
  5. 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,

  1. Presentation Tier Patterns
  2. Business Tier Patterns
  3. 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,

  1. Front Controller
  2. Application Controller
  3. Intercepting Filter
  4. Context Object
  5. View Helper
  6. Composite View
  7. Service toWorker
  8. Dispathcer View

2.2) Business Tier Patterns

This tier provides the business service required by the application clients. The business tier design patterns are:-

  1. Business Delegate
  2. Service Locator
  3. Session Facade
  4. Application Service
  5. Business Object
  6. Composite Entity
  7. Transfer Object
  8. Transfer Object Assembler
  9. 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:-

  1. Data Access Object (DAO)
  2. Service Activator
  3. Domain Store
  4. 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,

  1. Singleton:- In any situation it allows JVM to create only one object.
  2. Factory:- Provides abstraction on the object creation process.
  3. Factory Method:- Provides the set of rules and guidelines for multiple factories when they are creating objects for the same family classes.
  4. 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.
  5. Template:- The class that contains the method of automating the sequence of operations to complete the task has the flexibility of customizing certain operations.
  6. Builder:- Allows to creation the complex object as the combination of multiple sub-objects having reusability of sub-objects.
  7. Decorator/Wrapper:- Useful to decorate the object with additional facilities without using inheritance.
  8. Adapter:- To make the class of one module compatible to work with another module class we need to use the adaptor.
  9. 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,

  1. Front Controller:- Entry and exit point for all requests having mandatory system services.
  2. Composite View:- Recommended to take common logics (Header and footer logics) in separate web components and to include their outputs in main web components.
  3. Service Locator:- The class that contains lookup code to get the reference of external service component and maintains cache having to get reusability.
  4. 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.
  5. Session Facade:- The special server-side external component talks to multiple other external components to complete the task.
  6. Message Facade:- Same as the session facade but gives Asynchronous communication.
  7. Data Access Objects (DAO):- The class that separates persistence logic from other logic.
  8. 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!

Leave a Comment

Your email address will not be published. Required fields are marked *