Functional Decorator

Recently I’ve been experimenting with functional programming and Scala. As a developer used to Object Oriented paradigm I was wondering what are the functional equivalents of popular design patterns. One of the best articles summarizing this subject is Mark Seeman’s “Patterns Across Paradigms”. I am currently working on a small project where I had a chance to implement the Decorator Pattern using functional constructs in Scala.

The Object-Oriented approach

My example is be based on “DDD CQRS Leaven”, a project presenting some system and domain modeling concepts. This application was originally created by Sławek Sobótka and Rafał Jamróz, you can browse the codebase on Github. Here we are going to focus only on a small part of the domain, the  Rebate Policy. It’s a simple representation of Strategy Pattern, responsible for calculating eventual rebates for products in an online store. The model of policies can be described in few boxes:

Dead simple so far, right? We have our RebatePolicy contract with a couple of implementations. Now let’s see how it looks like when we add a Decorator:

Here’s the implementation of these components:

public abstract class RebateDecorator implements RebatePolicy {
protected RebatePolicy decorated;
protected RebateDecorator(RebatePolicy decorated){
this.decorated = decorated;
view raw hosted with ❤ by GitHub
public interface RebatePolicy {
Money calculateRebate(Product product, int quantity, Money regularCost);
view raw hosted with ❤ by GitHub
public class StandardRebate implements RebatePolicy {
private BigDecimal rebateRatio;
private int mininalQuantity;
public StandardRebate(double rebate, int mininalQuantity) {
rebateRatio = new BigDecimal(rebate / 100);
this.mininalQuantity = mininalQuantity;
public Money calculateRebate(Product product, int quantity,
Money regularCost) {
if (quantity >= mininalQuantity)
return regularCost.multiplyBy(rebateRatio);
return Money.ZERO;
view raw hosted with ❤ by GitHub
public class VipRebate extends RebateDecorator{
private Money minimalThreshold;
private Money rebateValue;
public VipRebate(Money minimalThreshold, Money rebateValue) {
this(null, minimalThreshold, rebateValue);
public VipRebate(RebatePolicy decorated, Money minimalThreshold,
Money rebateValue) {
if (rebateValue.greaterThan(minimalThreshold))
throw new IllegalArgumentException(
"Rabate can't be graterThan minimal threshold");
this.minimalThreshold = minimalThreshold;
this.rebateValue = rebateValue;
public Money calculateRebate(Product product, int quantity,
Money regularCost) {
Money baseValue = (decorated == null)
? regularCost
: decorated.calculateRebate(product, quantity, regularCost);
if (baseValue.greaterThan(minimalThreshold))
return baseValue.subtract(rebateValue);
return baseValue;
view raw hosted with ❤ by GitHub

Such design allows combining different Domain Policies (Strategies) in a flexible way to obtain object which still matches the RebatePolicy interface and represents the composition. Decorator pattern allows adding new policies and creating various combinations in runtime without modifying existing ones or the “client code”, which keeps using the original abstraction. Neat.

Functional implementation

Trying to achieve similar goals using functional code requires reminding that the GoF Decorator Pattern is, in fact, a supplementary construct required to compensate the shortcomings of typical OO languages. In functional world we can leverage currying and functions as first-class citizens to get same desired effect. Before we explore the functional implementation in Scala, take a look at the RebateDecorator class. It represents an abstract base for all rebates which can wrap other rebates. A RebateDecorator forces our rebate to pass some inner rebate object in the constructor and provides it as a protected member for further use by the inheriting class. Then, the VipRebate class allows creating an instance in two ways: either with some decorated member or without it. Let’s do something similar with functions in Scala:

object Rebates {
type RebatePolicy = (Product, Int, Money) => Money
object standardRebate extends ((Double, Int) => RebatePolicy) {
def apply(rebate: Double, minimalQuantity: Int) = {
(product, quantity, regularCost) => {
val rebateRatio = BigDecimal(rebate / 100)
if (quantity >= minimalQuantity)
regularCost * rebateRatio
object vipRebate extends
((Money, Money) => Option[RebatePolicy] => RebatePolicy) {
override def apply(minimalThreshold: Money, rebateValue: Money) =
(innerPolicy) => {
(product, quantity, regularCost) => {
val baseValue =, quantity, regularCost)).
if (baseValue > minimalThreshold)
(baseValue - rebateValue)
else baseValue
// somewhere in a rebate factory:
def createPolicy: RebatePolicy = {
val vipPolicy = vipRebate(Money(1000), Money(100))
val standard = standardRebate(10, 1)
if (isVip)

As you can see, the RebatePolicy type is now a functional type, which means that we speak more directly of our contract: A RebatePolicy is a function which takes a Product, quantity and minimumPrice and returns rebate value of type Money. Standard policy produces a function fulfilling this contract by calculating the rebate with some simple algorithm. What about VipRebate? It’s also a function, but a bit more complex 🙂 In fact, the VipRebate represents kind of a Factory (yes! Another pattern that we get for free!) which allows creating new function of type RebatePolicy with additional parameters: two Money values and innerPolicy. The Option type in Scala gives a way to initialize the rebate with “none” inner policy which is much more elegant than null manipulation that we saw before. Our goal has been achieved with some additional bonuses:

  1. Much more concise. The baroque entourage of Java has been strongly reduced to what’s essential. No more unnecessary ceremony, while the readability and comprehensibility are still high (or even higher). The superfluous RebateDecorator class is no longer needed and it doesn’t “pollute” our real domain logic anymore.
  2. Flexibility. Thanks to currying we have free “functional dependency injection” capabilities. The VipRebate signature allows creating final policy in three steps: first with initial parameters (minimalThreshold, rebateValue). Such call would produce another function which we can pass around and eventually call with optional inner policy argument. This second call will finally produce a RebatePolicy ready to use whenever it is needed. In previous, objective approach we were forced to build our object with all the dependencies right away (with constructor). To achieve more flexibility we would require some setters which breaks immutability and stinks 😉


Exploring the world of functional programming is addictive and changes your mindset forever. If you are interested in further learning then you should definitely check the free “Functional programming principles” course on Coursera. First edition just ended but next one will launch probably around spring and you will be more than satisfied 🙂

4 thoughts on “Functional Decorator

    1. kciesielski Post author

      Hmm I can’t quite fit this concept here… Could you elaborate a bit more on how to apply Type Classes in this case? A short piece of pseudo-code would be perfect 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s