Template method Pattern and Spring Framework
Why Spring Uses Template Method Pattern?
As Spring technology configures, instantiates and maintains objects in IoC container as beans injects them into needed dependent beans.And Template Method Pattern is Mother of all Frameworks, and components in SpringFramework. Spring and its all Template components like JDBC Template, JMS Template are instantiating beans in the same way. Also let developer to extend them.
What is Template Method Pattern?
The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets programmer redefine certain steps of an algorithm without changing the algorithm's structure.If we declare a abstract class or framework it has to perform some operations, we declare all methods one method which call all the other methods to perform a respective operation.
Like if we have a class EmployeeRegister and which serves users registerEmployee() and to register employee we have to perform following three operations like
getEmployeeDetails()
generateNewRegNo()
registerNewEmployee().
These methods are abstract and let users to override. But we let the user to call only one method from outside registerEmployee().
EmployeeDO registerEmployee()
{
getEmployeeDetails();
generateNewRegNo();
registerNewEmployee();
}
So when implementing this abstraact class we have to override the these methods.
Finally this pattern let users to perform the registering also let him to override the methods.In simple words make a class "Open for Extension but closed for Modification"
like here we can extend how to do registration but not able to modify the steps.
What is Ioc?
" Do not call me ! I will call you ! ".
First we will see the problem of "Tight Coupling".class Customer()
{
private Address address;
private AccountDetails accountDetails;
Customer()
{
address = new Address();
accountDetails = new AccountDetails():
}
}
Here the problem is creation of instances of Address and AccountDetails.It is fully depends on Customer. So it is tightly coupled.
Now that we know the issue, let’s understand the solution. The solution definitely revolves around shifting the object creation control from the customer class to some one else. Solution comes with concept IoC it resolves the instantiating the objcets and looses the objects from other. So objects are instantiated individually at common place called IoC container and shared across beans though DI process.
And they are injected to the classes which calls them. Like in our case Address and AccountDetails are instantiated and injected to Customer instance this process called as Dependency Injection.
What are all the templates uses this Pattern?
As Spring let the users to extend its all components like JDBC Template, JMS Template, etc.,. uses this same method.They declares its abstract components, developers have to implement them by extending. He cant modify the steps but have to extend and override the methods.
Also Spring uses all instantiating are maintained as beans created at common place and injected to other beans.
1 comment:
Hi, I wrote an article on this design pattern too. You can find it here http://www.mgiglione.com/2018/08/13/template-method-design-pattern-using-spring/
Post a Comment