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:
1 2 3 4 5 6 7 8 9 10 11 12 | public class ProductFactory { public static Product createProduct (String name) { switch (name) { case "loan": return new Loan (); case "stock": return new Stock (); case "bond": return new Bond (); default: throw new RuntimeException ( "No such product" + name ); } } } |
In which Loan, Stock, Bond are inherited from Product. So when you want to create a product, Loan will call you the following:
1 | Product p = ProductFactory.createProduct ("loan"); |
With the method Lambda:
1 2 3 4 5 6 7 8 9 10 11 12 13 | final static Map <String, Supplier <Product>> map = new HashMap <> (); static { map.put ("loan", Loan :: new); map.put ("stock", Stock :: new); map.put ("bond", Bond :: new); } public static Product createProduct (String name) { Supplier <Product> p = map.get (name); if (p! = null) { return p.get (); } throw new IllegalArgumentException ("No such product" + name); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | inteface Observer { void notify (String tweet); } class NYTimes implements Observer { public void notify (String tweet) { if (tweet! = null && tweet.contains ("happiest")) { System.out.println ("Breaking news in NY!" + Tweet); } }print } class Guardian implements Observer { public void notify (String tweet) { if (tweet! = null && tweet.contains ("nice")) { System.out.printlew "Breaking news in Guardian!" + Tweet); } } } interface Subject { void registerObserver (Observer o); void notifyObservers (String tweet); } Class Feed Subject subject { private final List <Observer> observers = new ArrayList <> (); public void registerObserver (Observer o) { this.observers.add (o); } public void notifyObservers (String tweet) { observers.forEach (o -> o.notify (tweet)); } } |
We have the demo of using the pattern above as follows:
1 2 3 4 | Feed f = new Feed (); f.registerObserver (new NYTimes ()); f.registerObserver (new Guardian ()); f.notifyObservies ("Van is the happiest man in the world"); |
Obviously The New York Times will receive the tweet above.
With Lambda we can directly link observers as follows:
1 2 3 4 5 6 7 8 9 10 | f.registerObserver ((String tweet) -> { if (tweet! = null && tweet.cointains ("happiest")) { System.out.println ("Breaking news in NY!" + Tweet); } }); f.registerObserver ((String tweet) -> { if (tweet! = null && tweet.cointains ("nice")) { System.out.println ("Breaking news in Guardian" + tweet); } }); |