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

Design Patterns in Scala

Pavel Fatin
October 19, 2013

Design Patterns in Scala

Pavel Fatin

October 19, 2013
Tweet

More Decks by Pavel Fatin

Other Decks in Programming

Transcript

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

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

    Decorator Behavioral patterns: Value object Null Object Strategy Command Chain of responsibility Dependency injection
  3. 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/
  4. 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.
  5. 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/
  6. Lazy initialization • A special case of lazy evaluation strategy.

    • Initializes a value/object on its first access. Allows to: • defer expensive computation.
  7. 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/
  8. 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.
  9. Adapter • Converts an interface into expected interface. Allows to:

    • integrate incompatible classes, • adapt existing components.
  10. 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/
  11. Decorator • Extends functionality of a particular object. • Provides

    a flexible alternative to subclassing. Allows to: • decorate final classes, • arbitrarily combine extensions.
  12. 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/
  13. 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.
  14. Null Object • Represents the absence of an object. •

    Implements «do-nothing» behavior. Allows to: • avoid explicit checks of reference validity.
  15. Strategy • Defines a family of encapsulated algorithms. • Vary

    algorithms independently from clients. Allows to: • select an algorithm at runtime.
  16. 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/
  17. Command • Encapsulates information about a mehtod call. • Provides

    a way to call the method later. Allows to: • delay, sequence or log method calls.
  18. Chain of responsibility «Things in motion: Colored Domino» by Sigurd

    Decroos from http://www.sxc.hu/photo/1077403
  19. 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.
  20. 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/
  21. 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.
  22. «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/