Java8 - Interfaces, evolved

443 views

Published on

A little talk on Java 8 interfaces I gave a few months back at FastConnect.

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
443
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java8 - Interfaces, evolved

  1. 1. Interfaces, evolved
  2. 2. Interfaces, evolved static methods default methods traits functional interfaces
  3. 3. Static Methods
  4. 4. Static Methods //No different from static methods from classes public interface StaticFoo { static void foo() { System.out.println("Static implementation of foo"); } }
  5. 5. Default Methods
  6. 6. Default Methods Previously, on Java 7
  7. 7. Default Methods signature class vision… implem contract… Java 7 – Interfaces
  8. 8. Default Methods abstract class utility class static methods copy / paste Java 7 – Sharing Implementations
  9. 9. Default Methods Now, in Java 8…
  10. 10. Default Methods public interface Foo { public default void foo() { System.out.println("Default implementation of foo()"); } } Java 8 – Basic Syntax
  11. 11. Default Methods public interface Itf { //No implementation; will be overridden public void foo(); //Default implementation; will be overriden public default void bar() { System.out.println("Itf -> bar() [DEFAULT]"); } //Default implementation; will not be overriden public default void baz() { System.out.println("Itf -> baz() [DEFAULT]"); } } Java 8 – Call &Overload
  12. 12. Default Methods public class Clazz implements Itf { @Override public void foo() { System.out.println("Clazz -> foo()"); } @Override public void bar() { System.out.println("Clazz -> bar()"); } } Java 8 – Call &Overload
  13. 13. Default Methods public class Test { public static void main(String[] args) { Clazz clz = new Clazz(); clz.foo(); clz.bar(); clz.baz(); } } Java 8 – Call &Overload Clazz -> foo() Clazz -> bar() Itf -> baz() [DEFAULT]
  14. 14. Default Methods Diamond Inheritance
  15. 15. Default Methods public interface InterfaceA { public default void foo() {System.out.println("A -> foo()");} } public interface InterfaceB { public default void foo() {System.out.println("B -> foo()");} } public class Test implements InterfaceA, InterfaceB { //Whoops } Diamond Inheritance error: classTest inherits unrelated defaults for foo() from types InterfaceA and InterfaceB
  16. 16. Default Methods //Solution 1 : the class method has the priority public class Test implements InterfaceA, InterfaceB { @Override public void foo() { System.out.println("Test -> foo()"); } } Diamond Inheritance
  17. 17. Default Methods //Solution 2 : we specify which method we want to call public class Test implements InterfaceA, InterfaceB { @Override public void foo() { InterfaceA.super.foo(); } } Diamond Inheritance
  18. 18. Default Methods Reflection & Proxies
  19. 19. Default Methods public static void main(String[] args) { Object proxy = Proxy.newProxyInstance( Test.class.getClassLoader(), new Class[]{InterfaceA.class, InterfaceB.class}, (targetProxy, targetMethod, targetMethodArgs) -> { System.out.println(targetMethod.toGenericString()); return null; }); ((InterfaceA) proxy).foo(); ((InterfaceB) proxy).foo(); } Reflection & Proxies public default void defaultmethods.InterfaceA.foo() public default void defaultmethods.InterfaceA.foo()
  20. 20. Default Methods public static void main(String[] args) { Object proxy = Proxy.newProxyInstance( Test.class.getClassLoader(), new Class[]{InterfaceA.class, InterfaceB.class}, (targetProxy, targetMethod, targetMethodArgs) -> { System.out.println(targetMethod.toGenericString()); return null; }); ((InterfaceB) proxy).foo(); ((InterfaceA) proxy).foo(); } Reflection & Proxies public default void defaultmethods.InterfaceA.foo() public default void defaultmethods.InterfaceA.foo()
  21. 21. Default Methods public static void main(String[] args) { Object proxy = Proxy.newProxyInstance( Test.class.getClassLoader(), new Class[]{InterfaceB.class, InterfaceA.class}, (targetProxy, targetMethod, targetMethodArgs) -> { System.out.println(targetMethod.toGenericString()); return null; }); ((InterfaceA) proxy).foo(); ((InterfaceB) proxy).foo(); } Reflection & Proxies public default void defaultmethods.InterfaceB.foo() public default void defaultmethods.InterfaceB.foo()
  22. 22. Default Methods When two or more interfaces of a proxy class contain a method with the same name and parameter signature, the order of the proxy class's interfaces becomes significant. When such a duplicate method is invoked on a proxy instance, the Method object passed to the invocation handler will not necessarily be the one whose declaring class is assignable from the reference type of the interface that the proxy's method was invoked through.This limitation exists because the corresponding method implementation in the generated proxy class cannot determine which interface it was invoked through. Therefore, when a duplicate method is invoked on a proxy instance, the Method object for the method in the foremost interface that contains the method (either directly or inherited through a superinterface) in the proxy class's list of interfaces is passed to the invocation handler's invoke method, regardless of the reference type through which the method invocation occurred. java.lang.reflect.Proxy, 2014 Reflection & Proxies
  23. 23. Traits functional interfaces VS
  24. 24. Traits
  25. 25. Traits From what I understood, a trait encapsulates a single behavior.
  26. 26. Traits abstract method derived methods trait …with their default implementation
  27. 27. Traits public interface Orderable<T> extends Comparable<T> { //everything relies on the implementation of compareTo public default boolean isAfter(T other) { return compareTo(other) > 0; } public default boolean isBefore(T other) { return compareTo(other) < 0; } public default boolean isSameAs(T other) { return compareTo(other) == 0; } }
  28. 28. Traits public class Person implements Orderable<Person> { private final String name; public Person(String name) { this.name = name; } @Override public int compareTo(Person other) { return name.compareTo(other.name); } }
  29. 29. Traits public class Test { public static void main(String[] args) { Person tony = new Person("Tony Stark"); Person bruce = new Person("Dr. Robert Bruce Banner"); println("Tony compareto Bruce : " + tony.compareTo(bruce)); println("Tony > Bruce : " + tony.isAfter(bruce)); println("Tony < Bruce : " + tony.isBefore(bruce)); println("Tony == Bruce : " + tony.isSameAs(bruce)); } } Tony compareTo Bruce : 16 Tony > Bruce : true Tony < Bruce : false Tony == Bruce : false
  30. 30. Functional Interfaces
  31. 31. Functional Interfaces one abstract / many defaults…
  32. 32. Functional Interfaces @FunctionalInterface // optional public interface Foo { // 1 and only 1 abstract method void foo(); // many defaults default void bar() {…} default void baz() {…} … } one abstract / many defaults…
  33. 33. Functional Interfaces @FunctionalInterface public interface Foo { void foo(); void bar(); // whoops, one too many } one abstract / many defaults… error: Foo is not a functional interface multiple non-overriding abstract methods found in interface Foo 1 error
  34. 34. Functional Interfaces instance method constructor static method lambda …to pass them all
  35. 35. Functional Interfaces public class Name { private String firstName; private String lastName; public Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } } Java 7
  36. 36. Functional Interfaces public class NameParser<T> { public T parse(String name,Creator<T> creator) { String[] tokens = name.split(" "); String firstName = tokens[0]; String lastName = tokens[1]; return creator.create(firstName, lastName); } } Java 7
  37. 37. Functional Interfaces public interface Creator<T> { T create(String firstName, String lastName); } Java 7
  38. 38. Functional Interfaces public static void main(String[] args) { NameParser<Name> parser = new NameParser(); Name res = parser.parse("Bruce Banner", new Creator<Name>() { @Override public Name create(String firstName, String lastName) { return new Name(firstName, lastName); } }); } Java 7
  39. 39. Functional Interfaces @FunctionalInterface public interface Creator<T> { T create(String firstName, String lastName); } Java 8
  40. 40. Functional Interfaces Name res = parser.parse("Bruce Banner", Name::new); Java 8 CONSTRUCTOR
  41. 41. Functional Interfaces public class Factory { public static Name createName(String firstName, String lastName) { return new Name(firstName, lastName); } } Name res = parser.parse("Bruce Banner", Factory::createName); Java 8 STATICMETHOD
  42. 42. Functional Interfaces public class Factory { public Name createName(String firstName, String lastName) { return new Name(firstName, lastName); } } Factory factory = new Factory(); Name res = parser.parse("Bruce Banner", factory::createName); Java 8 INSTANCEMETHOD
  43. 43. Functional Interfaces Name res = parser.parse("Bruce Banner", (s1, s2) -> new Name(s1, s2)); Java 8 LAMBDAEXPRESSIONS
  44. 44. Functional Interfaces java.util.function
  45. 45. Functional Interfaces java.util.function
  46. 46. Biblio
  47. 47. Biblio  CROISIER, Olivier. Java 8 : du neuf dans les interfaces !, 2014 http://thecodersbreakfast.net/index.php?post/2014/01/20/Java8- du-neuf-dans-les-interfaces  HORSTMANN, Cay S.. Java SE 8 for the Really Impatient. Addison- Wesley, 2014  SEIGNEURIN,Alexis. Java 8 – Interfaces fonctionnelles, 2014 http://blog.ippon.fr/2014/03/18/java-8-interfaces-fonctionnelles/

×