- Interface
Pizza
ThePizza
interface defines basic properties of a pizza.
interface Pizza {
//...
}
//Provides description of a pizza
fun Pizza.description() : String {
//...
}
- A simple Farm House pizza can be created using
class FarmHouse
.
class FarmHouse : Pizza {
//...
}
val pizza = FarmHouse()
println(pizza.description()) //Prints ..
//A Farm House pizza with Regular base, topped with Onion, Tomato, Mushroom and priced at 200.0
- A simple pizza can be customized using decorators.
- To use different base for any pizza, wrap the pizza object in a
Dough
decorator.//Simple Farm House pizza val pizza: Pizza = FarmHouse() //Cheese Burst Farm House pizza - costs additional 75 val cheeseBurstPizza: Pizza = Dough(pizza = pizza, dough = "Cheese Burst", cost = 75.0) println(cheeseBurstPizza.description()) //Prints ... //A Farm House pizza with Cheese Burst base, topped with Onion, Tomato, Mushroom and priced at 275.0
- To add a topping, wrap the pizza object in a
Topping
decorator. With each topping price increases by 20.//Simple Farm House pizza val pizza: Pizza = FarmHouse() //Farm House pizza with Golden corn - costs additional 20 val farmHouseWithCorn: Pizza = Topping(pizza = pizza, type = "Golden Corn") println(farmHouseWithCorn.description()) //Prints ... //A Farm House pizza with Regular base, topped with Onion, Tomato, Mushroom, Golden Corn and priced at 220.0
val pizza = Dough( dough = "Cheese Burst", cost = 75.0, pizza = Topping( type = "Golden corn", pizza = FarmHouse() ) )
- To use different base for any pizza, wrap the pizza object in a
With the help of DSL, one should be able to make a pizza as shown in code below.
val pizza: Pizza = CheeseBurst Farmhouse pizza {
topped with "Golden corn"
}
println(pizza.description()) //Prints ...
//A Farmhouse pizza with Cheese Burst base, topped with Onion, Tomato, Mushroom, Golden corn and priced at 295.0
Note:
- You cannot change any code in package
com.thoughtworks.workshop.kotlin.pizza
. - Implement your Dsl in Dsl.kt file.
- Uncomment sample dsl usage in
main
function.
-
Allow user to make pizza without additional toppings
val pizza: Pizza = CheeseBurst Farmhouse pizza
-
Allow user to make pizza with regular dough
val pizza: Pizza = Just Farmhouse pizza
-
Make DSL extendable to support different dough types such as Thin crust and basic pizzas like Pepperoni.