Functional equivalent of decorator pattern?

后端 未结 10 2128
悲&欢浪女
悲&欢浪女 2021-02-02 06:34

What is the functional programming equivalent of the decorator design pattern?

For example, how would you write this particular example in a functional style?

10条回答
  •  逝去的感伤
    2021-02-02 07:12

    Ok, first of all lets try to find all the main components of decorator pattern in respect to OOP. This pattern is basically used to decorate i.e add additional features to an existing object. This is the simplest possible definition of this pattern. Now if we try to find the same components that are there in this definition in the world of FP, we can say that additional features = new functions and object are not there in FP, rather FP has what you call data or data structure in various forms. So in FP terms this patterns becomes, adding additional functions for FP data structures or enhancing existing function with some additional features.

提交回复
热议问题