Implementing the template design pattern

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
-GOF Design Pattern

We discussed the Template method design pattern in Chapter 3, Consideration of Structural and Behavioral Patterns. It is widely used, and comes under the structural design pattern of the GOF design pattern family. This pattern defines the outline or skeleton of an algorithm, and leaves the details to specific implementations later. This pattern hides away large amounts of boilerplate code. Spring provides many template classes, such as JdbcTemplate, JmsTemplate, RestTemplate, and WebServiceTemplate. Mostly, this pattern hides the low-level resource management as discussed earlier in the pizza example.

In the example, the process is ordering a pizza for home delivery from an online portal. The process followed by the pizza company has some fixed steps for each customer, like taking the order, preparing the pizza, adding the toppings according to the customer's specifications, and delivering it to the customer's address. We can add these steps, or define these steps to a specific algorithm. The system can then implement this algorithm accordingly.

Spring implements this pattern to access data from a database. In a database, or any other technology, there are some steps that are always common, such as establishing a connection to the database, handling transactions, handling exceptions, and some clean up actions which are required for each data access process. But there are also some steps which are not fixed, but depend on the application's requirement. It is the responsibility of the developer to define these steps. But spring allows us to separate the fixed and dynamic parts of the data-access process into different parts as templates and callbacks. All fixed steps come under the template, and dynamic custom steps come under callbacks. The following figure describes the two in detail:

As you can see in the preceding figure, all the fixed parts of the process for data access wraps to the template classes of the Spring Framework as open and close connection, open and close statements, handling exceptions, and managing resources. But the other steps like writing SQLs, declaring connection parameters, and so on are parts of the callbacks, and callbacks are handled by the developer.

Spring provides several implementations of the Template method design pattern such as JdbcTemplate, JmsTemplate, RestTemplate, and WebServiceTemplate, but in this chapter, I will explain only its implementation for the JDBC API as JdbcTemplate. There is another variant of JdbcTemplate-NamedParameterJdbcTemplate, which wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset