SlideShare a Scribd company logo
OOP and FP 
Richard Warburton
What on earth are you talking about? 
SOLID Principles 
Design Patterns 
Anthropology
Twins: Object Oriented Programming and Functional Programming
In Quotes ... 
"OOP is to writing a program, what going through airport 
security is to flying" 
- Richard Mansfield 
"TDD replaces a type checker in Ruby in the same way that 
a strong drink replaces sorrows." 
- byorgey
In Quotes ... 
"Brain explosion is like a traditional pasttime in #haskell" 
"Some people claim everything is lisp. One time I was 
eating some spaghetti and someone came by and said: 
'Hey, nice lisp dialect you're hacking in there'"
Twins: Object Oriented Programming and Functional Programming
Caveat: some unorthodox definitions may be 
provided
What on earth are you talking about? 
SOLID Principles 
Design Patterns 
Anthropology
SOLID Principles 
● Basic Object Oriented Programming 
Principles 
● Make programs easier to maintain 
● Guidelines to remove code smells
Single Responsibility Principle 
● Each class/method should have single 
responsibility 
● Responsibility means “reason to change” 
● The responsibility should be encapsulated
int countPrimes(int upTo) { 
int tally = 0; 
for (int i = 1; i < upTo; i++) { 
boolean isPrime = true; 
for (int j = 2; j < i; j++) { 
if (i % j == 0) { 
isPrime = false; 
} 
} 
if (isPrime) { 
tally++; 
} 
} 
return tally; 
}
Twins: Object Oriented Programming and Functional Programming
int countPrimes(int upTo) { 
int tally = 0; 
for (int i = 1; i < upTo; i++) { 
if (isPrime(i)) { 
tally++; 
} 
} 
return tally; 
} 
boolean isPrime(int number) { 
for (int i = 2; i < number; i++) { 
if (number % i == 0) { 
return false; 
} 
} 
return true; 
}
long countPrimes(int upTo) { 
return IntStream.range(1, upTo) 
.filter(this::isPrime) 
.count(); 
} 
boolean isPrime(int number) { 
return IntStream.range(2, number) 
.allMatch(x -> (number % x) != 0); 
}
Higher Order Functions 
● Hard to write single responsibility code in 
Java before 8 
● Single responsibility requires ability to pass 
around behaviour 
● Not just functions, Higher Order Functions
Open Closed Principle
"software entities should be open for extension, 
but closed for modification" 
- Bertrand Meyer
Example: Graphing Metric Data
OCP as Polymorphism 
● Graphing Metric Data 
○ CpuUsage 
○ ProcessDiskWrite 
○ MachineIO 
● GraphDisplay depends upon a 
TimeSeries rather than each individually 
● No need to change GraphDisplay to add 
SwapTime
OCP as High Order Function 
// Example creation 
ThreadLocal<DateFormat> formatter = 
withInitial(() -> new SimpleDateFormat()); 
// Usage 
DateFormat formatter = formatter.get(); 
// Or ... 
AtomicInteger threadId = new AtomicInteger(); 
ThreadLocal<Integer> formatter = 
withInitial(() -> threadId.getAndIncrement());
OCP as Immutability 
● Immutable Object cannot be modified after 
creation 
● Safe to add additional behaviour 
● New pure functions can’t break existing 
functionality because it can’t change state
Liskov Substitution Principle 
Let q(x) be a property provable about objects 
x of type T. Then q(y) should be true for 
objects y of type S where S is a subtype of T. 
* Excuse the informality
Twins: Object Oriented Programming and Functional Programming
A subclass behaves like its parent. 
* This is a conscious simplification
1. Where the parent worked the child should. 
2. Where the parent caused an effect then the 
child should. 
3. Where parent always stuck by something 
then the child should. 
4. Don’t change things your parent didn’t.
Functional Perspective 
● Inheritance isn’t key to FP 
● Lesson: don’t inherit implementation and 
LSP isn’t an issue! 
● Composite Reuse Principle already 
commonly accepted OOP principle
Interface Segregation Principle 
"The dependency of one class to another one 
should depend on the smallest possible 
interface" 
- Robert Martin
Factory Example 
interface Worker { 
public void goHome(); 
public void work(); 
} 
AssemblyLine requires instances of 
Worker: AssemblyWorker and Manager
The factories start using robots... 
… but a Robot doesn’t goHome()
Nominal Subtyping 
● For Foo to extend Bar you need to see Foo 
extends Bar in your code. 
● Relationship explicit between types based 
on the name of the type 
● Common in Statically Typed, OO languages: 
Java, C++
class AssemblyWorker implements 
Worker 
class Manager implements Worker 
class Robot implements Worker
public void addWorker(Worker worker) { 
workers.add(worker); 
} 
public static AssemblyLine newLine() { 
AssemblyLine line = new AssemblyLine(); 
line.addWorker(new Manager()); 
line.addWorker(new AssemblyWorker()); 
line.addWorker(new Robot()); 
return line; 
}
Structural Subtyping 
● Relationship implicit between types based 
on the shape/structure of the type 
● If you call obj.getFoo() then obj needs a 
getFoo method 
● Common in wacky language: Ocaml, Go, 
C++ Templates, Ruby (quack quack)
class StructuralWorker { 
def work(step:ProductionStep) { 
println( 
"I'm working on: " 
+ step.getName) 
} 
}
def addWorker(worker: {def work(step:ProductionStep)}) { 
workers += worker 
} 
def newLine() = { 
val line = new AssemblyLine 
line.addWorker(new Manager()) 
line.addWorker(new StructuralWorker()) 
line.addWorker(new Robot()) 
line 
}
Hypothetically … 
def addWorker(worker) { 
workers += worker 
} 
def newLine() = { 
val line = new AssemblyLine 
line.addWorker(new Manager()) 
line.addWorker(new StructuralWorker()) 
line.addWorker(new Robot()) 
line 
}
Functional Interfaces 
● An interface with a single abstract 
method 
● By definition the minimal interface! 
● Used as the inferred types for lambda 
expressions in Java 8
Thoughts on ISP 
● Structural Subtyping removes the need for 
Interface Segregation Principle 
● Functional Interfaces provide a nominal-structural 
bridge 
● ISP != implementing 500 interfaces
Dependency Inversion Principle
Dependency Inversion Principle 
● Abstractions should not depend on details, 
details should depend on abstractions 
● Decouple glue code from business logic 
● Inversion of Control/Dependency Injection is 
an implementation of DIP
Streams Library 
album.getMusicians() 
.filter(artist -> artist.name().contains(“The”)) 
.map(artist -> artist.getNationality()) 
.collect(toList());
Resource Handling & Logic 
List<String> findHeadings() { 
try (BufferedReader reader 
= new BufferedReader(new FileReader(file))) { 
return reader.lines() 
.filter(isHeading) 
.collect(toList()); 
} catch (IOException e) { 
throw new HeadingLookupException(e); 
} 
}
Business Logic 
private List<String> findHeadings() { 
return withLinesOf(file, 
lines -> lines.filter(isHeading) 
.collect(toList()), 
HeadingLookupException::new); 
}
Resource Handling 
<T> T withLinesOf(String file, 
Function<Stream<String>, T> handler, 
Function<IOException, 
RuntimeException> error) { 
try (BufferedReader reader = 
new BufferedReader(new FileReader(file))) { 
return handler.apply(reader.lines()); 
} catch (IOException e) { 
throw error.apply(e); 
} 
}
DIP Summary 
● Higher Order Functions also provide 
Inversion of Control 
● Abstraction != interface 
● Functional resource handling, eg withFile 
in haskell
All the solid patterns have a functional 
equivalent
The same idea expressed in different ways
What on earth are you talking about? 
SOLID Principles 
Design Patterns 
Anthropology
Command Pattern 
• Receiver - performs the actual work. 
• Command - encapsulates all the information 
required to call the receiver. 
• Invoker - controls the sequencing and 
execution of one or more commands. 
• Client - creates concrete command instances
Macro: take something that’s long and make it short
public interface Editor { 
public void save(); 
public void open(); 
public void close(); 
}
public interface Action { 
public void perform(); 
}
public class Open implements Action { 
private final Editor editor; 
public Open(Editor editor) { 
this.editor = editor; 
} 
public void perform() { 
editor.open(); 
} 
}
public class Macro { 
private final List<Action> actions; 
… 
public void record(Action action) { 
actions.add(action); 
} 
public void run() { 
actions.forEach(Action::perform); 
} 
}
Macro macro = new Macro(); 
macro.record(new Open(editor)); 
macro.record(new Save(editor)); 
macro.record(new Close(editor)); 
macro.run();
The Command Object is a Function 
Macro macro = new Macro(); 
macro.record(() -> editor.open()); 
macro.record(() -> editor.save()); 
macro.record(() -> editor.close()); 
macro.run();
Observer Pattern
Twins: Object Oriented Programming and Functional Programming
Concrete Example: Profiler 
public interface ProfileListener { 
public void accept(Profile profile); 
}
private final List<ProfileListener> listeners; 
public void addListener(ProfileListener listener) { 
listeners.add(listener); 
} 
private void accept(Profile profile) { 
for (ProfileListener listener : listeners) { 
listener.accept(profile) 
} 
}
Previously you needed to write this EVERY 
time.
Consumer<T> === T → () 
ProfileListener === Profile → () 
ActionListener === Action → ()
public class Listeners<T> implements Consumer<T> { 
private final List<Consumer<T>> consumers; 
public Listeners<T> add(Consumer<T> consumer) { 
consumers.add(consumer); 
return this; 
} 
@Override 
public void accept(T value) { 
consumers.forEach(consumer -> consumer.accept(value)); 
}
public ProfileListener provide( 
FlatViewModel flatModel, 
TreeViewModel treeModel) { 
Listeners<Profile> listener = new 
Listeners<Profile>() 
.of(flatModel::accept) 
.of(treeModel::accept); 
return listener::accept; 
}
Existing Design Patterns don’t need to be 
thrown away.
Existing Design Patterns can be improved.
What on earth are you talking about? 
SOLID Principles 
Design Patterns 
Anthropology
Popular programming language evolution 
follows Arnie’s career.
The 1980s were great!
Programming 80s style 
● Strongly multiparadigm languages 
○ Smalltalk 80 had lambda expressions 
○ Common Lisp Object System 
● Polyglot Programmers 
● Fertile Language Research 
● Implementation Progress - GC, JITs, etc.
The 1990s ruined everything
90s and 2000s Market Convergence 
● Huge Java popularity ramp 
○ Javaone in 2001 - 28,000 attendees 
○ Servlets, J2EE then Spring 
● Virtual death of Smalltalk, LISP then Perl 
● Object Oriented Dominance
Now everyone is friends
Increasingly Multiparadigm 
● Established languages going multiparadigm 
○ Java 8 - Generics + Lambdas 
○ C++ - Templates, Lambdas 
● Newer Languages are multi paradigm 
○ F# 
○ Ruby/Python/Groovy can be functional 
○ New JVM languages: 
■ Scala 
■ Ceylon 
■ Kotlin
http://java8training.com 
http://is.gd/javalambdas 
@richardwarburto 
http://insightfullogic.com

More Related Content

What's hot (20)

Java generics final
Java generics finalJava generics final
Java generics final
Akshay Chaudhari
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
Reem Alattas
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
 
Lazy java
Lazy javaLazy java
Lazy java
Mario Fusco
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
Sergey Bandysik
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Codemotion
 
Kotlin
KotlinKotlin
Kotlin
YeldosTanikin
 
Java Generics
Java GenericsJava Generics
Java Generics
Carol McDonald
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
Vadim Dubs
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
NAVER Engineering
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Chris Richardson
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
Reem Alattas
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
daewon jeong
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
Anjan Banda
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
2 kotlin vs. java: what java has that kotlin does not
2  kotlin vs. java: what java has that kotlin does not2  kotlin vs. java: what java has that kotlin does not
2 kotlin vs. java: what java has that kotlin does not
Sergey Bandysik
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Codemotion
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
Rasan Samarasinghe
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
Vadim Dubs
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
Trisha Gee
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Chris Richardson
 

Viewers also liked (7)

Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)
RichardWarburton
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hood
RichardWarburton
 
Generics Past, Present and Future
Generics Past, Present and FutureGenerics Past, Present and Future
Generics Past, Present and Future
RichardWarburton
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictability
RichardWarburton
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakens
RichardWarburton
 
How to run a hackday
How to run a hackdayHow to run a hackday
How to run a hackday
RichardWarburton
 
Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)
RichardWarburton
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hood
RichardWarburton
 
Generics Past, Present and Future
Generics Past, Present and FutureGenerics Past, Present and Future
Generics Past, Present and Future
RichardWarburton
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictability
RichardWarburton
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakens
RichardWarburton
 
Ad

Similar to Twins: Object Oriented Programming and Functional Programming (20)

TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
Codemotion
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
RichardWarburton
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
RichardWarburton
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)
Felix Chen
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
jinaldesailive
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
The good, the bad and the SOLID
The good, the bad and the SOLIDThe good, the bad and the SOLID
The good, the bad and the SOLID
Frikkie van Biljon
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
vivek p s
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
Mohammad Shawahneh
 
Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
Stanislav Tiurikov
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
Scott Wlaschin
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
Chris Eargle
 
SOLID Principles in OOPS ooooooooo.pptx
SOLID  Principles in OOPS ooooooooo.pptxSOLID  Principles in OOPS ooooooooo.pptx
SOLID Principles in OOPS ooooooooo.pptx
banjaaring
 
Understanding SOLID Principles in OOP programming
Understanding SOLID Principles in OOP programmingUnderstanding SOLID Principles in OOP programming
Understanding SOLID Principles in OOP programming
AntonelaAniLeka
 
L22 Design Principles
L22 Design PrinciplesL22 Design Principles
L22 Design Principles
Ólafur Andri Ragnarsson
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
Attila Bertók
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
Dmitry Kandalov
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
Xiaoyan Chen
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
Aditya Kumar Rajan
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
Codemotion
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)
Felix Chen
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
jinaldesailive
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
Muhammad Raza
 
The good, the bad and the SOLID
The good, the bad and the SOLIDThe good, the bad and the SOLID
The good, the bad and the SOLID
Frikkie van Biljon
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
vivek p s
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
Mohammad Shawahneh
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
Scott Wlaschin
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
Chris Eargle
 
SOLID Principles in OOPS ooooooooo.pptx
SOLID  Principles in OOPS ooooooooo.pptxSOLID  Principles in OOPS ooooooooo.pptx
SOLID Principles in OOPS ooooooooo.pptx
banjaaring
 
Understanding SOLID Principles in OOP programming
Understanding SOLID Principles in OOP programmingUnderstanding SOLID Principles in OOP programming
Understanding SOLID Principles in OOP programming
AntonelaAniLeka
 
An Introduction to the SOLID Principles
An Introduction to the SOLID PrinciplesAn Introduction to the SOLID Principles
An Introduction to the SOLID Principles
Attila Bertók
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
Xiaoyan Chen
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
Aditya Kumar Rajan
 
Ad

More from RichardWarburton (20)

Fantastic performance and where to find it
Fantastic performance and where to find itFantastic performance and where to find it
Fantastic performance and where to find it
RichardWarburton
 
Production profiling what, why and how technical audience (3)
Production profiling  what, why and how   technical audience (3)Production profiling  what, why and how   technical audience (3)
Production profiling what, why and how technical audience (3)
RichardWarburton
 
Production profiling: What, Why and How
Production profiling: What, Why and HowProduction profiling: What, Why and How
Production profiling: What, Why and How
RichardWarburton
 
Production profiling what, why and how (JBCN Edition)
Production profiling  what, why and how (JBCN Edition)Production profiling  what, why and how (JBCN Edition)
Production profiling what, why and how (JBCN Edition)
RichardWarburton
 
Production Profiling: What, Why and How
Production Profiling: What, Why and HowProduction Profiling: What, Why and How
Production Profiling: What, Why and How
RichardWarburton
 
Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)
RichardWarburton
 
Collections forceawakens
Collections forceawakensCollections forceawakens
Collections forceawakens
RichardWarburton
 
Generics past, present and future
Generics  past, present and futureGenerics  past, present and future
Generics past, present and future
RichardWarburton
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
RichardWarburton
 
Introduction to lambda behave
Introduction to lambda behaveIntroduction to lambda behave
Introduction to lambda behave
RichardWarburton
 
Introduction to lambda behave
Introduction to lambda behaveIntroduction to lambda behave
Introduction to lambda behave
RichardWarburton
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictability
RichardWarburton
 
Simplifying java with lambdas (short)
Simplifying java with lambdas (short)Simplifying java with lambdas (short)
Simplifying java with lambdas (short)
RichardWarburton
 
The Bleeding Edge
The Bleeding EdgeThe Bleeding Edge
The Bleeding Edge
RichardWarburton
 
Lambdas myths-and-mistakes
Lambdas myths-and-mistakesLambdas myths-and-mistakes
Lambdas myths-and-mistakes
RichardWarburton
 
Caching in
Caching inCaching in
Caching in
RichardWarburton
 
Lambdas: Myths and Mistakes
Lambdas: Myths and MistakesLambdas: Myths and Mistakes
Lambdas: Myths and Mistakes
RichardWarburton
 
Better than a coin toss
Better than a coin tossBetter than a coin toss
Better than a coin toss
RichardWarburton
 
Devoxx uk lambdas hackday
Devoxx uk lambdas hackdayDevoxx uk lambdas hackday
Devoxx uk lambdas hackday
RichardWarburton
 
How to run a hackday
How to run a hackdayHow to run a hackday
How to run a hackday
RichardWarburton
 
Fantastic performance and where to find it
Fantastic performance and where to find itFantastic performance and where to find it
Fantastic performance and where to find it
RichardWarburton
 
Production profiling what, why and how technical audience (3)
Production profiling  what, why and how   technical audience (3)Production profiling  what, why and how   technical audience (3)
Production profiling what, why and how technical audience (3)
RichardWarburton
 
Production profiling: What, Why and How
Production profiling: What, Why and HowProduction profiling: What, Why and How
Production profiling: What, Why and How
RichardWarburton
 
Production profiling what, why and how (JBCN Edition)
Production profiling  what, why and how (JBCN Edition)Production profiling  what, why and how (JBCN Edition)
Production profiling what, why and how (JBCN Edition)
RichardWarburton
 
Production Profiling: What, Why and How
Production Profiling: What, Why and HowProduction Profiling: What, Why and How
Production Profiling: What, Why and How
RichardWarburton
 
Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)
RichardWarburton
 
Generics past, present and future
Generics  past, present and futureGenerics  past, present and future
Generics past, present and future
RichardWarburton
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
RichardWarburton
 
Introduction to lambda behave
Introduction to lambda behaveIntroduction to lambda behave
Introduction to lambda behave
RichardWarburton
 
Introduction to lambda behave
Introduction to lambda behaveIntroduction to lambda behave
Introduction to lambda behave
RichardWarburton
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictability
RichardWarburton
 
Simplifying java with lambdas (short)
Simplifying java with lambdas (short)Simplifying java with lambdas (short)
Simplifying java with lambdas (short)
RichardWarburton
 
Lambdas myths-and-mistakes
Lambdas myths-and-mistakesLambdas myths-and-mistakes
Lambdas myths-and-mistakes
RichardWarburton
 
Lambdas: Myths and Mistakes
Lambdas: Myths and MistakesLambdas: Myths and Mistakes
Lambdas: Myths and Mistakes
RichardWarburton
 

Recently uploaded (20)

Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 

Twins: Object Oriented Programming and Functional Programming

  • 1. OOP and FP Richard Warburton
  • 2. What on earth are you talking about? SOLID Principles Design Patterns Anthropology
  • 4. In Quotes ... "OOP is to writing a program, what going through airport security is to flying" - Richard Mansfield "TDD replaces a type checker in Ruby in the same way that a strong drink replaces sorrows." - byorgey
  • 5. In Quotes ... "Brain explosion is like a traditional pasttime in #haskell" "Some people claim everything is lisp. One time I was eating some spaghetti and someone came by and said: 'Hey, nice lisp dialect you're hacking in there'"
  • 7. Caveat: some unorthodox definitions may be provided
  • 8. What on earth are you talking about? SOLID Principles Design Patterns Anthropology
  • 9. SOLID Principles ● Basic Object Oriented Programming Principles ● Make programs easier to maintain ● Guidelines to remove code smells
  • 10. Single Responsibility Principle ● Each class/method should have single responsibility ● Responsibility means “reason to change” ● The responsibility should be encapsulated
  • 11. int countPrimes(int upTo) { int tally = 0; for (int i = 1; i < upTo; i++) { boolean isPrime = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = false; } } if (isPrime) { tally++; } } return tally; }
  • 13. int countPrimes(int upTo) { int tally = 0; for (int i = 1; i < upTo; i++) { if (isPrime(i)) { tally++; } } return tally; } boolean isPrime(int number) { for (int i = 2; i < number; i++) { if (number % i == 0) { return false; } } return true; }
  • 14. long countPrimes(int upTo) { return IntStream.range(1, upTo) .filter(this::isPrime) .count(); } boolean isPrime(int number) { return IntStream.range(2, number) .allMatch(x -> (number % x) != 0); }
  • 15. Higher Order Functions ● Hard to write single responsibility code in Java before 8 ● Single responsibility requires ability to pass around behaviour ● Not just functions, Higher Order Functions
  • 17. "software entities should be open for extension, but closed for modification" - Bertrand Meyer
  • 19. OCP as Polymorphism ● Graphing Metric Data ○ CpuUsage ○ ProcessDiskWrite ○ MachineIO ● GraphDisplay depends upon a TimeSeries rather than each individually ● No need to change GraphDisplay to add SwapTime
  • 20. OCP as High Order Function // Example creation ThreadLocal<DateFormat> formatter = withInitial(() -> new SimpleDateFormat()); // Usage DateFormat formatter = formatter.get(); // Or ... AtomicInteger threadId = new AtomicInteger(); ThreadLocal<Integer> formatter = withInitial(() -> threadId.getAndIncrement());
  • 21. OCP as Immutability ● Immutable Object cannot be modified after creation ● Safe to add additional behaviour ● New pure functions can’t break existing functionality because it can’t change state
  • 22. Liskov Substitution Principle Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. * Excuse the informality
  • 24. A subclass behaves like its parent. * This is a conscious simplification
  • 25. 1. Where the parent worked the child should. 2. Where the parent caused an effect then the child should. 3. Where parent always stuck by something then the child should. 4. Don’t change things your parent didn’t.
  • 26. Functional Perspective ● Inheritance isn’t key to FP ● Lesson: don’t inherit implementation and LSP isn’t an issue! ● Composite Reuse Principle already commonly accepted OOP principle
  • 27. Interface Segregation Principle "The dependency of one class to another one should depend on the smallest possible interface" - Robert Martin
  • 28. Factory Example interface Worker { public void goHome(); public void work(); } AssemblyLine requires instances of Worker: AssemblyWorker and Manager
  • 29. The factories start using robots... … but a Robot doesn’t goHome()
  • 30. Nominal Subtyping ● For Foo to extend Bar you need to see Foo extends Bar in your code. ● Relationship explicit between types based on the name of the type ● Common in Statically Typed, OO languages: Java, C++
  • 31. class AssemblyWorker implements Worker class Manager implements Worker class Robot implements Worker
  • 32. public void addWorker(Worker worker) { workers.add(worker); } public static AssemblyLine newLine() { AssemblyLine line = new AssemblyLine(); line.addWorker(new Manager()); line.addWorker(new AssemblyWorker()); line.addWorker(new Robot()); return line; }
  • 33. Structural Subtyping ● Relationship implicit between types based on the shape/structure of the type ● If you call obj.getFoo() then obj needs a getFoo method ● Common in wacky language: Ocaml, Go, C++ Templates, Ruby (quack quack)
  • 34. class StructuralWorker { def work(step:ProductionStep) { println( "I'm working on: " + step.getName) } }
  • 35. def addWorker(worker: {def work(step:ProductionStep)}) { workers += worker } def newLine() = { val line = new AssemblyLine line.addWorker(new Manager()) line.addWorker(new StructuralWorker()) line.addWorker(new Robot()) line }
  • 36. Hypothetically … def addWorker(worker) { workers += worker } def newLine() = { val line = new AssemblyLine line.addWorker(new Manager()) line.addWorker(new StructuralWorker()) line.addWorker(new Robot()) line }
  • 37. Functional Interfaces ● An interface with a single abstract method ● By definition the minimal interface! ● Used as the inferred types for lambda expressions in Java 8
  • 38. Thoughts on ISP ● Structural Subtyping removes the need for Interface Segregation Principle ● Functional Interfaces provide a nominal-structural bridge ● ISP != implementing 500 interfaces
  • 40. Dependency Inversion Principle ● Abstractions should not depend on details, details should depend on abstractions ● Decouple glue code from business logic ● Inversion of Control/Dependency Injection is an implementation of DIP
  • 41. Streams Library album.getMusicians() .filter(artist -> artist.name().contains(“The”)) .map(artist -> artist.getNationality()) .collect(toList());
  • 42. Resource Handling & Logic List<String> findHeadings() { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { return reader.lines() .filter(isHeading) .collect(toList()); } catch (IOException e) { throw new HeadingLookupException(e); } }
  • 43. Business Logic private List<String> findHeadings() { return withLinesOf(file, lines -> lines.filter(isHeading) .collect(toList()), HeadingLookupException::new); }
  • 44. Resource Handling <T> T withLinesOf(String file, Function<Stream<String>, T> handler, Function<IOException, RuntimeException> error) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { return handler.apply(reader.lines()); } catch (IOException e) { throw error.apply(e); } }
  • 45. DIP Summary ● Higher Order Functions also provide Inversion of Control ● Abstraction != interface ● Functional resource handling, eg withFile in haskell
  • 46. All the solid patterns have a functional equivalent
  • 47. The same idea expressed in different ways
  • 48. What on earth are you talking about? SOLID Principles Design Patterns Anthropology
  • 49. Command Pattern • Receiver - performs the actual work. • Command - encapsulates all the information required to call the receiver. • Invoker - controls the sequencing and execution of one or more commands. • Client - creates concrete command instances
  • 50. Macro: take something that’s long and make it short
  • 51. public interface Editor { public void save(); public void open(); public void close(); }
  • 52. public interface Action { public void perform(); }
  • 53. public class Open implements Action { private final Editor editor; public Open(Editor editor) { this.editor = editor; } public void perform() { editor.open(); } }
  • 54. public class Macro { private final List<Action> actions; … public void record(Action action) { actions.add(action); } public void run() { actions.forEach(Action::perform); } }
  • 55. Macro macro = new Macro(); macro.record(new Open(editor)); macro.record(new Save(editor)); macro.record(new Close(editor)); macro.run();
  • 56. The Command Object is a Function Macro macro = new Macro(); macro.record(() -> editor.open()); macro.record(() -> editor.save()); macro.record(() -> editor.close()); macro.run();
  • 59. Concrete Example: Profiler public interface ProfileListener { public void accept(Profile profile); }
  • 60. private final List<ProfileListener> listeners; public void addListener(ProfileListener listener) { listeners.add(listener); } private void accept(Profile profile) { for (ProfileListener listener : listeners) { listener.accept(profile) } }
  • 61. Previously you needed to write this EVERY time.
  • 62. Consumer<T> === T → () ProfileListener === Profile → () ActionListener === Action → ()
  • 63. public class Listeners<T> implements Consumer<T> { private final List<Consumer<T>> consumers; public Listeners<T> add(Consumer<T> consumer) { consumers.add(consumer); return this; } @Override public void accept(T value) { consumers.forEach(consumer -> consumer.accept(value)); }
  • 64. public ProfileListener provide( FlatViewModel flatModel, TreeViewModel treeModel) { Listeners<Profile> listener = new Listeners<Profile>() .of(flatModel::accept) .of(treeModel::accept); return listener::accept; }
  • 65. Existing Design Patterns don’t need to be thrown away.
  • 66. Existing Design Patterns can be improved.
  • 67. What on earth are you talking about? SOLID Principles Design Patterns Anthropology
  • 68. Popular programming language evolution follows Arnie’s career.
  • 69. The 1980s were great!
  • 70. Programming 80s style ● Strongly multiparadigm languages ○ Smalltalk 80 had lambda expressions ○ Common Lisp Object System ● Polyglot Programmers ● Fertile Language Research ● Implementation Progress - GC, JITs, etc.
  • 71. The 1990s ruined everything
  • 72. 90s and 2000s Market Convergence ● Huge Java popularity ramp ○ Javaone in 2001 - 28,000 attendees ○ Servlets, J2EE then Spring ● Virtual death of Smalltalk, LISP then Perl ● Object Oriented Dominance
  • 73. Now everyone is friends
  • 74. Increasingly Multiparadigm ● Established languages going multiparadigm ○ Java 8 - Generics + Lambdas ○ C++ - Templates, Lambdas ● Newer Languages are multi paradigm ○ F# ○ Ruby/Python/Groovy can be functional ○ New JVM languages: ■ Scala ■ Ceylon ■ Kotlin