Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Scala as "Better Java"             from object-oriented viewpoint                         Shinya Mochizuki                ...
About this talk                            Main Topic                                             Object-Oriented Progra...
Difficulty                                   Easy! But...                         Difficulty != Importancehttps://lepidum....
What is Scala?                         FP + Traditional OO like Java                  FP + Improved OO compared to Javahtt...
What is a pretty program?                            Structured by reusable components.                            Compo...
What is a component?                            Structured by components.                                             Mo...
Implmentation inheritance                           Component composition by OO feature.                              pub...
Lack of language feature                            No multiple composition in Java                                     ...
Composition direction                            Single inheritance                                             Only ver...
Scalas feature: Traits and mixin                            Traits are like Javas interfaces, but                       ...
In Scala: Implmentation mixin                          Using trait instead of class.                            trait Log...
Lack of language feature                            Implementation only Inheritance.                                    ...
Lack of language feature causes...                            Repeated patterns.                                        ...
Aggregation (aka Composition)                          Well-known alternative.                            public class Re...
Hard linking problem                            For a component to be reusable, it must:                                ...
Dependency Injection (DI) Pattern                          Well-known pattern in Java community.                         ...
Dependency Injection (DI) Pattern                            Problems                                             Wiring...
Lack of language feature                            Java dosent have features for                                       ...
Lack of language feature causes...                            Work-around libraries/frameworks.                         ...
Using DI Container                            For example, in Guice                           public class Receptionist {...
Using DI Container                            Resolved                                             Wiring (Composition) ...
Lack of language feature causes...                            Createing some common patterns.                           ...
Scalas feature: Self type annotation                            Expressing dependencies                           class R...
Scalas feature: Self type annotation                            Flexible composition.                            val rece...
Self type annotation vs DI Container                          Static vs Dynamic?                          Flexibility   ...
Is self type annotation perfect?                         No, unfortunately it is not...https://lepidum.co.jp/         Copy...
Fibonacci number                            Easy and simple example.                         public class Fib {          ...
Extend Fib                           Memoization, easy.                         public class MemoFib extends Fib {       ...
Extend Fib                            Logging, easy.                         public class LogFib extends Fib {           ...
Combination explosion                            Combine extension features.                              public class Me...
Decorator pattern (GoF)                            Specialized DI Pattern.                            public interface IF...
Decorator pattern (GoF)                            Problems                                             Composition is t...
Try to apply self type annotation                         The result ... ?                            class Fib extends I...
Scalas feature: Abstract override                          Modifier for overriding abstract method.                      ...
Scalas feature: Abstract override                            Flexible combination with mixin.                            ...
Summary of Scalas OO features                           Traits and mixin                                             pro...
Benefities of the language support                           Patterns can resolve problems, but...                      ...
Scale, Scale, Scale!                          Out of scopes                                           Sealed modifier, c...
Q&A                         Any questions?https://lepidum.co.jp/   Copyright © 2004-2013 Lepidum Co. Ltd. All rights reser...
Thanks                         Scale Your Programming!https://lepidum.co.jp/       Copyright © 2004-2013 Lepidum Co. Ltd. ...
Upcoming SlideShare
Loading in …5
×

Scala as "Better Java" from object-oriented viewpoint

2,739 views

Published on

http://www.scalaconf.jp/en/program/index.html#A15102

Scala as "Better Java" from object-oriented viewpoint

  1. 1. Scala as "Better Java" from object-oriented viewpoint Shinya Mochizuki lepidum Co Ltd. 02 March 2013https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  2. 2. About this talk  Main Topic  Object-Oriented Programming  No Functional Programming!https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  3. 3. Difficulty Easy! But... Difficulty != Importancehttps://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  4. 4. What is Scala? FP + Traditional OO like Java FP + Improved OO compared to Javahttps://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  5. 5. What is a pretty program?  Structured by reusable components.  Components is expressed as  Function, Class, Module, etc.  In OO, a component is basically class.  More precisely, class instances.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  6. 6. What is a component?  Structured by components.  Mostly smaller.  Component composition is expressed as  “composition” != Composite Pattern (GoF)  Function composition, Inheritance, Include, etc.  In OO, composition is inheritance.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  7. 7. Implmentation inheritance  Component composition by OO feature. public class Logger {...} public class Counter extends Logger {...} public class Receptionist extends Counter {...}  Problems  Unintended polymorphism.  Counter is not a Logger! Neither is Receptionist.  Publishing implementation details.  Receptionist inherits Counter and Logger public methods.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  8. 8. Lack of language feature  No multiple composition in Java  e.g. Multiple inheritance like C++https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  9. 9. Composition direction  Single inheritance  Only vertically.  Multiple inheritance  Vertically and horizontally.  But has some problems.  e.g. conflict method resolution  In Scala?https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  10. 10. Scalas feature: Traits and mixin  Traits are like Javas interfaces, but  can contain arbitrary method implementations.  Class/Trait can inherit from arbitrary trait.  Inheriting traits is called “mixin”.  Trait as component  can be composed horizontally.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  11. 11. In Scala: Implmentation mixin  Using trait instead of class. trait Logger {...} trait Counter {...} class Receptionist extends Logger with Counter {...}  Resolved  Unintended polymorphism.  Unresolved  Publishing implementation details.  Problems  Each component is hard linked.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  12. 12. Lack of language feature  Implementation only Inheritance.  Like private inheritance of C++.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  13. 13. Lack of language feature causes...  Repeated patterns.  Like GoFs Design Patterns.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  14. 14. Aggregation (aka Composition)  Well-known alternative. public class Receptionist { private Logger logger = new Logger(); private Counter counter = new Counter(); ... } ● Resolved  Publishing implementation details.  Unresolved  Each component is hard linked.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  15. 15. Hard linking problem  For a component to be reusable, it must:  Remove hard links.  Abstract dependencies (aka interface extraction). public interface ILogger {...} public interface ICounter {...} public class Logger implements ILogger {...} public class Counter implements ICounter {...}https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  16. 16. Dependency Injection (DI) Pattern  Well-known pattern in Java community. public class Receptionist { private ILogger logger; private ICounter counter; public Receptionist(ILogger logger, ICounter counter) {...} ... }  Resolved  Each component is hard linked.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  17. 17. Dependency Injection (DI) Pattern  Problems  Wiring is too verbose. ILogger logger = new Logger(...); ICounter counter = new Counter(...); Receptionist receptionist = new Receptionist(logger, counter);  Unclear dependencies between components.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  18. 18. Lack of language feature  Java dosent have features for  Expressing dependencies.  Flexible composition.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  19. 19. Lack of language feature causes...  Work-around libraries/frameworks.  DI Container frameworks provide it.  e.g. Guice, Spring Framework, Seaser, etc.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  20. 20. Using DI Container  For example, in Guice public class Receptionist { @Inject private ILogger logger; @Inject private ICounter counter; ... }  Resolved  Unclear dependencies between components.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  21. 21. Using DI Container  Resolved  Wiring (Composition) is too verbose. public class Module extends AbstractModule { @Override protected void configure() { bind(ILogger.class).to(Logger.class); bind(ICounter.class).to(Counter.class); } }https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  22. 22. Lack of language feature causes...  Createing some common patterns.  Work-around libraries/frameworks. Scala has the features that Java lacks!https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  23. 23. Scalas feature: Self type annotation  Expressing dependencies class Receptionist { this: ILogger with ICounter => ... }  Resolved  Unclear dependencies between components.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  24. 24. Scalas feature: Self type annotation  Flexible composition. val receptionist = new Receptionist with Logger with Counter  Resolved  Wiring (Composition) is too verbose.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  25. 25. Self type annotation vs DI Container  Static vs Dynamic?  Flexibility  Static < Dynamic  DI Container wins?  But static methods can compound dynamic methods.  To be precise, Static + Dynamic vs Dynamic!  Static + Dynamic > Dynamichttps://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  26. 26. Is self type annotation perfect? No, unfortunately it is not...https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  27. 27. Fibonacci number  Easy and simple example. public class Fib { int fib(int n) { if (n <= 1) return n; else return fib(n – 1) + fib(n – 2); } }https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  28. 28. Extend Fib  Memoization, easy. public class MemoFib extends Fib { private Map<Integer, Integer> tbl = new HashMap<Integer, Integer>(); @Override int fib(int n) { if (tbl.containsKey(n)) return tbl.get(n); int result = super.fib(n); tbl.put(n, result); return result; } }https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  29. 29. Extend Fib  Logging, easy. public class LogFib extends Fib { @Override int fib(int n) { System.out.println(“Entering fib(“ + n + “)”); int ret = super.fib(n); println(“Exiting fib(“ + n + “) = “ + ret); return ret; } }https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  30. 30. Combination explosion  Combine extension features. public class MemoLogFib extends LogFib {...} public class LogMemoFib extends MemoFib {...}  Problems  Each component is hard linked.  Code duplication.  Code of MemoFib and MemoLogFib are duplicated.  The same is true for LogFib and LogMemoFib.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  31. 31. Decorator pattern (GoF)  Specialized DI Pattern. public interface IFib {...} public class Fib implements IFib {...} public class MemoFib implements IFib {...} public class LogFib implements IFib {...}  Resolved  Each component is hard linked.  Code duplication.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  32. 32. Decorator pattern (GoF)  Problems  Composition is too verbose. IFib fib = new MemoFib( new LogFib( new Fib() ) )https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  33. 33. Try to apply self type annotation  The result ... ? class Fib extends IFib {...} trait MemoFib extends IFib { this: IFib => def fib(n: Int) = ... // how to refer composited fib? } new Fib with MemoFib // oops, error! Lacking override modifier  Problems  Can not refer to its own required components.  Can not composit components.  The Cause  Provided and required components are same.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  34. 34. Scalas feature: Abstract override  Modifier for overriding abstract method. trait LogFib extends IFib { abstract override def fib(n: Int): Int = { println(s“Entering fib($n)”) val ret = super.fib(n) println(s“Exiting fib($n) = $ret”) ret }  Resolved  Can not refer to its own required components.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  35. 35. Scalas feature: Abstract override  Flexible combination with mixin. new Fib with LogFib with MemoFib fib 42 new Fib with MemoFib with LogFib fib 42  Resolved  Combination is too verbose.  Can not composit components.  Improved  Resolving abstract method at composition timing.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  36. 36. Summary of Scalas OO features  Traits and mixin  provides flexible component composition.  Self type annotation  expresses required other components.  Abstract override  refers to abstract methods by super.  can handle some corner case of self type annotation.  provided and required component are same.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  37. 37. Benefities of the language support  Patterns can resolve problems, but...  Mostly verbose.  Need for common language. ● Frameworks can resolve problems, but...  A little verbose compared with language feature.  Sometimes too large.  Language support needs for  reusability, readability and writability.https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  38. 38. Scale, Scale, Scale!  Out of scopes  Sealed modifier, case class, pattern match and extractor  As (G)ADTs and pattern match  (Abstract) type member  Structural subtype  Implicit conversion and view bounds  Implicit parameter and context bounds  As type classes  Path/method dependent types  Etc...https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  39. 39. Q&A Any questions?https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  40. 40. Thanks Scale Your Programming!https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.

×