Skip to content

Instantly share code, notes, and snippets.

@DavidMellul
Last active July 8, 2018 13:31
Show Gist options
  • Save DavidMellul/1e797d086a5ca69c7c3e1c6d6db3be4f to your computer and use it in GitHub Desktop.
Save DavidMellul/1e797d086a5ca69c7c3e1c6d6db3be4f to your computer and use it in GitHub Desktop.
class Pizza {
protected Pizza base;
Pizza() {}
Pizza(Pizza base) {
this.base = base;
}
public int cost() {
return 6; // Pizza dough with nothing on it will cost 6€
}
// Make System.out.println work like magic
public String toString() {
return this.cost() + "€";
}
}
class WithGarlic extends Pizza {
WithGarlic(Pizza base) {
super(base);
}
public int cost() {
return this.base.cost() + 1; // Additional 1€ for garlic
}
}
class WithOnions extends Pizza {
WithOnions(Pizza base) {
super(base);
}
public int cost() {
return this.base.cost() + 1; // Additional 1€ for onions
}
}
class WithPepperonis extends Pizza {
WithPepperonis(Pizza base) {
super(base);
}
public int cost() {
return this.base.cost() + 2; // Additional 2€ for pepperonis
}
}
public class PizzaBusiness {
public static void main(String[] args) {
Pizza custom = new Pizza();
custom = new WithGarlic(custom);
custom = new WithOnions(custom);
custom = new WithPepperonis(custom);
// We could add 10 times onions for onion lovers
// ....
System.out.println("This pizza costs: "+custom);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment