Skip to content

Commit d6dfe7c

Browse files
committed
Design patterns and lambdas
1 parent d6d14d0 commit d6dfe7c

File tree

6 files changed

+285
-0
lines changed

6 files changed

+285
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.function.Function;
4+
import java.util.function.UnaryOperator;
5+
6+
/**
7+
* Created by raoul-gabrielurma on 16/05/2014.
8+
*/
9+
public class ChainOfResponsibilityMain {
10+
11+
public static void main(String[] args) {
12+
ProcessingObject<String> p1 = new HeaderTextProcessing();
13+
ProcessingObject<String> p2 = new SpellCheckerProcessing();
14+
p1.setSuccessor(p2);
15+
String result1 = p1.handle("Aren't labdas really sexy?!!");
16+
System.out.println(result1);
17+
18+
19+
UnaryOperator<String> headerProcessing =
20+
(String text) -> "From Raoul, Mario and Alan: " + text;
21+
UnaryOperator<String> spellCheckerProcessing =
22+
(String text) -> text.replaceAll("labda", "lambda");
23+
Function<String, String> pipeline = headerProcessing.andThen(spellCheckerProcessing);
24+
String result2 = pipeline.apply("Aren't labdas really sexy?!!");
25+
System.out.println(result2);
26+
}
27+
28+
static private abstract class ProcessingObject<T> {
29+
protected ProcessingObject<T> successor;
30+
31+
public void setSuccessor(ProcessingObject<T> successor) {
32+
this.successor = successor;
33+
}
34+
35+
public T handle(T input) {
36+
T r = handleWork(input);
37+
if (successor != null) {
38+
return successor.handle(r);
39+
}
40+
return r;
41+
}
42+
43+
abstract protected T handleWork(T input);
44+
}
45+
46+
static private class HeaderTextProcessing
47+
extends ProcessingObject<String> {
48+
public String handleWork(String text) {
49+
return "From Raoul, Mario and Alan: " + text;
50+
}
51+
}
52+
53+
static private class SpellCheckerProcessing
54+
extends ProcessingObject<String> {
55+
public String handleWork(String text) {
56+
return text.replaceAll("labda", "lambda");
57+
}
58+
}
59+
}
60+
61+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Supplier;
6+
7+
/**
8+
* Created by raoul-gabrielurma on 16/05/2014.
9+
*/
10+
public class FactoryMain {
11+
12+
public static void main(String[] args) {
13+
Product p1 = ProductFactory.createProduct("loan");
14+
15+
Supplier<Product> loanSupplier = Loan::new;
16+
Product p2 = loanSupplier.get();
17+
18+
Product p3 = ProductFactory.createProductLambda("loan");
19+
20+
}
21+
22+
static private class ProductFactory {
23+
public static Product createProduct(String name){
24+
switch(name){
25+
case "loan": return new Loan();
26+
case "stock": return new Stock();
27+
case "bond": return new Bond();
28+
default: throw new RuntimeException("No such product " + name);
29+
}
30+
}
31+
32+
public static Product createProductLambda(String name){
33+
Supplier<Product> p = map.get(name);
34+
if(p != null) return p.get();
35+
throw new RuntimeException("No such product " + name);
36+
}
37+
}
38+
39+
static private interface Product {}
40+
static private class Loan implements Product {}
41+
static private class Stock implements Product {}
42+
static private class Bond implements Product {}
43+
44+
final static private Map<String, Supplier<Product>> map = new HashMap<>();
45+
{
46+
map.put("loan", Loan::new);
47+
map.put("stock", Stock::new);
48+
map.put("bond", Bond::new);
49+
}
50+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Observable;
6+
import java.util.Observer;
7+
8+
/**
9+
* Created by raoul-gabrielurma on 16/05/2014.
10+
*/
11+
public class ObserverMain {
12+
13+
public static void main(String[] args) {
14+
Feed f = new Feed();
15+
f.registerObserver(new NYTimes());
16+
f.registerObserver(new Guardian());
17+
f.registerObserver(new LeMonde());
18+
f.notifyObservers("The queen said her favourite book is Java 8 in Action!");
19+
20+
21+
Feed feedLambda = new Feed();
22+
23+
feedLambda.registerObserver((String tweet) -> {
24+
if(tweet != null && tweet.contains("money")){
25+
System.out.println("Breaking news in NY! " + tweet); }
26+
});
27+
feedLambda.registerObserver((String tweet) -> {
28+
if(tweet != null && tweet.contains("queen")){
29+
System.out.println("Yet another news in London... " + tweet); }
30+
});
31+
32+
feedLambda.notifyObservers("Money money money, give me money!");
33+
34+
}
35+
36+
37+
interface Observer{
38+
void inform(String tweet);
39+
}
40+
41+
interface Subject{
42+
void registerObserver(Observer o);
43+
void notifyObservers(String tweet);
44+
}
45+
46+
static private class NYTimes implements Observer{
47+
@Override
48+
public void inform(String tweet) {
49+
if(tweet != null && tweet.contains("money")){
50+
System.out.println("Breaking news in NY!" + tweet);
51+
}
52+
}
53+
}
54+
55+
static private class Guardian implements Observer{
56+
@Override
57+
public void inform(String tweet) {
58+
if(tweet != null && tweet.contains("queen")){
59+
System.out.println("Yet another news in London... " + tweet);
60+
}
61+
}
62+
}
63+
64+
static private class LeMonde implements Observer{
65+
@Override
66+
public void inform(String tweet) {
67+
if(tweet != null && tweet.contains("wine")){
68+
System.out.println("Today cheese, wine and news! " + tweet);
69+
}
70+
}
71+
}
72+
73+
static private class Feed implements Subject{
74+
private final List<Observer> observers = new ArrayList<>();
75+
public void registerObserver(Observer o) {
76+
this.observers.add(o);
77+
}
78+
public void notifyObservers(String tweet) {
79+
observers.forEach(o -> o.inform(tweet));
80+
}
81+
}
82+
83+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package lambdasinaction.chap7;
2+
3+
/**
4+
* Created by raoul-gabrielurma on 16/05/2014.
5+
*/
6+
abstract class OnlineBanking {
7+
public void processCustomer(int id){
8+
Customer c = Database.getCustomerWithId(id);
9+
makeCustomerHappy(c);
10+
}
11+
abstract void makeCustomerHappy(Customer c);
12+
13+
14+
// dummy Customer class
15+
static private class Customer {}
16+
// dummy Datbase class
17+
static private class Database{
18+
static Customer getCustomerWithId(int id){ return new Customer();}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lambdasinaction.chap7;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* Created by raoul-gabrielurma on 16/05/2014.
7+
*/
8+
public class OnlineBankingLambda {
9+
10+
public static void main(String[] args) {
11+
new OnlineBankingLambda().processCustomer(1337, (Customer c) -> System.out.println("Hello!"));
12+
}
13+
14+
public void processCustomer(int id, Consumer<Customer> makeCustomerHappy){
15+
Customer c = Database.getCustomerWithId(id);
16+
makeCustomerHappy.accept(c);
17+
}
18+
19+
// dummy Customer class
20+
static private class Customer {}
21+
// dummy Database class
22+
static private class Database{
23+
static Customer getCustomerWithId(int id){ return new Customer();}
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package lambdasinaction.chap7;
2+
3+
/**
4+
* Created by raoul-gabrielurma on 16/05/2014.
5+
*/
6+
public class StrategyMain {
7+
8+
public static void main(String[] args) {
9+
// old school
10+
Validator v1 = new Validator(new IsNumeric());
11+
System.out.println(v1.validate("aaaa"));
12+
Validator v2 = new Validator(new IsAllLowerCase ());
13+
System.out.println(v2.validate("bbbb"));
14+
15+
16+
// with lambdas
17+
Validator v3 = new Validator((String s) -> s.matches("\\d+"));
18+
System.out.println(v3.validate("aaaa"));
19+
Validator v4 = new Validator((String s) -> s.matches("[a-z]+"));
20+
System.out.println(v4.validate("bbbb"));
21+
}
22+
23+
interface ValidationStrategy {
24+
public boolean execute(String s);
25+
}
26+
27+
static private class IsAllLowerCase implements ValidationStrategy {
28+
public boolean execute(String s){
29+
return s.matches("[a-z]+");
30+
}
31+
}
32+
static private class IsNumeric implements ValidationStrategy {
33+
public boolean execute(String s){
34+
return s.matches("\\d+");
35+
}
36+
}
37+
38+
static private class Validator{
39+
private final ValidationStrategy strategy;
40+
public Validator(ValidationStrategy v){
41+
this.strategy = v;
42+
}
43+
public boolean validate(String s){
44+
return strategy.execute(s); }
45+
}
46+
}

0 commit comments

Comments
 (0)