<h2 class="wp-block-heading">Decorator Pattern</h2>



<p class="wp-block-paragraph">A decorator pattern allows a user to add new functionality to an existing object without altering its structure.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img src="https://dineshonjava.com/wp-content/uploads/2022/10/decorator-pattern.png" alt="decorator pattern" class="wp-image-4638" /><figcaption>Decorator Pattern</figcaption></figure>
</div>


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



<pre class="wp-block-code"><code>BufferedReader buffer = new BufferedReader(new FileReader("file path"));</code></pre>



<p class="wp-block-paragraph">But rather than being a decorator, this pattern looks ugly.</p>



<p class="wp-block-paragraph">Using Lambda makes creating a decorator pattern very simple. Let&#8217;s look at a very simple example first, before applying the decorator pattern.</p>



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


public class Sample {
	public static void print(Integer num, String message, Function<;Integer, Integer>; func) {
		System.out.println(num + " " + message + ": " + func.apply(num));
	}


	public static void main(String&#091;] args) {
		Function<;Integer, Integer>; increment = e ->; e + 1;
		Function<;Integer, Integer>; doubled = e ->; e * 2;
		print(5, "Incremented", increment);
		print(5, "doubled", doubled);
	}
}</code></pre>



<p class="wp-block-paragraph">Output</p>



<pre class="wp-block-preformatted">5 Incremented: 6
5 doubled: 10</pre>



<p class="wp-block-paragraph">We can easily call the increment method to increment a number. to double a number we can call the method <strong>doubled</strong>.</p>



<p class="wp-block-paragraph">Now lets you want to double a number but increment it first. How are you going to carry this out? One of the answers will be to add a new method <strong>incAndThendouble</strong> like this.</p>



<pre class="wp-block-code"><code>Function<;Integer, Integer>; incAndThendouble = e ->; (e + 1) * 2;</code></pre>



<p class="wp-block-paragraph">We may now use the method to get the desired outcome. but this is the incorrect strategy.</p>



<p class="wp-block-paragraph">The method composition can be used to obtain the desired output that lambda provides out of the box. Let&#8217;s explore how to use the method composition.</p>



<pre class="wp-block-code"><code>print(5, "incrented and doubled", increment.andThen(doubled));

Output

5 increented and doubled: 12</code></pre>



<p class="wp-block-paragraph">Let&#8217;s examine how we may use this method&#8217;s composition capability to make a Decorator Pattern.</p>



<p class="wp-block-paragraph">Consider a camera with filters. We could add 0 or more filters to the camera. This is a perfect example of a Decorator Pattern.</p>



<pre class="wp-block-code"><code>import java.awt.Color
import java.util.function.Function;
import java.util.stream.Stream;


class Camera {
	private Function<;Color, Color>; filter;
	public Camera(Function<;Color, Color>; ...filters) {
		filter = Stream.of(filters)
					.reduce(Function.identity(),Function::andThen);
	}
	public Color snap(Color input) {
		return filter.apply(input);
	}
}


public class Sample {
	public static void print(Camera camera) {
 //Default color of the snap
		System.out.println(camera.snap(new Color(125, 125, 125)));
	}


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

 // Camera with 0 filter
		print(new Camera());

		//Camera with one filter
		print(new Camera(Color::brighter));

 //Camera with two filters
		print(new Camera(Color::brighter, Color::darker));
	}
}
</code></pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/delegating-pattern-using-lambda/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/strategy-design-patterns-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: '4637'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
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…