05_intro_fp.pdf
Document Details
Uploaded by FavorableChicago
Concordia University
Tags
Related
Full Transcript
SOEN 6441 Lecture 5 Motivation Behavior Functional Programmin...
SOEN 6441 Lecture 5 Motivation Behavior Functional Programming parameterization Predicates Passing code with behavior parameterization Strategy Design Pattern Filtering by Predicate Passing Code/Behavior SOEN 6441 Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading SOEN 6441 Department of Computer Science and Software Engineering Concordia University 7.1 Outline SOEN 6441 1 Motivation Motivation Behavior parameterization 2 Behavior parameterization Predicates Predicates Strategy Design Pattern Filtering by Predicate Strategy Design Pattern Passing Code/Behavior Tackling verbosity Filtering by Predicate Anonymous classes Passing Code/Behavior Using a lambda expression Abstracting over List type Real-world examples 3 Tackling verbosity Summary Notes and Further Anonymous classes Reading Using a lambda expression Abstracting over List type Real-world examples 4 Summary 5 Notes and Further Reading 7.2 Coping with changing requirements SOEN 6441 Motivation Behavior parameterization Predicates Requirement 1: Filter green apples Strategy Design Pattern Filtering by Predicate Passing Code/Behavior public static List filterGreenApples(List inventory) { Tackling verbosity List result = new ArrayList(); Anonymous classes Using a lambda expression for(Apple apple: inventory){ Abstracting over List type if( "green".equals(apple.getColor() ) { Real-world examples Summary result.add(apple); Notes and Further } Reading } return result; } 7.3 Coping with changing requirements SOEN 6441 Requirement 2: Filter by arbitrary color Motivation Behavior public static List filterApplesByColor(List inventory, parameterization Predicates String color) { Strategy Design Pattern List result = new ArrayList(); Filtering by Predicate Passing Code/Behavior for (Apple apple: inventory){ Tackling verbosity if ( apple.getColor().equals(color) ) { Anonymous classes Using a lambda expression result.add(apple); Abstracting over List type } Real-world examples } Summary return result; Notes and Further Reading } 7.4 Coping with changing requirements SOEN 6441 Requirement 2: Filter by arbitrary color Motivation Behavior public static List filterApplesByColor(List inventory, parameterization Predicates String color) { Strategy Design Pattern List result = new ArrayList(); Filtering by Predicate Passing Code/Behavior for (Apple apple: inventory){ Tackling verbosity if ( apple.getColor().equals(color) ) { Anonymous classes Using a lambda expression result.add(apple); Abstracting over List type } Real-world examples } Summary return result; Notes and Further Reading } Calling List greenApples = filterApplesByColor(inventory, "green"); List redApples = filterApplesByColor(inventory, "red");... 7.4 Coping with changing requirements SOEN 6441 Motivation Behavior parameterization Requirement 3: Filter by weight Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior public static List filterApplesByWeight(List inventory, Tackling verbosity int weight) { Anonymous classes List result = new ArrayList(); Using a lambda expression Abstracting over List type for (Apple apple: inventory){ Real-world examples if ( apple.getWeight() > weight ){ Summary result.add(apple); Notes and Further Reading } } return result; } 7.5 Coping with changing requirements SOEN 6441 Requirement 4: Filter by weight or color Motivation public static List filterApples(List inventory, Behavior String color, parameterization int weight, Predicates Strategy Design Pattern boolean flag) { Filtering by Predicate Passing Code/Behavior List result = new ArrayList(); Tackling verbosity for (Apple apple: inventory){ Anonymous classes if ( (flag && apple.getColor().equals(color)) || Using a lambda expression Abstracting over List type (!flag && apple.getWeight() > weight) ){ Real-world examples result.add(apple); Summary } Notes and Further Reading } return result; } 7.6 Coping with changing requirements SOEN 6441 Requirement 4: Filter by weight or color Motivation public static List filterApples(List inventory, Behavior String color, parameterization int weight, Predicates Strategy Design Pattern boolean flag) { Filtering by Predicate Passing Code/Behavior List result = new ArrayList(); Tackling verbosity for (Apple apple: inventory){ Anonymous classes if ( (flag && apple.getColor().equals(color)) || Using a lambda expression Abstracting over List type (!flag && apple.getWeight() > weight) ){ Real-world examples result.add(apple); Summary } Notes and Further Reading } return result; } Calling List greenApples = filterApples(inventory, "green", 0, true); List heavyApples = filterApples(inventory, "", 150, false);... 7.6 Outline SOEN 6441 Motivation 1 Motivation Behavior parameterization Predicates Strategy Design Pattern 2 Behavior parameterization Filtering by Predicate Passing Code/Behavior Predicates Tackling verbosity Strategy Design Pattern Anonymous classes Using a lambda expression Filtering by Predicate Abstracting over List type Passing Code/Behavior Real-world examples Summary Notes and Further 3 Tackling verbosity Reading 4 Summary 5 Notes and Further Reading 7.7 Behavior parameterization SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes public interface ApplePredicate{ Using a lambda expression Abstracting over List type boolean test (Apple apple); Real-world examples } Summary Notes and Further Reading 7.8 Apple selection strategies SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading Copyright 2018 by Manning Publications Co., [UFM18] 7.9 ApplePredicate Implementations SOEN 6441 Motivation Behavior parameterization Predicates public class AppleHeavyWeightPredicate implements ApplePredicate { Strategy Design Pattern Filtering by Predicate public boolean test(Apple apple){ Passing Code/Behavior return apple.getWeight() > 150; Tackling verbosity Anonymous classes } Using a lambda expression } Abstracting over List type Real-world examples Summary public class AppleGreenColorPredicate implements ApplePredicate { Notes and Further public boolean test(Apple apple){ Reading return "green".equals(apple.getColor()); } } 7.10 Design Pattern SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading 7.11 Filtering by Predicate SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern public static List filterApples(List inventory, Filtering by Predicate Passing Code/Behavior ApplePredicate p){ Tackling verbosity List result = new ArrayList(); Anonymous classes for(Apple apple: inventory){ Using a lambda expression Abstracting over List type if(p.test(apple)){ Real-world examples result.add(apple); Summary } Notes and Further Reading } return result; } 7.12 Passing Code/Behavior SOEN 6441 Motivation Behavior parameterization Find red apples heavier than 150g? Predicates Strategy Design Pattern Filtering by Predicate public class AppleRedAndHeavyPredicate implements ApplePredicate{ Passing Code/Behavior public boolean test(Apple apple){ Tackling verbosity Anonymous classes return "red".equals(apple.getColor()) && apple.getWeight() > 150; Using a lambda expression } Abstracting over List type Real-world examples } Summary Notes and Further Reading Calling List redAndHeavyApples = filterApples(inventory, new AppleRedAndHeavyPredicate()); 7.13 Behavior Parameterization SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading Copyright 2018 by Manning Publications Co., [UFM18] 7.14 Multiple behaviors, one parameter SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading Copyright 2018 by Manning Publications Co., [UFM18] 7.15 Outline SOEN 6441 Motivation 1 Motivation Behavior parameterization Predicates Strategy Design Pattern 2 Behavior parameterization Filtering by Predicate Passing Code/Behavior Tackling verbosity 3 Tackling verbosity Anonymous classes Using a lambda expression Anonymous classes Abstracting over List type Real-world examples Using a lambda expression Summary Abstracting over List type Notes and Further Real-world examples Reading 4 Summary 5 Notes and Further Reading 7.16 Tackling verbosity SOEN 6441 Motivation Behavior public interface ApplePredicate{ parameterization boolean test (Apple apple); Predicates Strategy Design Pattern } Filtering by Predicate Passing Code/Behavior Tackling verbosity public class AppleHeavyWeightPredicate implements ApplePredicate{ Anonymous classes public boolean test(Apple apple){ Using a lambda expression Abstracting over List type return apple.getWeight() > 150; Real-world examples } Summary } Notes and Further Reading public class AppleGreenColorPredicate implements ApplePredicate{ public boolean test(Apple apple){ return "green".equals(apple.getColor()); } } 7.17 Tackling verbosity (II) SOEN 6441 public class FilteringApples{ public static void main(String...args){ Motivation List inventory = Arrays.asList(new Apple(80,"green"), Behavior new Apple(155, "green"), parameterization Predicates new Apple(120, "red")); Strategy Design Pattern List heavyApples = Filtering by Predicate Passing Code/Behavior filterApples(inventory, new AppleHeavyWeightPredicate()); Tackling verbosity List greenApples = Anonymous classes Using a lambda expression filterApples(inventory, new AppleGreenColorPredicate()); Abstracting over List type } Real-world examples Summary public static List filterApples(List inventory, Notes and Further Reading ApplePredicate p) { List result = new ArrayList(); for (Apple apple : inventory){ if (p.test(apple)){ result.add(apple); } } return result; } } 7.18 Anonymous classes SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity List redApples = filterApples(inventory, new ApplePredicate() { Anonymous classes public boolean test(Apple apple){ Using a lambda expression Abstracting over List type return "red".equals(apple.getColor()); Real-world examples } Summary }); Notes and Further Reading 7.19 Using a lambda expression SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression List result = Abstracting over List type filterApples(inventory, (Apple apple)->"red".equals(apple.getColor())); Real-world examples Summary Notes and Further Reading 7.20 Behavior parameterization vs. value parameterization SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading Copyright 2018 by Manning Publications Co., [UFM18] 7.21 Abstracting over List type SOEN 6441 Filter anything, not just Apple? public interface Predicate{ Motivation Behavior boolean test(T t); parameterization } Predicates Strategy Design Pattern Filtering by Predicate public static List filter(List list, Predicate p){ Passing Code/Behavior Tackling verbosity List result = new ArrayList(); Anonymous classes for(T e: list){ Using a lambda expression Abstracting over List type if(p.test(e)){ Real-world examples result.add(e); Summary } Notes and Further Reading } return result; } 7.22 Abstracting over List type SOEN 6441 Filter anything, not just Apple? public interface Predicate{ Motivation Behavior boolean test(T t); parameterization } Predicates Strategy Design Pattern Filtering by Predicate public static List filter(List list, Predicate p){ Passing Code/Behavior Tackling verbosity List result = new ArrayList(); Anonymous classes for(T e: list){ Using a lambda expression Abstracting over List type if(p.test(e)){ Real-world examples result.add(e); Summary } Notes and Further Reading } return result; } Calling List redApples = filter(inventory, (Apple apple) -> "red".equals(apple.getColor())); List evenNumbers = filter(numbers, (Integer i) -> i%2 == 0); 7.22 Sorting with a Comparator SOEN 6441 List.sort (since Java 8) Motivation // java.util.Comparator Behavior public interface Comparator { parameterization Predicates public int compare(T o1, T o2); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading 7.23 Sorting with a Comparator SOEN 6441 List.sort (since Java 8) Motivation // java.util.Comparator Behavior public interface Comparator { parameterization Predicates public int compare(T o1, T o2); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Implementing a Comparator using an anonymous class Abstracting over List type Real-world examples Summary inventory.sort(new Comparator() { Notes and Further public int compare(Apple a1, Apple a2){ Reading return a1.getWeight().compareTo(a2.getWeight()); } }); 7.23 Sorting with a Comparator SOEN 6441 List.sort (since Java 8) Motivation // java.util.Comparator Behavior public interface Comparator { parameterization Predicates public int compare(T o1, T o2); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Implementing a Comparator using an anonymous class Abstracting over List type Real-world examples Summary inventory.sort(new Comparator() { Notes and Further public int compare(Apple a1, Apple a2){ Reading return a1.getWeight().compareTo(a2.getWeight()); } }); With a lambda expression inventory.sort( (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())); 7.23 Executing a block of code with Runnable SOEN 6441 Threads Motivation // java.lang.Runnable Behavior parameterization public interface Runnable{ Predicates public void run(); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Summary Notes and Further Reading 7.24 Executing a block of code with Runnable SOEN 6441 Threads Motivation // java.lang.Runnable Behavior parameterization public interface Runnable{ Predicates public void run(); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Creating a Thread using an anonymous class Abstracting over List type Real-world examples Summary Thread t = new Thread(new Runnable() { Notes and Further public void run() { Reading System.out.println("Hello world"); } }); 7.24 Executing a block of code with Runnable SOEN 6441 Threads Motivation // java.lang.Runnable Behavior parameterization public interface Runnable{ Predicates public void run(); Strategy Design Pattern Filtering by Predicate } Passing Code/Behavior Tackling verbosity Anonymous classes Using a lambda expression Creating a Thread using an anonymous class Abstracting over List type Real-world examples Summary Thread t = new Thread(new Runnable() { Notes and Further public void run() { Reading System.out.println("Hello world"); } }); With a lambda expression Thread t = new Thread(() -> System.out.println("Hello world")); 7.24 GUI event handling SOEN 6441 Motivation Behavior parameterization JavaFX EventHandler using an anonymous class Predicates Strategy Design Pattern Filtering by Predicate Button button = new Button("Send"); Passing Code/Behavior button.setOnAction(new EventHandler() { Tackling verbosity Anonymous classes public void handle(ActionEvent event) { Using a lambda expression label.setText("Sent!!"); Abstracting over List type Real-world examples } Summary }); Notes and Further Reading 7.25 GUI event handling SOEN 6441 Motivation Behavior parameterization JavaFX EventHandler using an anonymous class Predicates Strategy Design Pattern Filtering by Predicate Button button = new Button("Send"); Passing Code/Behavior button.setOnAction(new EventHandler() { Tackling verbosity Anonymous classes public void handle(ActionEvent event) { Using a lambda expression label.setText("Sent!!"); Abstracting over List type Real-world examples } Summary }); Notes and Further Reading With a lambda expression button.setOnAction((ActionEvent event) -> label.setText("Sent!!")); 7.25 Outline SOEN 6441 Motivation Behavior parameterization 1 Motivation Predicates Strategy Design Pattern Filtering by Predicate Passing Code/Behavior 2 Behavior parameterization Tackling verbosity Anonymous classes Using a lambda expression 3 Tackling verbosity Abstracting over List type Real-world examples Summary 4 Summary Notes and Further Reading 5 Notes and Further Reading 7.26 Summary SOEN 6441 Behavior parameterization Motivation Behavior Ability for a method to take multiple different behaviors as parameters and use parameterization Predicates them internally to accomplish different behaviors Strategy Design Pattern Filtering by Predicate Lets you make your code more adaptive to changing requirements and saves Passing Code/Behavior on engineering efforts in the future Tackling verbosity Anonymous classes Using a lambda expression Abstracting over List type Real-world examples Implementation Summary Notes and Further Passing code is a way to give new behaviors as arguments to a method Reading Very verbose prior to Java 8 Anonymous classes were used to get rid of the verbosity associated with declaring multiple concrete classes for an interface that are needed only once The Java API contains many methods that can be parameterized with different behaviors, which include sorting, threads, and GUI handling Much more concise, readable, and flexible with Java 8+ lambdas 7.27 Outline SOEN 6441 1 Motivation Motivation Behavior parameterization 2 Behavior parameterization Predicates Predicates Strategy Design Pattern Filtering by Predicate Strategy Design Pattern Passing Code/Behavior Tackling verbosity Filtering by Predicate Anonymous classes Passing Code/Behavior Using a lambda expression Abstracting over List type Real-world examples 3 Tackling verbosity Summary Notes and Further Anonymous classes Reading Using a lambda expression Abstracting over List type Real-world examples 4 Summary 5 Notes and Further Reading 7.28 Reading Material SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate Required Passing Code/Behavior Tackling verbosity Anonymous classes [UFM18, Chapter 2] (Passing code with behavior parameterization ) Using a lambda expression Abstracting over List type Real-world examples Summary Supplemental Notes and Further Reading [GHJV95] (Strategy Design Pattern) 7.29 References SOEN 6441 Motivation Behavior parameterization Predicates Strategy Design Pattern Filtering by Predicate [GHJV95] E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Passing Code/Behavior Tackling verbosity Design Patterns: Elements of Reusable Object-Oriented Software. Anonymous classes Addison-Wesley, 1995. Using a lambda expression Abstracting over List type Real-world examples [UFM18] Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft. Summary Modern Java in Action: Lambdas, streams, functional and reactive Notes and Further Reading programming. Manning Publications, 2018. 7.30