<h2 class="wp-block-heading"><span style=", Helvetica, Arial, sans-serif;font-size: var(--artdeco-reset-typography_getFontSize)">Strategy Design Pattern</span>s</h2>



<p class="wp-block-paragraph">We can easily create a strategy design pattern using lambda. </p>



<p class="wp-block-paragraph">To implement this pattern we will be using Interfaces and classes. The interface declares a behaviour and concrete classes define the behaviour.</p>



<p class="wp-block-paragraph">If we want to introduce a new behaviour to the design, we need to add a new concrete class. So this implementation ends up with tons of concrete classes.</p>



<p class="wp-block-paragraph"><strong>Example</strong></p>



<p class="wp-block-paragraph">Consider list of numbers <strong>numbers = (1,2,3,4,5,6,7,8,9,10).</strong> The requirement is to find the <strong>sum</strong> of all numbers.</p>



<ol class="wp-block-list"><li>We will introduce an Interface <strong>Strategy</strong> and add an abstract method int <strong>sum(List<;Integer>; numbers)</strong></li></ol>



<pre class="wp-block-code"><code>public interface Strategy 
	public int sum(List<;Integer>; numbers);
}</code></pre>



<p class="wp-block-paragraph">Then we will add a concrete class which will define the behaviour</p>



<pre class="wp-block-code"><code>import java.util.List

public class SummAll implements Strategy {
	@Override
	public int sum(List<;Integer>; numbers) {
		int sum = 0;
		for (Integer num : numbers) {
			sum = sum+num;
		}
		return sum;
	}
}</code></pre>



<p class="wp-block-paragraph">2. Now if we get a new requirement to get a sum of all even numbers then we need to add a new concrete class and need to define behaviour to get a sum of all even numbers.</p>



<pre class="wp-block-code"><code>import java.util.List


public class SummAllEven implements Strategy {

	@Override
	public int sum(List<;Integer>; numbers) {
		int sum = 0;
		for (Integer num : numbers) {
			if (num % 2 == 0) {
				sum = sum + num;
			}
		}
		return sum;
	}
}</code></pre>



<p class="wp-block-paragraph">For each such requirement, we need to add a new concrete class and this will end up with a lot of classes.</p>



<p class="wp-block-paragraph">Using Lambda we can solve this problem very easily, let us implement the <strong>Strategy Pattern using Lambda.</strong></p>



<h3 class="wp-block-heading"><strong>Strategy Patterns using Lambda</strong></h3>



<pre class="wp-block-code"><code>import java.util.Arrays
import java.util.List;
import java.util.function.Predicate;


public class StrategyPatternWithLambda {
	public static int sum(final List<;Integer>; numbers, final Predicate<;Integer>; selector) {
		return numbers.stream()
				.filter(selector)
				.mapToInt(Integer::valueOf)
				.sum();
	}
	
	public static void main(String&#091;] args) {
		List<;Integer>; numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
		//Sum of all numbers
		int sumALl = sum(numbers,e->;true);
		
		//Sum of all even numbers
		int sumALlEven = sum(numbers,e->;e%2==0);
		
		//Sum of all odd numbers
		int sumALlOdd = sum(numbers,e->;e%2!=0);
		
		//Sum of all numbers greater thann 4
		int sumGt4 = sum(numbers,e->;e>;4);
	}
}</code></pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/decorator-pattern-using-lambda/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4640'});
});
</script>
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…
Overview In this article, we will explore a simple Spring Boot application to implement a…