One of Java8 functional programming: Behavioral parameterization __div

Source: Internet
Author: User

One important feature that distinguishes JAVA8 from previous versions of Java is the style of functional programming.

So what is the functional style of programming?

Before you understand functional programming, let's take a look at the previous programming approach, which is imperative programming. For example, calculate the number of two numbers:

public static int Add (int a, int b) {return

a + b;

}

Imperative programming passes data, which is parameter A and parameter B, while functional programming passes behavior. For example, the "add" operation is an act, or an action.

Functional programming is to take "behavior" or "action" as a parameter, that is, behavior parameterization.

Before we explain the specific behavior parameterization, let's see why we need behavioral parameterization.

——————————————————————————

This is an entity domain that represents the transactions made by a company.

Importjava.util.Date;

Public Classtrade {

private intid;//transaction ID

privatestringcountry;//transaction

Date

of the country privatedatedate;//transaction Private doublemoney;//Transaction amount

privatestringtrader;//Trader public

Intgetid () {

returnid;

}

Public Voidsetid (IntID) {

this.id= id;

}

Publicstring Getcountry () {

returncountry;

}

Public Voidsetcountry (String country) {

this.country= country;

}

Publicdate getDate () {

returndate;

}

Public voidsetdate (date date) {

this.date= date;

}

Public Doublegetmoney () {

returnmoney;

}

Public Voidsetmoney (Doublemoney) {

this.money= money;

}

Publicstring Gettrader () {

returntrader;

}

Public Voidsettrader (String trader) {

this.trader= trader;

}

}

——————————————

Now there's a demand that your boss needs to sift out the deals that happen in the United States

To sift through the deals that happened in the United States.

Public staticlist Filtertradeinamerica (list trades) {

List List =newarraylist<> ();

for (Trade trade:trades) {

if ("America". Equals (Trade.getcountry ())) {

list.add (Trade);

}

}

returnlist;

}

——————————————

If your boss changes his mind now, he needs to find out what to do with the deal in the UK.

You may find it simple to change the method name to Filtertradeinuk and then change the IF statement.

But this can cause a lot of duplicate code, and the country is so much, you can't write every single one.

——————

Of course, people would think of such a way to pass the country as a parameter.

Passing the country as a parameter

Public staticlist filtertradebycountry (list trades, String country) {

list List =newarraylist<> ();

for (Trade trade:trades) {

if (Trade.getcountry (). Equals (country)) {

list.add (Trade);

}

}

returnlist;

}

——————————————————————

It seems to solve some of the problems, but the boss's demand is endless, he should want to know the transaction amount of large and small transactions. For example, a transaction with a trading volume greater than (W). You might have done that.

If necessary, you need to filter by the size of the transaction amount. Find a deal larger than 1 million

Public staticlist filtertradebycountry (list Trades,doublemoney) {

list List =newarraylist<> ();

for (Trade trade:trades) {

if (Trade.getmoney () >100) {

list.add (Trade);

}

}

returnlist;

}

————————————————————

That, while satisfying the needs, but there are huge problems, a lot of repetitive code, and even more troubling, there are a lot of important fields in a transaction, and if we do this filter on every property, then the code becomes cumbersome and the client gets even more trouble when it calls.

————————————————————
Behavioral parameterization

We now consider returning a Boolen value based on certain properties of the transaction, such as whether the transaction is in the United States, whether the transaction amount is greater than a value, and so on.

We refer to it as a predicate (predicate, a function that returns a Boolean value). Define an interface:

Public Interfacetradepredicate {

booleantest (Trade Trade);

}

Now let's Get the Filtertrade () method to accept a variety of behaviors, that is:

The filter method receives multiple behaviors. predicate objects encapsulate the trade conditions, and you can pass anything in.

Public staticlist Filtertrade (list trades, tradepredicate predicate) {

list List =newarraylist<> ();

for (Trade trade:trades) {

if (predicate.test (Trade)) {

list.add (Trade);

}

}

returnlist;

}

This is what you can do when you call in the Main method.

List Tradeinamerica =filtertrade (Trades,newtradepredicate () {

@Override public

booleantest (Trade Trade) { return

"America". Equals (Trade.getcountry ());

}

);

Analysis: Tradepredicate is a predicate, at this point we think of it as an act, we do not know exactly what it is until we actually use it. When we use it, we pass a behavior that is now using an anonymous inner class.

This is the behavioral parameterization, which is an important feature of functional programming.

But now it seems that the use of anonymous inner class code is still very annoying, rest assured that Java8 very good for us to solve the problem, that is the famous lambda expression. As for what a lambda expression is, what does it do, please read the next blog post.

——————————————

Key notes: From "Java8 combat"

Behavior parameterization: A method that receives multiple different behaviors as parameters and uses them internally to accomplish different behaviors.

Behavioral parameterization allows code to better adapt to changing needs and reduce future workloads.

————————————————————

Recommended in this "Java8 actual combat", this book tells the Java8 example is more obvious and understandable.

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.