Behavior parameterization In Java

Alok Kumar
Javarevisited
Published in
3 min readSep 14, 2020

The past isn’t here anymore, the future cannot be seen and the only thing permanent is the present moment and nothing else. Everything is the result of the change. There is nothing to question — change is the only constant in the life.

A well-known and most irritating problem in the software industry is that no matter what we do, customer requirements will always be changed. For example, suppose you are developing an application to help your customer to understand his Employee. Your customer might want an API in your application to find all the employees who are older than 50 years. But the next day he might tell you, “Actually I also want to find all employees who are Manager.” Two days later, the same customer comes back and adds something new like “It would be really great if I could find all employees who is Manager and older than 50 years.” How can you manage these changing requirements for application?

Behavior parameterization is a software development pattern that lets you how to handle frequent requirement changes. Behavior parameterization adds an ability to a method to receive multiple different behaviors as its parameter and use them internally to accomplish the task.

Let’s walk through an example and go through the Java code in the context of an EmployeeInfo application,

We have to implement an API to categorize employees who are from India.

  1. Naive and Simple Approach

But now the Customer changes his mind and wants to also get employees from other countries like the US, Germany, What can we do? The easiest solution would be to duplicate the above method getIndiaEmp, rename it as getUsemp(), and getGermanEmp(), But this approach doesn’t look good. Might be your customer has some other filter on the age.

2. Parameterizing the Address(Country)

What we can do is add a country as a parameter to the method to parameterize and be more flexible to such changes.

We can now make the customer happy and call our method as below
List<Employee> usEmp = getEmpByCountry(empList,”US”);
List<Employee> germanEmp = getEmpByCountry(empList,”GER”);
List<Employee> indiaEmp = getEmpByCountry(empList,”IN”);

Suppose the same customer comes back says, We need to differentiate employees by age as older(age>40) and younger(age<40).
A naive and simple solution is we can rename the above method getEmpByCountry() as getEmpByAge() with parameter employee list and age getEmpByAge(List<Employee>, age) but here most of codes are duplicate.

Now to eliminate the above problem like continuous requirement changes, a lot of duplicate codes, etc. We need to introduce a better level of abstraction.

3. Behavior parameterization using Different Strategies (Interface)

We can create an interface

We can create different type of predicates like

Now we can create a new method filterEmp(List<Employee>, EmployeePredicate) as below

We can use this method like this

List<Employee> usEmp = filterEmp(empList, new USEmpPredicate());

[Employee{name=’Kumar’, add=’US’, designation=’Manager’, age=45}, Employee{name=’Kavi’, add=’US’, designation=’Manager’, age=25}]

We can create a new predicate for an older employee who is from India as below.

We can use as below

List<Employee> olderIndianEmp = filterEmp(empList, new OlderIndianEmpPredicate());
[Employee{name=’Ram’, add=’IN’, designation=’Developer’, age=58}]

In this approach, We will have to write a lot of codes that introduces a lot of verbosities. To eliminate these verbosities we can use the java anonymous class.

4. Behavior parameterization using Anonymous classes

The following code shows how to rewrite the filtering example by creating an object that implements EmployeePredicate using an anonymous class.

5. Behavior parameterization using Lambda expression

How can we write the above code using the lambda expression.

List<Employee> olderIndianEmp_lambdaExp = filterEmp(empList, (Employee emp)-> emp.getAge()>50 && emp.getCountry().equals("IN"));

Now We can see that behavior parameterization is a useful pattern to easily adapt to changing requirements. This pattern lets you encapsulate a behavior (a piece of code) and parameterize the behavior of methods bypassing and using these behaviors you create.

Behavior parameterization is the ability of a method to take multiple different behaviors as parameters and use them internally to accomplish different behaviors. Behavior parameterization lets you make your code more adaptive to changing requirements and saves on engineering efforts in the future.

This is all about Behavior parameterization in java. I know this is not complete. I have just given introductory information and the implementation of Behavior parameterization. If you like this information please go through that and please share your knowledge and understanding through comment, suggestion.

--

--