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

Executive Summary: Anthropic's Claude Mythos Model

  What Happened Anthropic announced  Claude Mythos Preview , a new AI model it describes as its most capable ever — and so powerful in cyber...