Groovy Explore custom Range two custom range class with decorate mode (next)

Source: Internet
Author: User
Tags range

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.