dimanche 20 septembre 2015

How to choose Inheritance or Composition for adding behavior

I have a class hierarchy like this Beverage -> Coffee-> Latte. Where Beverage is the abstract superclass being extended by Coffee. Coffee class then adds some behavior but is also abstract. Latte extends Coffee class and is a concrete class. I have used inheritance to add behaviors here. And inheritance do have drawbacks like visibility of superclass methods, making code fragile, code is tightly coupled. So, programming principles dictate Composition should be preferred over Inheritance. But in this case inheritance feels so natural as Latte is a type of Coffee and Coffee is a type of Beverage that using composition to add behavior feels wrong in spite of its benefits. Kindly share your views and what is the right way to deal with this. thanks.

public abstract class Beverage {

    private final String name;
    private final double price;

    Beverage(String name, double price){
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }
    public double getPrice() {
        return price;
    }

    public abstract void make();
    }

Coffee:

public abstract class Coffee extends Beverage {

    public Coffee(String name, double price) {
        super(name, price);
    }


    public final void make(){
        grindBeans();
        takeShot();
        frothMilk();
        addCondiments();
    }

    public void grindBeans(){
        System.out.println("Grinding Beans...");
    }

    public void takeShot(){
        System.out.println("Taking Shot....");
    }

    public abstract void frothMilk();
    public abstract void addCondiments();


}

Latte:

public class Latte extends Coffee {

    public Latte() {
        super("Latte", 4.0);
    }

    @Override
    public void frothMilk() {
        System.out.println("Frothing milk to create micro foam");

    }

    @Override
    public void addCondiments() {
        // TODO Auto-generated method stub

    }

}

Aucun commentaire:

Enregistrer un commentaire