Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns in Scala

5138e9107efc123e40339b73a67dbfdc?s=47 Pavel Fatin
October 19, 2013

Design Patterns in Scala

5138e9107efc123e40339b73a67dbfdc?s=128

Pavel Fatin

October 19, 2013
Tweet

Transcript

  1. None
  2. Design pattern is: • Solution. • Template. • Best practice.

    • Obect-oriented. • Language extension. • Means of communication.
  3. Creational patterns: Factory method Lazy initialization Singleton Structural patterns: Adapter

    Decorator Behavioral patterns: Value object Null Object Strategy Command Chain of responsibility Dependency injection
  4. Factory method «Salty Oat Cookies» by Kimberly Vardeman from http://www.flickr.com/photos/kimberlykv/5061421858/,

    available under a Creative Commons Attribution 2.0 Generic license: http://creativecommons.org/licenses/by/2.0/
  5. Factory method • Provides an interface for creating an object.

    • Encapsulates class instantiation in a method. • Lets subclasses decide which class to instantiate. Allows to: • extract complex object creation code, • select which class to instantiate, • cache objects.
  6. Factory method

  7. Factory method

  8. Lazy initialization Photo by Viola Heusser from http://pixabay.com/en/hangover-cat-domestic-cat-black-173669/, available under

    a Creative Commons CC0 1.0 Universal license: http://creativecommons.org/publicdomain/zero/1.0/
  9. Lazy initialization • A special case of lazy evaluation strategy.

    • Initializes a value/object on its first access. Allows to: • defer expensive computation.
  10. Lazy initialization

  11. Lazy initialization

  12. Singleton «The Solitary Tree» by Bobbi Jones Jones from http://www.publicdomainpictures.net/view-image.php?image=22282,

    available under a Creative Commons CC0 1.0 Universal license: http://creativecommons.org/publicdomain/zero/1.0/
  13. Singleton • Restricts the instantiation to one object. • Provides

    a global point of access to it. Allows to: • limit number of class objects, • implement interfaces, • use lazy initialization.
  14. Singleton

  15. Singleton

  16. Adapter «Multiple jacks / adaptors» by Mihai Andoni from http://www.sxc.hu/photo/972429

  17. Adapter • Converts an interface into expected interface. Allows to:

    • integrate incompatible classes, • adapt existing components.
  18. Adapter

  19. Adapter

  20. Decorator «Colour Pencils» by George Hodan from http://www.publicdomainpictures.net/view-image.php?image=25646, available under

    a Creative Commons CC0 1.0 Universal license: http://creativecommons.org/publicdomain/zero/1.0/
  21. Decorator • Extends functionality of a particular object. • Provides

    a flexible alternative to subclassing. Allows to: • decorate final classes, • arbitrarily combine extensions.
  22. Decorator

  23. Decorator

  24. Value object «Dollars» by Виталий Смолыгин from http://publicdomainpictures.net/view-image.php?image=31593, available under

    a Creative Commons CC0 1.0 Universal license: http://creativecommons.org/publicdomain/zero/1.0/
  25. Value object • A small immutable object. • Equality isn’t

    based on identity. Allows to: • represent numbers, dates, colors, etc, • implement inter-process communication, • simplify multi-threaded programing.
  26. Value object

  27. Value object Tuple: Tuple with a type alias: Case class:

  28. Null Object «Mr. Invisible at the beach» by Carlos Koblischek

    from http://www.sxc.hu/photo/348295
  29. Null Object • Represents the absence of an object. •

    Implements «do-nothing» behavior. Allows to: • avoid explicit checks of reference validity.
  30. Null Object

  31. Null Object

  32. Strategy Photo by Nachrichten_muc from http://pixabay.com/en/chess-chess-board-black-play-white-140340/, available under Creative Commons

    CC0 1.0 Universal license http://creativecommons.org/publicdomain/zero/1.0/
  33. Strategy • Defines a family of encapsulated algorithms. • Vary

    algorithms independently from clients. Allows to: • select an algorithm at runtime.
  34. Strategy

  35. Strategy

  36. Command «Dog Stick Water» by Randen Pederson from http://www.flickr.com/photos/chefranden/2668370301, available

    under a Creative Commons Attribution 2.0 Generic license: http://creativecommons.org/licenses/by/2.0/
  37. Command • Encapsulates information about a mehtod call. • Provides

    a way to call the method later. Allows to: • delay, sequence or log method calls.
  38. Command

  39. Command

  40. Chain of responsibility «Things in motion: Colored Domino» by Sigurd

    Decroos from http://www.sxc.hu/photo/1077403
  41. Chain of responsibility • Decouples the sender of a request

    from its receiver. • The request is processed by the chain until some object handles it. Allows to: • give more than one object a chance to handle the request.
  42. Chain of responsibility

  43. Chain of responsibility

  44. Dependency injection «Tool Box & Levels» by Dylan Foley from

    http://www.flickr.com/photos/shoesfullofdust/3999374824/, available under a Creative Commons Attribution 2.0 Generic license: http://creativecommons.org/licenses/by/2.0/
  45. Dependency injection • Removes hard-coded dependencies. • Provides a way

    to substitute dependencies. Allows to: • choose among multiple implementations of a particular component, • use mock implementations during unit testing.
  46. Dependency injection

  47. Dependency injection

  48. «Cat And Dog» by Anna Langova from http://www.publicdomainpictures.net/view-image.php?image=24076, available under

    a Creative Commons CC0 1.0 Universal license: http://creativecommons.org/publicdomain/zero/1.0/
  49. Thank you!