Refactoring Design Patterns the Functional Way (in Scala)

30
@kfirondev Refactoring Design Patterns the Functional Way (in Scala) @kfirondev Kfir Bloch Backend engineering manager

Transcript of Refactoring Design Patterns the Functional Way (in Scala)

Page 1: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Kfir BlochBackend engineering manager

Page 2: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

What the hell is functional programming?

Page 3: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

• Treats computation as the evaluation of mathematical functions

• Avoid changing state and mutable data• Declarative• Pure functions with no side effects

http://www.clker.com/

Page 4: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

case class Salad(name: String, veggy: Boolean, chopped: Boolean)

Immutability via Scala case class

Page 5: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

sealed trait Salad case class Lentils() extends Salad case class Hummus() extends Salad case class Tahini() extends Salad case class SaladInfo(salad: Salad, spices: Seq[String])

def makeSaladInfo(salad: Salad, f: Salad => Seq[String]): SaladInfo = SaladInfo(salad, f(salad))

// usage makeSaladInfo(Hummus(), spicer)

def spicer(salad: AnyRef): Seq[String] = salad match { case l: Lentils => Seq("Salt", "Olive Oil", "Red paper") case h: Hummus => Seq("Salt", "Garlic", "Onion") case t: Tahini => Seq("Salt", "Garlic", "Lemon") }

Higher order function

Page 6: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

def doSomething(msg: String): Unit= { val formatted = msg.toUpperCase logger.log(Level.WARNING, formatted) }

Function with side effects

Page 7: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

def doSomething(msg: String): String = { val formatted = msg.toUpperCase formatted }

Pure function

Page 8: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

It leads naturally to Hexagonal Architecture(ports & adapters)http://blog.mattwynne.net/2012/05/31/hexagonal-rails-objects-values-and-hexagons/comment-

page-1/

Page 9: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Hexagonal Architecture with FP gives result which is:

Deterministic Separation of concerns Fast testing lifecycle Concurrent agnostic domain model

Page 10: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

People told you that ->

Page 11: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

People told you that ->

Abstract factory

Builder

Factory Method

Builder

Prototype

Adapter

Bridge

Composite

Decorator

Proxy

Chain of responsibility

Command

Iterator

Mediator

Observer

Object Oriented

http://www.clker.com/

Page 12: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

People told you that ->

Abstract factory

Builder

Factory Method

Builder

Prototype

Adapter

Bridge

Composite

Decorator

Proxy

Chain of responsibility

Command

Iterator

Mediator

Observer

Object Oriented FunctionalProgramming

http://www.clker.com/

Page 13: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

People told you that ->

Abstract factory

Builder

Factory Method

Builder

Prototype

Adapter

Bridge

Composite

Decorator

Proxy

Chain of responsibility

Command

Iterator

Mediator

Observer

Object Oriented FunctionalProgramming

http://www.clker.com/

Incorrect!

Page 14: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Builder

Page 15: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Builder

LET’S CODE

Page 16: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Behavior pattern that helps create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object

Strategy

Page 17: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

LET’S CODE

Behavior pattern that helps create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object

Strategy

Page 18: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects.Each processing object contains logic that defines the types of command objects that it can handle.Only qualifying predicate for the request will process it, otherwise it will pass to the next handler in chain

Chain ofResponsibility

Page 19: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

LET’S CODE

The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects.Each processing object contains logic that defines the types of command objects that it can handle.Only qualifying predicate for the request will process it, otherwise it will pass to the next handler in chain

Chain ofResponsibility

Page 20: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service)

DependencyInjection

Page 21: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service)

DependencyInjection

Dependency Injection framework!=

Dependency Injection

Page 22: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

Page 23: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

Constructor Injection

Vanilla

Page 24: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

CakePattern

Constructor Injection

Vanilla

Page 25: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

CakePattern

Constructor Injection

Vanilla

LET’S CODE

Page 26: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

• https://github.com/kfiron/design-pattern-fp-talk

My code is available for you

Page 27: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

• Went over functional programming definition

• Understood that FP as “side effect” help you go towards hexagonal architecture

• Refactor various patterns from Java to Scala Idioms

• Dependency injection using Scala with cake pattern

Summary of what we did

Page 28: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev@kfirondev

Page 29: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

linkedin/in/blochkfir github.com/kfiron@[email protected]

Kfir BlochHead of Backend Engineering

Q&A

Page 30: Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Thank You Wix Engineering Blog

http://engineering.wix.com/

We are hiring http://jobs.wix.com

[email protected]

@kfirondev

Kfir Bloch