The Template Method Pattern :
"Defines the skeleton of an algorithm in a method,defering some steps to subclasses.Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure"
Principle used : The Hollywood Principle i.e "Don't call us,we'll call you" (ha ha "never")
Here High level components determine "when" lower level components are needed and how they are used, but letting low level components to hook themselves into the system.
- head first design patterns book.
http://www.oodesign.com/template-pattern.html
Couple of months back I was annoyed by the dailymail article about how men look at women.
For your information this is the article.
What are you staring at? Scientists reveal how our brain sees men as people, but women as body parts (and it's not just men who do it)
Read more: http://www.dailymail.co.uk/sciencetech/article-2179164/Double-vision-How-brains-men-people-women-body-parts.html#ixzz2rgMM8Kr7.
Let us think of a project for the men to create perfect woman.Let "PerfectWoman" is an abstract class.
GarysPerfectWoman, PetersPerfectWoman are two concreate implementations.
public abstract class PerfectWoman{
// here you have some stuff to do. you need a media tackers,a graphic,a canvas ...., I only give general ideas it is not the full implementation
final void drawPerfectWomanImage(){
drawHeadImage();
drawUpperBodyImage();
drawLowerBodyImage();
drawLegImage();
drawString();//this is the hook which does nothing.
}
abstract void drawUpperBodyImage();
abstract void drawLowerBodyImage();
final void drawHeadImage(){
g. setImage("kimkardashianheadImage.jpg" ,20.00,20.00,c);/** you never know whose head in that jpg**/
}
final void drawLegImage(){
g. setImage("JenniferLopezLeg.jpg",20.0,400.00,c);/** but you never know whose legs are in that jpg**/
}
void drawString(){}/** does nothing here , you can name your own name or you can leave this as it is.**/
}
Concreate implementation:
public class GarysPerfectWoman extends PerfectWoman{
// constructors not shown
public void drawUpperBodyImage(){
g.setImage("kieraknightlyUpperbodyimage.jpg",20.0,100.0,c);/** like this you can set your own picture.**/
}
public void drawLowerBodyImage(){
g.setImage("jenniferLopezLowerBodyImage.jpg",20.0,200.0,c);
}
}
now some where in the tester
PerfectWoman garys = new GarysPerfectWoman();
garys. drawPerfectWomanImage();/** that draws a perfect woman for Gary , but Gary cannot change the head or leg.(we never let Gary to hurt the feelings of good women)**/
In this way high level components controls the flow of the program and holds some control over some parts of the program.
title:
"Defines the skeleton of an algorithm in a method,defering some steps to subclasses.Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure"
Principle used : The Hollywood Principle i.e "Don't call us,we'll call you" (ha ha "never")
Here High level components determine "when" lower level components are needed and how they are used, but letting low level components to hook themselves into the system.
- head first design patterns book.
http://www.oodesign.com/template-pattern.html
Couple of months back I was annoyed by the dailymail article about how men look at women.
For your information this is the article.
What are you staring at? Scientists reveal how our brain sees men as people, but women as body parts (and it's not just men who do it)
Read more: http://www.dailymail.co.uk/sciencetech/article-2179164/Double-vision-How-brains-men-people-women-body-parts.html#ixzz2rgMM8Kr7.
Let us think of a project for the men to create perfect woman.Let "PerfectWoman" is an abstract class.
GarysPerfectWoman, PetersPerfectWoman are two concreate implementations.
public abstract class PerfectWoman{
// here you have some stuff to do. you need a media tackers,a graphic,a canvas ...., I only give general ideas it is not the full implementation
final void drawPerfectWomanImage(){
drawHeadImage();
drawUpperBodyImage();
drawLowerBodyImage();
drawLegImage();
drawString();//this is the hook which does nothing.
}
abstract void drawUpperBodyImage();
abstract void drawLowerBodyImage();
final void drawHeadImage(){
g. setImage("kimkardashianheadImage.jpg" ,20.00,20.00,c);/** you never know whose head in that jpg**/
}
final void drawLegImage(){
g. setImage("JenniferLopezLeg.jpg",20.0,400.00,c);/** but you never know whose legs are in that jpg**/
}
void drawString(){}/** does nothing here , you can name your own name or you can leave this as it is.**/
}
Concreate implementation:
public class GarysPerfectWoman extends PerfectWoman{
// constructors not shown
public void drawUpperBodyImage(){
g.setImage("kieraknightlyUpperbodyimage.jpg",20.0,100.0,c);/** like this you can set your own picture.**/
}
public void drawLowerBodyImage(){
g.setImage("jenniferLopezLowerBodyImage.jpg",20.0,200.0,c);
}
}
now some where in the tester
PerfectWoman garys = new GarysPerfectWoman();
garys. drawPerfectWomanImage();/** that draws a perfect woman for Gary , but Gary cannot change the head or leg.(we never let Gary to hurt the feelings of good women)**/
In this way high level components controls the flow of the program and holds some control over some parts of the program.
title:
No comments:
Post a Comment