The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello dear friends. In this video, we will examine the Template Design Pattern, let's begin. Have you faced any situation in your programming, where you needed to find a set of specific steps every time with a fixed order, so that this can be enforced by implementation classes?
If so, then the template method pattern is the solution for you, which can be used to enforce these steps algorithm in an orderly manner. In subject-oriented programming, the template method is one of the behavioral design patterns identified by GoF in the book 'Design Patterns.' The template method is a method in a super class, usually an abstract super class, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method. In template pattern, an abstract class exposes defined ways/templates to execute its methods.
Its subclasses can override the method implementation as per need, but the invocation is to be in the same way as defined by an abstract class. This pattern comes under the behavior pattern category. The template method pattern is a very common technique for reusing code. Template method design pattern is to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm are preserved by the parent class. Template means preset format like HTML templates will have a fixed preset format. Similarly, in the template method pattern, we have a preset structure method called template method, which consists of steps. These steps can be an abstract method that will be implemented by its subclasses.
This design pattern is one of the easiest to understand and implement. This design pattern is used frequently in framework development, and also helps to avoid code duplication. The template method patterns intent is to define the skeleton of a function in an operation, deferring some steps to its subclasses. The template method let's subclasses redefine certain steps of a function without changing the structure of the function. The HTTP servIet does this in the servlet API. As you see in this UML, the strategy pattern has two participants. Abstract class contains the template method which should be made final, so that it cannot be overridden. This template method makes use of other operations available in order to run the algorithm, but is decoupled for the actual implementation of these methods. All operations used by this template method are made abstract, but their implementation is deferred to subclasses. Concrete class implements all the operations required by the template method that were defined as abstract in the parent class. There can be many different concrete classes.
So, let's understand this pattern with an example. Suppose we want to provide an algorithm to build a house. The steps that need to be taken to build a house are building foundation, building pillars, building walls, and windows. The important point is that we can't change the order of execution, because we can't build windows before building the foundation. So, in this case we can create a template method that will use different methods to build the house. When we have predefined steps to achieve some algorithm or when we want to avoid duplicating code, we can use the template design pattern. So, now I think an example will help this be more understandable. So, let's begin. First, create a new project and name it 'Template.' In our example code, we will try and build houses with this pattern. Let's create an abstract class template and name it HouseTemplate. We need a method to build a house.
In this method, we will define our build order. We need to make the template method final so that subclasses don't override it. Let's define our operations. Windows, now the foundation in windows will be in each house a standard so we can define operations. Walls, pillars, and last one is foundation. Now let's call them in the build method, Done, now we will define our house types, wooden house. As you remember, we haven't defined the operations of the pillar and wall methods. Let's override these methods for the wooden type. Let's continue with the GlassHouse. If you want, we can add another house type also. Okay, the structure is ready. Let's test the structure. GlassHouse, now we will call the build method. Change house type. Call build again, okay, done. Let's run and look at the result. As you can see, our houses were built step by step. We have now finished the template design pattern. With this video, we have finished examining behavioral patterns. In the following video, we'll start looking at an architectural pattern known as MVC. So, I'll see you in the following video my friends.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.