Decorator pattern
This is an example for implementation for decorator patter
Note: this is an example od decorator patterns, its will be a series for implement all patterns in java / python / go
in the next classes you can see the implementation for apply the decorator patter and how in the MainClass you can see the flexibility for use this strcuture in the problem for OOP on Stabuzz coffe
abstraccions
public abstract class AbstactBeverage {
protected abstract String getDescription();
protected abstract Double cost();
}
public abstract class AbstractCondiment extends AbstactBeverage {
}
Usage
public class Blacktea extends AbstactBeverage {
@Override
protected String getDescription() {
return "???";
}
@Override
protected Double cost() {
return new Double(10);
}
}
public class Coffeetea extends AbstactBeverage {
@Override
protected String getDescription() {
return "????";
}
@Override
protected Double cost() {
return new Double(2000);
}
}
public class Greentea extends AbstactBeverage {
@Override
protected String getDescription() {
return "???";
}
@Override
protected Double cost() {
return new Double(200);
}
}
public class Lemon extends AbstractCondiment {
AbstactBeverage abstactBeverage;
public Lemon(AbstactBeverage abstactBeverage) {
this.abstactBeverage= abstactBeverage;
}
@Override
protected String getDescription() {
return abstactBeverage.getDescription()+"??????";
}
@Override
protected Double cost() {
return abstactBeverage.cost()+11;
}
}
public class MainClass {
public static void main(String[] args) {
Blacktea blacktea = new Blacktea();
System.out.println("???=="+blacktea.getDescription()+",???"+blacktea.cost());
Lemon lemon = new Lemon(blacktea);
System.out.println("???????????=="+lemon.getDescription()+",???"+lemon.cost());
}
}