10. Java
public class PrintCommand implements Runnable {
private final String s;
PrintCommand(String s) { this.s = s; }
public void run() { System.out.println(s); }
}
public class Invoker {
private final List<Runnable> history = new ArrayList<>();
void invoke(Runnable command) {
command.run();
history.add(command);
}
}
Invoker invoker = new Invoker();
invoker.invoke( new PrintCommand( "Scala"));
invoker.invoke( new PrintCommand( "Vienna"));
21. Scala filter
filtering certain elements from collection
def vowelsNumber(word : String) =
word.filter(isVowel).toSet
vowelsNumber(“Radim”) // Set(‘a’, ‘i’)
22. Scala map
function is applied to each element
def prependHello(names : Seq[String]) =
names.map((name) => “Hello, “ + name)
23. Scala reduce
reduce sequence to a single value
def sum(sq : Seq[Int]) =
if (sq.isEmpty) 0
else
sq.reduce((acc,curr) => acc + curr)
24. Scala comprehensions
● guard
for (a <- list if isEven(a))
● pattern matching
for (Person(name, address) <- people)
● still no mutable state
25. Scala for comprehensions
case class Person(name: String, addr: Address)
case class Address(zip: Int)
def greetings(people: Seq[Person]) =
for (Person(name, address) <-
people if isCloseZip(address.zip) )
yield “Hello, %s”.format(name)
26. Template method
defines the program skeleton of an algorithm in
a method, called template method, which defers
some steps to subclasses
27. Java
public abstract class Template {
public void doStuff() {
beforeStuff(); afterStuff();
}
protected abstract void beforeStuff();
protected abstract void afterStuff();
}
31. Java
public interface Strategy {
int compute(int a, int b);
}
public class Add implements Strategy {
public int compute(int a, int b) { return a + b; }
}
public class Multiply implements Strategy {
public int compute(int a, int b) { return a * b; }
}
public class Context {
private final Strategy strategy;
public Context(Strategy strategy) { this.strategy = strategy; }
public void use(int a, int b) { strategy.compute(a, b); }
}
new Context(new Multiply()).use(2, 3);
32. Scala
define type alias and use first-class functions
type Strategy = (Int, Int) => Int
class Context(computer: Strategy) {
def use(a: Int, b: Int) { computer(a, b) }
}
val add: Strategy = _ + _
val multiply: Strategy = _ * _
new Context(multiply).use( 2, 3)
33. Null Object
avoid null checks in code and
centralize logic that deals with handling the
absence of a value
34. Java
class NullPerson extends Person {....}
public Person build(String first, String last) {
if (first == null || last == null)
return new NullPerson();
return new Person (first, last);
}
35. Scala
val nullPerson = Person() // case class
def build(first: Option[String],
last: Option[String]) =
(for (f <- first; l <- last)
yield Person(f, l)
).getOrElse(nullPerson)
37. Java
public interface OutputStream {
void write(byte b);
void write(byte[] b);
}
public class FileOutputStream implements OutputStream { /* ... */ }
public abstract class OutputStreamDecorator implements OutputStream {
protected final OutputStream delegate;
protected OutputStreamDecorator(OutputStream delegate) {
this.delegate = delegate;
}
public void write(byte b) { delegate.write(b); }
public void write(byte[] b) { delegate.write(b); }
}
38. Scala
def add(a: Int, b: Int) = a + b
def decorateLogger(calcF: (Int, Int) => Int) =
(a: Int, b: Int) => {
val result = calcF(a, b)
println(“Result is: “ + result) // here it comes
result
}