refactoring design patterns the functional way (in scala)
TRANSCRIPT
![Page 1: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/1.jpg)
@kfirondev
Refactoring Design Patterns the Functional Way (in Scala)
@kfirondev
Kfir BlochBackend engineering manager
![Page 2: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/2.jpg)
@kfirondev
What the hell is functional programming?
![Page 3: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/3.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/4.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/5.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/6.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/7.jpg)
@kfirondev
def doSomething(msg: String): String = { val formatted = msg.toUpperCase formatted }
Pure function
![Page 8: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/8.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/9.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/10.jpg)
@kfirondev
People told you that ->
![Page 11: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/11.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/12.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/13.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/14.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/15.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/16.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/17.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/18.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/19.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/20.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/21.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/22.jpg)
@kfirondev
Dependency injection
SpringEJB Guice
Dependency Injection Frameworks
![Page 23: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/23.jpg)
@kfirondev
Dependency injection
SpringEJB Guice
Dependency Injection Frameworks
Constructor Injection
Vanilla
![Page 24: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/24.jpg)
@kfirondev
Dependency injection
SpringEJB Guice
Dependency Injection Frameworks
CakePattern
Constructor Injection
Vanilla
![Page 25: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/25.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/26.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/27.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/28.jpg)
@kfirondev@kfirondev
![Page 29: Refactoring Design Patterns the Functional Way (in Scala)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/29.jpg)
@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)](https://reader036.vdocuments.us/reader036/viewer/2022081503/5870813a1a28ab57368b668d/html5/thumbnails/30.jpg)
@kfirondev
Thank You Wix Engineering Blog
http://engineering.wix.com/
We are hiring http://jobs.wix.com
@kfirondev
Kfir Bloch