Last active
July 19, 2018 07:10
-
-
Save jainnancy/d43055c12956641463d252abc99d87cf to your computer and use it in GitHub Desktop.
Decorator design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BasePizza extends Topping { | |
def getName() : String = "Pizza" | |
def getPrice() : Double = 77.0 | |
def addTopping() : Topping = this | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheeseTopping(override val topping : Topping) extends ToppingDecorator(topping) { | |
override def getPrice() : Double = { | |
super.getPrice() + 59.0 | |
} | |
override def getName() : String = { | |
val previous = super.getName() | |
"Ocean Cheese " + previous | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnionTopping(override val topping : Topping) extends ToppingDecorator(topping) { | |
override def getPrice() : Double = { | |
super.getPrice() + 39.0 | |
} | |
override def getName() : String = { | |
val previous = super.getName() | |
"Sprinkled Onion " + previous | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Pizza { | |
var topping : Topping = new BasePizza | |
def getPrice() : Double = { | |
topping.getPrice() | |
} | |
def getName() : String = { | |
topping.getName() | |
} | |
def addNewTopping(toppingName : String) : Boolean = { | |
toppingName match | |
{ | |
case "Onion" => | |
{ | |
this.topping = new OnionTopping(topping) | |
true | |
} | |
case "Cheese" => | |
{ | |
this.topping = new CheeseTopping(topping) | |
true | |
} | |
case _ => | |
println("Topping unavailable! Better luck next time! :(") | |
false | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object PizzaStore extends App { | |
val pizza = new Pizza | |
pizza.addNewTopping("Cheese") | |
pizza.addNewTopping("Onion") | |
println(s"You have ordered ${pizza.getName}") | |
println(s"You have to pay Rupees ${pizza.getPrice}") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Topping { | |
def getName() : String | |
def getPrice() : Double | |
def addTopping() : Topping | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ToppingDecorator(val topping : Topping) extends Topping { | |
var nextTopping : Topping = topping | |
def getName() : String = nextTopping.getName() | |
def getPrice() : Double = nextTopping.getPrice() | |
def addTopping() : Topping = this | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment