<h2 class="wp-block-heading">Delegating pattern</h2>



<p class="wp-block-paragraph">In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as an inheritance. In delegation, an object handles a request by delegating to a second object. A delegate is a helper object, but with the original context.</p>



<p class="wp-block-paragraph"><strong>To demonstrate the necessity of delegation of execution, let&#8217;s look at an example.</strong></p>



<pre class="wp-block-code"><code>package delegation

public class LazyTest {

	private static int calculate(int x) {
		System.out.println("Called ....");
		return x * 2;
	}


	public static void main(String&#091;] args) {

		int x = 5;
		int temp = calculate(4);
		if (x >; 5 &;&; temp >;= 4) {
			System.out.println("Path 1");
		} else {
			System.out.println("Path 2");
		}
	}
}</code></pre>



<p class="wp-block-paragraph">If we run the code, we can observe that the method &#8220;<strong>calculate</strong>&#8221; is only called once, and its output is never used.</p>



<p class="wp-block-paragraph">If we consider other languages like <strong>Scala</strong>, the execution can be deferred as needed by using the <strong>lazy</strong> keyword, for example, <strong>lazy int temp = calculate(4).</strong></p>



<p class="wp-block-paragraph">Unfortunately, Java doesn&#8217;t support the <strong>lazy</strong> keyword. We can achieve this by using <strong>Lambda.</strong></p>



<p class="wp-block-paragraph">Lambda can be used to delegate method calls when execution is required.</p>



<p class="wp-block-paragraph"><strong>Let us Design it</strong></p>



<pre class="wp-block-code"><code>package delegation


import java.util.function.Supplier;


public class Lazy<;T>; {
	
	private T instance;
	private Supplier<;T>; supplier;


	public Lazy(Supplier<;T>; supplier) {
		this.supplier = supplier;
	}


	public T get() {
		if (instance == null) {
			instance = supplier.get();
			supplier = null;
		}
		return instance;
	}
}

;</code></pre>



<p class="wp-block-paragraph">Now let us test the same</p>



<pre class="wp-block-code"><code>package delegation


public class LazyTest {


	private static int calculte(int x) {
		System.out.println("Called ....");
		return x * 2;
	}


	public static void main(String&#091;] args) {
		int x = 5;
		final Lazy<;Integer>; temp = new Lazy<;Integer>;(()->; calculte(4));
		if (x >; 5 &;&; temp.get() >;= 4) {
			System.out.println("Path 1");
		} else {
			System.out.println("Path 2");
		}
	}
}

;</code></pre>



<p class="wp-block-paragraph"><strong>Test 1 &#8211;</strong> ;If you run the code right now, you&#8217;ll notice that the method ;<strong>calculate</strong> ;is not being called.</p>



<p class="wp-block-paragraph"><strong>Test 2 &#8211; </strong>Change the <strong>value</strong> of x to 15 and then run the code. The function calculate is now only run once because the temp is assessed within it, which is fine.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-8/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/decorator-pattern-using-lambda/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4634'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
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…