Customers can need any kind of coffee, we can adjust according to range. You might say, wait a minute, what if I just need coffee and milk?
This is going to be a little tricky:
def c = new Coffee()
def m = new Milk()
m.types = ['Coffee','Milk']
(c..m).each{
println it.description()
}
The trouble is that we can't use the default "types" value of the "Base" class, and we need to customize the value of "types", such as:
m.types = ['Coffe','Milk']
The results of the operation are:
Coffee
with milk
Here, you might also say, wait, the decorate model has good scalability, and does your coffee series with custom range classes have a good scalability?
We say, of course, that the coffee series implemented by the custom range class still has a good scalability. Let's take a look at the example.
For example, we want to add a coffee type based on the above example: Chocolate coffee. We can add the following classes directly:
package range;
class Chocolate extends Base{
def Chocolate()
{
this.type = 'Chocolate'
this.getIndex()
}
def description()
{
'with chocolate'
}
def price()
{
8
}
}
It's all very simple, nothing special. Now, we can use chocolate coffee:
def coffee = new Coffee()
def chocolate = new Chocolate()
chocolate.types = ['Coffee','Ice','Milk','Sugar','Chocolate']
(coffee..chocolate).each{
println it.description()
}
The results of the operation are:
Coffee
with ice
with milk
with sugar
with chocolate