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 the visibility of superclass methods, making code fragile, the 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. So the question here is
Should Intuition override Design principles?
Beverage:
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
}
}
EDIT: Adding Sugar
to existing structure. Only new code is shown.
public abstract class Beverage {
private Sugar sugar;
public Sugar getSugar() {
return sugar;
}
public void setSugar(Sugar sugar) {
this.sugar = sugar;
}
}
Coffee:
public abstract class Coffee extends Beverage {
public final void make(){
grindBeans();
takeShot();
frothMilk();
addSugar();
addCondiments();
}
public void addSugar(){
Sugar sugar = super.getSugar();
if(!(sugar instanceof NoSugar)){
System.out.println("adding " + sugar.getTeaspoon() + " teaspoon sugar");
}
}