Wednesday 9 November 2011

Template Method Design Pattern

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();
    }
}

No comments:

Post a Comment

Azure OpenAI Architecture Patterns & Deployment Patterns

Sharing some useful links that will help customers architect Azure OpenAI solution using the best practices: (1) Azure OpenAI Landing Zone r...