What is Java Behaviour Parameterization?

Image Credits: Pexels

Behaviour parameterization is a technique used to improve the ability of a code to face changing needs. It is done by making a function able to take a parameter with a changing behaviour.

Let’s take an example

Suppose you have to make a library management software. Now, you may need to create an object called Book.

You can add get() and set() methods for each variable.

Now, you will be given a list of all the books in the library as an ArrayList of Book objects. From them, you are required to filter out all Electronics books. What would you do? 🙂

It’s easy. Let’s make a method with a loop inside a class called Library.

Method call:

here, in the method call, bookList is the list of all books in the library and the filteredList is the list of all books under electronics category.

The method filter() takes an ArrayList of Book objects and the category that we want to filter. The implementation of the method contains a for loop where for each Book in the unfiltered list, the computer checks whether the Book is of category “electronics”. If so, it is added to a new ArrayList called filterdBooks. In the end, this filteredBooks ArrayList is returned.

Next, you are asked to filter the books by the edition. The method you are going to write may take an integer to represent the edition. The method should return an ArrayList of all Books available in that edition.

This method is also quite similar to our previous method. Hope you can fill the code inside 🙂 The method would be as follows:

Now, what if you want to filter the books by the author. You have to get a list of books written by a certain author.

Method would be like:

What if you have to filter the books of an author with the same edition? Now the method is like:

Well, each time we need a different requirement, we are writing a new method! There are lots of repetitions here. Plus, we are increasing the number of parameters. See, in the previous method, we have 3 parameters. So, the public interface of our Library class would be complicated. The users of this class should need to know the meaning of each parameter.
Also, it would be so hard to deal with the changing requirements because the code is not flexible. So, what we should do?

Can we pass a method as a parameter?

Let’s deviate from our main discussion for a bit. So, can we pass a method as a parameter to another method? In Java, we can do this in an indirect way. We know a method can take an object as a parameter. So, we can create an object first. Inside that object, we can have our required method.

Back to our main discussion. Here comes the behaviour parameterization. We are going to apply the above technique of passing a method as a parameter. Behaviour parameterization avoids the above discussed problems and create a flexible code. Let’s see.

First, we can create an interface.

As you can see, this interface has a method called test() which requires a Book object as the parameter.
Then, we can start writing a custom class for our requirement. This class is written for a method of filtering the books by category.

Now, we can have our main book filtering method as follows:

This method can be used as:

What did we just do?

OK. Let’s see what happens here.

  • The method filterTheBooks() requests an object implementing BookFilter interface.
  • That object definitely has a method called test() to take a Book object and do whatever a test with that Book and return a boolean true or false value. We don’t care the implementation of the test() method. What we only know is the method returns either true or false.
  • If the return value is true, it implies that the test is passed so, we can add the Book object in to the filteredBooks ArrayList.

So, you can see, the filterTheBooks() method can have different behaviours according to the BookFilter parameter.

We can customize the test() method of BookFilter object as we wish. But, see, we don’t have to write many repetitive ugly methods each time we need to achieve different goal.

So, the user interface of the Library Class would be cleaner and concise. One filter method. Only 2 parameters. Many behaviours of the same method can be achieved with different BookFilter objects.

This is behaviour parameterization. It is very flexible.

The behaviour of the method changes according to the object implementing BookFilter

If you want a filtered list according to an author, now you don’t have to create a separate filter method for that.

Just create a new BookFilter object for the author and pass it as a parameter.

Still lots of code?

If you need to filter only once or few times, you no need to waste the time by creating a new Class implementing the BookFilter interface. Just make an anonymous inner class for the parameter.

Method call:

If you find this still verbose, you can have a Lambda Expression introduced in Java 8.

Method call:

So, behaviour parameterization makes our lives easier 🙂

Thanks for reading the post guys. If you enjoyed the post, I’d be really grateful if you’d share it!