0% found this document useful (0 votes)
16 views

Java Functional Programming

This document discusses when to use functions versus classes for delegation and decoration in Java. It states that functions are usually sufficient when the logic is simple and doesn't require complex state management, such as delegating a simple arithmetic operation. Classes are generally better when the logic is more complex or state needs to be managed, such as modifying an object's behavior by changing its internal state. The choice depends on the specific requirements of the application.

Uploaded by

shashank sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Functional Programming

This document discusses when to use functions versus classes for delegation and decoration in Java. It states that functions are usually sufficient when the logic is simple and doesn't require complex state management, such as delegating a simple arithmetic operation. Classes are generally better when the logic is more complex or state needs to be managed, such as modifying an object's behavior by changing its internal state. The choice depends on the specific requirements of the application.

Uploaded by

shashank sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Map function:

Filter function:

Reusing functions, higher order functions and difference between Functions


and Predicates
Map-Reduce Pattern – for example: find the longest word, shortest word,
sum of word lengths, average word length etc
Pure reduce:
Collecting elements of a stream:
Java compiler’s intelligent routing with method references
Delegate pattern in java (i.e. composition) when to use a function instead of
a class?
In the Java delegate pattern, a delegate is an object that delegates a task to another
object. The delegate pattern can be implemented using either functions or classes,
and the choice between them depends on the specific needs of the application.

Functions are usually used when the delegation is simple and doesn't require
complex logic or state management. For example, if you need to delegate a simple
arithmetic operation, like adding two numbers, a function would be a good choice. In
this case, you could define a function that takes two parameters and returns their
sum, and then delegate the operation to this function.

On the other hand, if the delegation requires more complex logic or state
management, classes are usually a better choice. For example, if you need to
delegate a task that involves multiple steps or requires the use of multiple objects, a
class would be more appropriate. In this case, you could define a class that
encapsulates the logic and state needed to perform the task, and then delegate the
task to an instance of this class.

In general, the choice between functions and classes in the delegate pattern depends
on the specific requirements of the application. If the delegation is simple and
doesn't require much logic or state management, functions can be used. If the
delegation is more complex and requires more logic or state management, classes
are a better choice.
Note : This is one way of converting static methods to functions that can be
injected into other classes ‘constructors. This allows us to use dependency
injection principle even with static methods, thereby improving the
testability and portability of the class(fewer dependencies).

Composing functions/decorator pattern using lambda expressions


In the Decorator pattern, a decorator is an object that wraps around
another object in order to modify its behavior. In Java, decorators can be
implemented using either functions or classes, and the choice between
them depends on the specific needs of the application.

Functions are usually used to implement the Decorator pattern when the
behavior modification is simple and doesn't require complex state
management or object instantiation. For example, if you need to add a
simple logging functionality to a method, a function can be defined that
wraps around the original method and logs the input and output.

Functions can also be used when the behavior modification is a simple


combination of existing functions. For example, if you need to concatenate
two strings, you could define a function that takes two strings as input and
returns their concatenation.

On the other hand, if the behavior modification requires more complex


logic or state management, classes are usually a better choice. For example,
if you need to modify the behavior of an object by adding or removing
some of its methods, or by changing its internal state, a class would be
more appropriate. In this case, you could define a decorator class that
wraps around the original object and modifies its behavior.
In general, the choice between functions and classes in the Decorator
pattern depends on the specific requirements of the application. If the
behavior modification is simple and doesn't require much logic or state
management, functions can be used. If the behavior modification is more
complex and requires more logic or state management, classes are a better
choice.

Garbage collection manages any objects that are created but


memory leaks often occur due to unclosed resources. The GC
doesn’t manage this and is why applications often run out of
memory,
My opinion : For now don’t worry about managing external
resources using lambda expressions. The automatic resource
management (ARM) added in Java 8 with the try(File f =..){} is best
suited for managing resources.

You might also like