The closure of groovy exploration is a

Source: Internet
Author: User
Tags anonymous closure trim

The closure of dynamic languages is an eternal topic. The convenience and quickness of closures in the coding process make the advocates of the dynamic language relish it, while static languages, especially the Java language fans, will come up with anonymous inner classes that the Java language has similar functions. I've been using the Java language for six or seven years now, and I really like the Java language as well, but I never dared to compare it to the anonymous inner class and closure of the Java language. Because the anonymous internal class of vague and cumbersome, so that I am in the process of coding rarely used it, how dare to take it out to brag about it? And in less than a year of using groovy coding, I used closures all the while.

Closures are so convenient and flexible that I use them easily. In my opinion, closures are like groovy and other dynamic languages that give us a shark-fin feast. The benefits of this feast need us to slowly one by one, remember star Ye's "any" has a line: A bowl of shark's fin cough mouth. Well, today we're going to start by saying that closures can be used anywhere in the groovy coding process, with the right to use the closure of this bowl of shark's fin to rinse the mouth.

Warm tips, read this article requires you to have the basics of closure. For example, the definition and invocation of closures, the scope of parameters, variables, and return values for closures are some simple basics. Of course, you can also try to understand and support each other by looking at the same basic knowledge as you read this article. The book that is most clearly articulated on the closure base is the Groovy in action

A A little test, anywhere.

In the Java language coding process, there are everywhere we use to feel awkward place, but not at all. For example, we often have to do a non-empty judgment on a series of strings before doing so:

if(str1!=null&&!str1.trim().equals(“”))
{
……
}
if(str2!=null&&!str2.trim().equals(“”))
{
……
}
if(str3!=null&&!str3.trim().equals(“”))
{
……
}
……

Such repeated if judgment is unbearable, but you do not have a good solution, can only be so five, six or even 10 write down.

stop! Let's consider the closure solution.

Here, we first define a closure, which is very simple.

def isNotNull = {
str,Closure closure ->
if(str!=null&&!str.trim().equals(""))
{
closure.call()
}
}

A very common closure, two input parameters, if not understood, now turn out the basics of closure to see.

Once the simple closure is defined, we can start using it:

def abc = 'abc'
isNotNull(abc)
{
println abc
}

In the example above, we had a little test, just to determine if the string ABC is empty, and if it is not empty, print to the console. It's much simpler than the if language above us.

Like this, we are everywhere in the coding process, and here's an example:

String cateType = getRequest().getParameter("cateType");//1: 确认存储 2:反确认
String cateId = getRequest().getParameter("cateId");
String year = getRequest().getParameter("year");
String percent = getRequest().getParameter("percent");

Yes, this is a section of code that gets the request parameter. We write such code, when more will be depressed, I always first to getrequest, and then GetParameter, repeat the same exercise our blind ability.

stop! Let's use closures to ease the frequency of our typing.

Again, first define a closure:

def getReqParam = {
paramName ->
return getRequest().getParameter(paramName)
}

So, the code above, we can write the following:

String cateType = getReqParam("cateType");//1: 确认存储 2:反确认
String cateId = getReqParam("cateId");
String year = getReqParam("year");
String percent = getReqParam("percent");

What, is it much simpler?

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.