Lambda’s application in Design Patterns (Part 1)

In this article, I want to introduce how to use Lambda from Java 8.0 to create the Gang of four Design Patterns:

1. Factory Pattern

For example: a bank with products such as Loan, Stock and Bond, we have a pattern for designing patterns like after using class:

In which Loan, Stock, Bond are inherited from Product. So when you want to create a product, Loan will call you the following:

With the method Lambda:

Solve problems in Scalable Social Network - Register now!
Solve problems in Scalable Social Network – Register now!

2. Observer Pattern

For example: We designed a system to create messages similar to Twitter. That is, some newspapers (The New York Times, The Guardian) follow the feed of new feeds and will receive a notification if a keyword contains a given keyword.

With regular class usage:

We have the demo of using the pattern above as follows:

Obviously The New York Times will receive the tweet above.

With Lambda we can directly link observers as follows:

ITZone via Viblo

Share the news now