scala as "better java" from object-oriented viewpoint

40
https://lepidum.co.jp/ Copyright © 2004-2013 Lepidum Co. Ltd. All rights reserved. Scala as "Better Java" from object-oriented viewpoint Shinya Mochizuki lepidum Co Ltd. 02 March 2013

Upload: lyricallogical

Post on 19-Jun-2015

1.637 views

Category:

Documents


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Scala as "Better Java" from object-oriented viewpoint

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

Scala as "Better Java"from object-oriented viewpoint

Shinya Mochizukilepidum Co Ltd.02 March 2013

Page 2: Scala as "Better Java" from object-oriented viewpoint

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

About this talk

Main Topic Object-Oriented Programming No Functional Programming!

Page 3: Scala as "Better Java" from object-oriented viewpoint

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

Difficulty

Easy! But...

Difficulty != Importance

Page 4: Scala as "Better Java" from object-oriented viewpoint

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

What is Scala?

FP + Traditional OO like Java

FP + Improved OO compared to Java

Page 5: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 6: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 7: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 8: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature

No multiple composition in Java e.g. Multiple inheritance like C++

Page 9: Scala as "Better Java" from object-oriented viewpoint

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

Composition direction

Single inheritance Only vertically.

Multiple inheritance Vertically and horizontally. But has some problems.

e.g. conflict method resolution

In Scala?

Page 10: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 11: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 12: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature

Implementation only Inheritance. Like private inheritance of C++.

Page 13: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature causes...

Repeated patterns. Like GoF's Design Patterns.

Page 14: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 15: Scala as "Better Java" from object-oriented viewpoint

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

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 {...}

Page 16: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 17: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 18: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature

Java dosen't have features for Expressing dependencies. Flexible composition.

Page 19: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature causes...

Work-around libraries/frameworks.

DI Container frameworks provide it. e.g. Guice, Spring Framework, Seaser, etc.

Page 20: Scala as "Better Java" from object-oriented viewpoint

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

Using DI Container

For example, in Guicepublic class Receptionist {

@Inject private ILogger logger;

@Inject private ICounter counter;

...

}

Resolved Unclear dependencies between components.

Page 21: Scala as "Better Java" from object-oriented viewpoint

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

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);

}

}

Page 22: Scala as "Better Java" from object-oriented viewpoint

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

Lack of language feature causes...

Createing some common patterns. Work-around libraries/frameworks.

Scala has the features that Java lacks!

Page 23: Scala as "Better Java" from object-oriented viewpoint

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

Scala's feature: Self type annotation

Expressing dependenciesclass Receptionist {

this: ILogger with ICounter =>

...

}

Resolved Unclear dependencies between components.

Page 24: Scala as "Better Java" from object-oriented viewpoint

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

Scala's feature: Self type annotation

Flexible composition.val receptionist =

new Receptionist with Logger with Counter

Resolved Wiring (Composition) is too verbose.

Page 25: Scala as "Better Java" from object-oriented viewpoint

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

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

Page 26: Scala as "Better Java" from object-oriented viewpoint

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

Is self type annotation perfect?

No, unfortunately it is not...

Page 27: Scala as "Better Java" from object-oriented viewpoint

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

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);

}}

Page 28: Scala as "Better Java" from object-oriented viewpoint

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

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; }}

Page 29: Scala as "Better Java" from object-oriented viewpoint

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

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; }}

Page 30: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 31: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 32: Scala as "Better Java" from object-oriented viewpoint

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

Decorator pattern (GoF)

Problems Composition is too verbose.

IFib fib =

new MemoFib(

new LogFib(

new Fib()

)

)

Page 33: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 34: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 35: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 36: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 37: Scala as "Better Java" from object-oriented viewpoint

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

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.

Page 38: Scala as "Better Java" from object-oriented viewpoint

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

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...

Page 39: Scala as "Better Java" from object-oriented viewpoint

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

Q & A

Any questions?

Page 40: Scala as "Better Java" from object-oriented viewpoint

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

Thanks

Scale Your Programming!