Template Design Pattern in java , Spring
-->
What is Template Pattern
– Template pattern is about defining the blueprint of the algorithm wherein some of the steps of the implemented in base class and others are deferred for subclass to implement. Spring framework implemented this pattern for many features like, JmsTemplate, JDBCTemplate.
Example:
< bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate"
scope="prototype" >
<eq bean >
Let's understand Template Design Pattern form core java perspective :
public abstract class ProjectManager {
public final void collectRequirement(){
System.out.println("requirements are gathered same way in all technology projects. So this could be implemented in baseclass itself and otehr classes can inherit it as it is.");
}
public abstract void developDesign();
public abstract void developCode();
public final void assessQuality(){
System.out.println("Quality is assessed in same way in all technology projects. So this could be implemented in baseclass itself and otehr classes can inherit it as it is.");
}
public abstract void deploy();
}
ProjectManager is abstract class. It has both abstract and implemented methods. Generic methods are implemented here while specific methods are kept abstract for implementing class to define them
Generic method are kept final So that they are not overriden and logic defined in them is used as it is in all implementations.
Generic method are kept final So that they are not overriden and logic defined in them is used as it is in all implementations.
class JavaProjectManager extends ProjectManager {
@Override
public void developDesign() {
System.out.println("Desiging is done here in JAVA way");
// TODO Auto-generated method stub
}
@Override
public void developCode() {
System.out.println("Coding is done here in JAVA way");
// TODO Auto-generated method stub
}
@Override
public void deploy() {
System.out.println("Deployment is done here in JAVA way");
// TODO Auto-generated method stub
}
}
JavaProjectManager is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to JavaProjectManager class functional requirement
class MicroSoftProjectManager extends ProjectManager {
@Override
public void developDesign() {
System.out.println("Desiging is done here in MicroSoft technology way");
// TODO Auto-generated method stub
}
@Override
public void developCode() {
System.out.println("Coding is done here in MicroSoft technology way");
// TODO Auto-generated method stub
}
@Override
public void deploy() {
System.out.println("Deployment is done here in MicroSoft technology way");
// TODO Auto-generated method stub
}
}
Similarly MicroSoftProjectManager is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to MicroSoftProjectManager class functional requirement
class mainFramesProjectManager extends ProjectManager {
@Override
public void developDesign() {
System.out.println("Desiging is done here in mainFrames technology way");
// TODO Auto-generated method stub
}
@Override
public void developCode() {
System.out.println("Coding is done here in mainFrames technology way");
// TODO Auto-generated method stub
}
@Override
public void deploy() {
System.out.println("Deployment is done here in mainFrames technology way");
// TODO Auto-generated method stub
}
}
Similarly mainFramesProjectManager is implementation of ProjectManager class. It has defined all the specific methods as per the logic spefic to MicroSoftProjectManager class functional requirement.
Conclusion:
So template method is all about defining the execution steps in a contract , provide implementation for all generic methods in contract itself and let the implementation class implement the specific methods. But template or blueprint for all high level steps is covered in abstract class only irrespective of operation is generic or specific.
No comments:
Post a Comment