abstract class Fruit {
// Is the implementation of a template method which will be used in all the subclasses.
// This pattern is implemented by a Implementation Inheritance.
public final void eat(){
System.out.println("I am eating :" + this.getClass().getName());
}
abstract void cut();
}
class Apple extends Fruit{
@Override
void cut() {
System.out.println("This is how we cut : " + this.getClass().getName());
}
}
class Banana extends Fruit{
@Override
void cut() {
System.out.println("This is how we cut : " + this.getClass().getName());
}
}
public class TemplateMethodPattern {
public static void main(String args[]){
Fruit apple = new Apple();
apple.eat();
apple.cut();
Fruit banana = new Banana();
banana.eat();
banana.cut();
}
}
// Is the implementation of a template method which will be used in all the subclasses.
// This pattern is implemented by a Implementation Inheritance.
public final void eat(){
System.out.println("I am eating :" + this.getClass().getName());
}
abstract void cut();
}
class Apple extends Fruit{
@Override
void cut() {
System.out.println("This is how we cut : " + this.getClass().getName());
}
}
class Banana extends Fruit{
@Override
void cut() {
System.out.println("This is how we cut : " + this.getClass().getName());
}
}
public class TemplateMethodPattern {
public static void main(String args[]){
Fruit apple = new Apple();
apple.eat();
apple.cut();
Fruit banana = new Banana();
banana.eat();
banana.cut();
}
}
No comments:
Post a Comment