@kfirondev Refactoring Design Patterns the Functional Way (in Scala) @kfirondev Kfir Bloch Backend engineering manager
@kfirondev What the hell is functional programming?
@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/
@kfirondev case class Salad(name: String, veggy: Boolean, chopped: Boolean) Immutability via Scala case class
@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
@kfirondev def doSomething(msg: String): Unit= { val formatted = msg.toUpperCase logger.log(Level.WARNING, formatted) } Function with side effects
@kfirondev def doSomething(msg: String): String = { val formatted = msg.toUpperCase formatted } Pure function
@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/
@kfirondev Hexagonal Architecture with FP gives result which is: Deterministic Separation of concerns Fast testing lifecycle Concurrent agnostic domain model
@kfirondev People told you that ->
@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/
@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 Functional Programming http://www.clker.com/
@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 Functional Programming http://www.clker.com/
@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
@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
@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
@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
@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 of Responsibility
@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 of Responsibility
@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) Dependency Injection
@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) Dependency Injection Dependency Injection framework != Dependency Injection
@kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks
@kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Constructor Injection Vanilla
@kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Cake Pattern Constructor Injection Vanilla
@kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Cake Pattern Constructor Injection Vanilla LET’S CODE
@kfirondev • https://github.com/kfiron/design-pattern- fp-talk My code is available for you
@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
@kfirondev@kfirondev
@kfirondev linkedin/in/blochkfir github.com/kfiron@kfirondevkfirb@wix.com Kfir Bloch Head of Backend Engineering Q&A
@kfirondev Thank YouWix Engineering Blog http://engineering.wix.com/ We are hiring http://jobs.wix.com email jobs@wix.com @kfirondev Kfir Bloch

Refactoring Design Patterns the Functional Way (in Scala)

Editor's Notes

  • #3 The first case is degradation of service