Design patterns for functional-OO hybrid languages?

前端 未结 5 719
逝去的感伤
逝去的感伤 2021-01-29 19:33

Is there already any collection of best practices for languages like Scala?

I\'ve found a work on design patterns for functional languages, Design patterns for funct

相关标签:
5条回答
  • 2021-01-29 19:46

    In as much as any Object-Functional language is quickly going to acquire an actor library, a large number of actor-based patterns probably qualify for this question. Almost any of the patterns in Bob Martin's Enterprise Integration Patterns is recastable in terms of actors, with patterns like Load Balancer, Message Filter, Content-Based Router, and Content Enricher being especially common in systems architected around coarse-grained actors.

    0 讨论(0)
  • 2021-01-29 19:50

    While not directly a design pattern catalog itself, the paper "Scalable Component Abstractions" (Martin Odersky; Matthias Zenger) examines three building blocks for reusable components:

    • abstract type members,
    • explicit selftypes, and
    • modular mixin composition.

    And it revisits several design pattern (publish/subscribe, subject/observer, Context/Component) to illustrate and understand what language constructs are essential to achieve systems of scalable and dynamic components.

    0 讨论(0)
  • 2021-01-29 19:55

    One frequently observed pattern, which badly needs a name, is creating control abstractions with curried parameter lists and by-name parameters.

    def command(expr: T)(block: => Unit) {...}
    

    yielding

    command (expr) {
      block
    }
    
    0 讨论(0)
  • 2021-01-29 19:57

    Two patterns from Bill Venners; I think both are heavily used in ScalaTest:

    Stackable Trait (similar in structure to the decorator pattern, except it involves decoration for the purpose of class composition instead of object composition).

    Selfless Trait (allows library designers to provide services that their clients can access either through mixins or imports).

    Type safe builder

    Independently Extensible Solutions to the Expression Problem - just like the "Scalable Component Abstraction", it's not a pattern catalog, but it also deals with similar problems (e.g. the Visitor pattern)

    Deprecating the Observer Pattern - an alternative to the Observer.

    We can also consider the Scala emulation of Haskell type classes a design pattern. The first description (that I could find at least) is in Poor Man's Type Classes. Quite some blog entries are also available with this topic.

    And I think I'm not completely wrong if I also mention the various monads. You can find a lot of resources dealing with them.

    0 讨论(0)
  • 2021-01-29 20:02

    Closely related, you might want to explore data structures as defined in purely functional (or hybrid functional) languages. For one, the ability to treat functions as first-class values make some patterns (like visitor, template method or decorator) unnecessary in some (not all) contexts. Secondly, data structures (and the algorithms that operate on them) are either the plumbing for design patterns, or present certain problems that design patterns attempt to address, see Wikipedia article Purely functional.

    Better yet, I'd refer you to Okasaki's thesis on purely functional data structures.

    0 讨论(0)
提交回复
热议问题