Categories: Java 8

Delegating pattern using lambda

&NewLine;<h2 class&equals;"wp-block-heading">Delegating pattern<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">In software engineering&comma; the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as an inheritance&period; In delegation&comma; an object handles a request by delegating to a second object&period; A delegate is a helper object&comma; but with the original context&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>To demonstrate the necessity of delegation of execution&comma; let&&num;8217&semi;s look at an example&period;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>package delegation&NewLine;&NewLine;public class LazyTest &lbrace;&NewLine;&NewLine;&Tab;private static int calculate&lpar;int x&rpar; &lbrace;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Called &period;&period;&period;&period;"&rpar;&semi;&NewLine;&Tab;&Tab;return x &ast; 2&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&NewLine;&Tab;public static void main&lpar;String&&num;091&semi;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine;&Tab;&Tab;int x &equals; 5&semi;&NewLine;&Tab;&Tab;int temp &equals; calculate&lpar;4&rpar;&semi;&NewLine;&Tab;&Tab;if &lpar;x &gt&semi; 5 &amp&semi;&amp&semi; temp &gt&semi;&equals; 4&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Path 1"&rpar;&semi;&NewLine;&Tab;&Tab;&rcub; else &lbrace;&NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Path 2"&rpar;&semi;&NewLine;&Tab;&Tab;&rcub;&NewLine;&Tab;&rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">If we run the code&comma; we can observe that the method &&num;8220&semi;<strong>calculate<&sol;strong>&&num;8221&semi; is only called once&comma; and its output is never used&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">If we consider other languages like <strong>Scala<&sol;strong>&comma; the execution can be deferred as needed by using the <strong>lazy<&sol;strong> keyword&comma; for example&comma; <strong>lazy int temp &equals; calculate&lpar;4&rpar;&period;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Unfortunately&comma; Java doesn&&num;8217&semi;t support the <strong>lazy<&sol;strong> keyword&period; We can achieve this by using <strong>Lambda&period;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Lambda can be used to delegate method calls when execution is required&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Let us Design it<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>package delegation&NewLine;&NewLine;&NewLine;import java&period;util&period;function&period;Supplier&semi;&NewLine;&NewLine;&NewLine;public class Lazy&lt&semi;T&gt&semi; &lbrace;&NewLine;&Tab;&NewLine;&Tab;private T instance&semi;&NewLine;&Tab;private Supplier&lt&semi;T&gt&semi; supplier&semi;&NewLine;&NewLine;&NewLine;&Tab;public Lazy&lpar;Supplier&lt&semi;T&gt&semi; supplier&rpar; &lbrace;&NewLine;&Tab;&Tab;this&period;supplier &equals; supplier&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&NewLine;&Tab;public T get&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;if &lpar;instance &equals;&equals; null&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;instance &equals; supplier&period;get&lpar;&rpar;&semi;&NewLine;&Tab;&Tab;&Tab;supplier &equals; null&semi;&NewLine;&Tab;&Tab;&rcub;&NewLine;&Tab;&Tab;return instance&semi;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;&NewLine;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">Now let us test the same<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>package delegation&NewLine;&NewLine;&NewLine;public class LazyTest &lbrace;&NewLine;&NewLine;&NewLine;&Tab;private static int calculte&lpar;int x&rpar; &lbrace;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Called &period;&period;&period;&period;"&rpar;&semi;&NewLine;&Tab;&Tab;return x &ast; 2&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&NewLine;&Tab;public static void main&lpar;String&&num;091&semi;&rsqb; args&rpar; &lbrace;&NewLine;&Tab;&Tab;int x &equals; 5&semi;&NewLine;&Tab;&Tab;final Lazy&lt&semi;Integer&gt&semi; temp &equals; new Lazy&lt&semi;Integer&gt&semi;&lpar;&lpar;&rpar;-&gt&semi; calculte&lpar;4&rpar;&rpar;&semi;&NewLine;&Tab;&Tab;if &lpar;x &gt&semi; 5 &amp&semi;&amp&semi; temp&period;get&lpar;&rpar; &gt&semi;&equals; 4&rpar; &lbrace;&NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Path 1"&rpar;&semi;&NewLine;&Tab;&Tab;&rcub; else &lbrace;&NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Path 2"&rpar;&semi;&NewLine;&Tab;&Tab;&rcub;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;&NewLine;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Test 1 &&num;8211&semi;<&sol;strong>&nbsp&semi;If you run the code right now&comma; you&&num;8217&semi;ll notice that the method&nbsp&semi;<strong>calculate<&sol;strong>&nbsp&semi;is not being called&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Test 2 &&num;8211&semi; <&sol;strong>Change the <strong>value<&sol;strong> of x to 15 and then run the code&period; The function calculate is now only run once because the temp is assessed within it&comma; which is fine&period;<&sol;p>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-8&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;decorator-pattern-using-lambda&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4634'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Pankaj Singh

Specialties: java technologies : Spring, Microservices ,Hibernate, JPA, Apache Spark, Spring Batch, React Js, Maven and Kafka.

Share
Published by
Pankaj Singh

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago

Configure Multiple Databases Spring JPA Spring Boot

Overview In this article, we will explore a simple Spring Boot application to implement a…

4 years ago