Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design pattern-refactor-functional

321 views

Published on

Refactoring design patterns the functional way (In Scala)

Published in: Engineering
  • Be the first to comment

Design pattern-refactor-functional

  1. 1. @kfirondev Refactoring Design Patterns the Functional Way (in Scala) @kfirondev Kfir Bloch Backend engineering manager
  2. 2. @kfirondev What the hell is functional programming?
  3. 3. @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/
  4. 4. @kfirondev case class Salad(name: String, veggy: Boolean, chopped: Boolean) Immutability via Scala case class
  5. 5. @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
  6. 6. @kfirondev def doSomething(msg: String): Unit= { val formatted = msg.toUpperCase logger.log(Level.WARNING, formatted) } Function with side effects
  7. 7. @kfirondev def doSomething(msg: String): String = { val formatted = msg.toUpperCase formatted } Pure function
  8. 8. @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/
  9. 9. @kfirondev Hexagonal Architecture with FP gives result which is: Deterministic Separation of concerns Fast testing lifecycle Concurrent agnostic domain model
  10. 10. @kfirondev People told you that ->
  11. 11. @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/
  12. 12. @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/
  13. 13. @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/
  14. 14. @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
  15. 15. @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
  16. 16. @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
  17. 17. @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
  18. 18. @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
  19. 19. @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
  20. 20. @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
  21. 21. @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
  22. 22. @kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks
  23. 23. @kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Constructor Injection Vanilla
  24. 24. @kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Cake Pattern Constructor Injection Vanilla
  25. 25. @kfirondev Dependency injection SpringEJB Guice Dependency Injection Frameworks Cake Pattern Constructor Injection Vanilla LET’S CODE
  26. 26. @kfirondev • https://github.com/kfiron/design-pattern- fp-talk My code is available for you
  27. 27. @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
  28. 28. @kfirondev@kfirondev
  29. 29. @kfirondev linkedin/in/blochkfir github.com/kfiron@kfirondevkfirb@wix.com Kfir Bloch Head of Backend Engineering Q&A
  30. 30. @kfirondev Thank YouWix Engineering Blog http://engineering.wix.com/ We are hiring http://jobs.wix.com email jobs@wix.com @kfirondev Kfir Bloch

×