SlideShare uma empresa Scribd logo
1 de 40
Baixar para ler offline
Scala as "Better Java"
             from object-oriented viewpoint

                         Shinya Mochizuki
                         lepidum Co Ltd.
                          02 March 2013

https://lepidum.co.jp/   Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
About this talk
          
                  Main Topic
                    
                         Object-Oriented Programming
                    
                         No Functional Programming!




https://lepidum.co.jp/               Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
Difficulty


                                   Easy! But...

                         Difficulty != Importance




https://lepidum.co.jp/       Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
What is Scala?


                         FP + Traditional OO like Java

                  FP + Improved OO compared to Java




https://lepidum.co.jp/          Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
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.
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.
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.
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.
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.
Scala's feature: Traits and mixin
          
                  Traits are like Java's 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.
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.
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.
Lack of language feature causes...
          
                  Repeated patterns.
                    
                         Like GoF's Design Patterns.




https://lepidum.co.jp/                Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
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.
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.
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.
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.
Lack of language feature
          
                  Java dosen't have features for
                    
                         Expressing dependencies.
                    
                         Flexible composition.




https://lepidum.co.jp/                Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
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.
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.
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.
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.
Scala's 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.
Scala's 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.
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 > Dynamic




https://lepidum.co.jp/                  Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
Is self type annotation perfect?



                         No, unfortunately it is not...




https://lepidum.co.jp/         Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
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.
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.
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.
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.
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.
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.
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.
Scala's 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.
Scala's 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.
Summary of Scala's 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.
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.
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.
Q&A



                         Any questions?




https://lepidum.co.jp/   Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
Thanks



                         Scale Your Programming!




https://lepidum.co.jp/       Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.

Mais conteúdo relacionado

Semelhante a Scala as "Better Java" from object-oriented viewpoint

BP203 limitless languages
BP203 limitless languagesBP203 limitless languages
BP203 limitless languagesMark Myers
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzJAX London
 
Enterprise Applications With OSGi and SpringSource dm Server
Enterprise Applications With OSGi and SpringSource dm ServerEnterprise Applications With OSGi and SpringSource dm Server
Enterprise Applications With OSGi and SpringSource dm ServerSam Brannen
 
What's Expected in Java 7
What's Expected in Java 7What's Expected in Java 7
What's Expected in Java 7Gal Marder
 
Greenplum Database on HDFS
Greenplum Database on HDFSGreenplum Database on HDFS
Greenplum Database on HDFSDataWorks Summit
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sampleprahalad_das_in
 
OSGi Best Practices - Tim Ward
OSGi Best Practices - Tim WardOSGi Best Practices - Tim Ward
OSGi Best Practices - Tim Wardmfrancis
 
Tales About Scala Performance
Tales About Scala PerformanceTales About Scala Performance
Tales About Scala PerformanceHaim Yadid
 
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developersnick_garrod
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMKris Mok
 
Graal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution PlatformGraal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution PlatformThomas Wuerthinger
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainAttila Szegedi
 
Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.Gunnar Wagenknecht
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java LanguagePawanMM
 
Raphael Amorim - Scrating React Fiber
Raphael Amorim - Scrating React FiberRaphael Amorim - Scrating React Fiber
Raphael Amorim - Scrating React FiberReact Conf Brasil
 
Dark side of the reflect
Dark side of the reflectDark side of the reflect
Dark side of the reflectsairoutine
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Codeeddiehaber
 

Semelhante a Scala as "Better Java" from object-oriented viewpoint (20)

BP203 limitless languages
BP203 limitless languagesBP203 limitless languages
BP203 limitless languages
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
Intel open mp
Intel open mpIntel open mp
Intel open mp
 
Enterprise Applications With OSGi and SpringSource dm Server
Enterprise Applications With OSGi and SpringSource dm ServerEnterprise Applications With OSGi and SpringSource dm Server
Enterprise Applications With OSGi and SpringSource dm Server
 
What's Expected in Java 7
What's Expected in Java 7What's Expected in Java 7
What's Expected in Java 7
 
Greenplum Database on HDFS
Greenplum Database on HDFSGreenplum Database on HDFS
Greenplum Database on HDFS
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sample
 
OSGi Best Practices - Tim Ward
OSGi Best Practices - Tim WardOSGi Best Practices - Tim Ward
OSGi Best Practices - Tim Ward
 
Tales About Scala Performance
Tales About Scala PerformanceTales About Scala Performance
Tales About Scala Performance
 
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
 
Graal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution PlatformGraal VM: Multi-Language Execution Platform
Graal VM: Multi-Language Execution Platform
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages Toolchain
 
Ruby
RubyRuby
Ruby
 
Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.Managing large and distributed Eclipse server applications.
Managing large and distributed Eclipse server applications.
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Clean Code
Clean CodeClean Code
Clean Code
 
Raphael Amorim - Scrating React Fiber
Raphael Amorim - Scrating React FiberRaphael Amorim - Scrating React Fiber
Raphael Amorim - Scrating React Fiber
 
Dark side of the reflect
Dark side of the reflectDark side of the reflect
Dark side of the reflect
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 

Scala as "Better Java" from object-oriented viewpoint

  • 1. Scala as "Better Java" from object-oriented viewpoint Shinya Mochizuki lepidum Co Ltd. 02 March 2013 https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 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. Difficulty Easy! But... Difficulty != Importance https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 4. What is Scala? FP + Traditional OO like Java FP + Improved OO compared to Java https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 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. 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. 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. 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. 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. Scala's feature: Traits and mixin  Traits are like Java's 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. 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. 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. Lack of language feature causes...  Repeated patterns.  Like GoF's Design Patterns. https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 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. 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. 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. 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. Lack of language feature  Java dosen't have features for  Expressing dependencies.  Flexible composition. https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 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. 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. 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. 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. Scala's 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. Scala's 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. 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 > Dynamic https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 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. 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. 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. 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. 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. 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. 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. 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. Scala's 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. Scala's 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. Summary of Scala's 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. 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. 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. Q&A Any questions? https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.
  • 40. Thanks Scale Your Programming! https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved.