Design pattern-refactor-functional

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

Transcript of Design pattern-refactor-functional

Page 1: Design pattern-refactor-functional

@kfirondev

Refactoring Design Patterns the Functional Way (in Scala)

@kfirondev

Kfir BlochBackend engineering manager

Page 2: Design pattern-refactor-functional

@kfirondev

What the hell is functional programming?

Page 3: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@kfirondev

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

Immutability via Scala case class

Page 5: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@kfirondev

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

Function with side effects

Page 7: Design pattern-refactor-functional

@kfirondev

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

Pure function

Page 8: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@kfirondev

Hexagonal Architecture with FP gives result which is:

Deterministic Separation of concerns Fast testing lifecycle Concurrent agnostic domain model

Page 10: Design pattern-refactor-functional

@kfirondev

People told you that ->

Page 11: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

Page 23: Design pattern-refactor-functional

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

Constructor Injection

Vanilla

Page 24: Design pattern-refactor-functional

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

CakePattern

Constructor Injection

Vanilla

Page 25: Design pattern-refactor-functional

@kfirondev

Dependency injection

SpringEJB Guice

Dependency Injection Frameworks

CakePattern

Constructor Injection

Vanilla

LET’S CODE

Page 26: Design pattern-refactor-functional

@kfirondev

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

My code is available for you

Page 27: Design pattern-refactor-functional

@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: Design pattern-refactor-functional

@kfirondev@kfirondev

Page 29: Design pattern-refactor-functional

@kfirondev

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

Kfir BlochHead of Backend Engineering

Q&A

Page 30: Design pattern-refactor-functional

@kfirondev

Thank You Wix Engineering Blog

http://engineering.wix.com/

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

[email protected]

@kfirondev

Kfir Bloch