Decorator Pattern in Swift
For some reason(legacy code, library code, etc…) you can not modify an object’s code but you want to alter its behaviour and do not want to use inheritance, then this pattern might help you.
As an example, let’s have a class named DefaultButtonStyler to style our buttons:
Later, you needed to have a button with corner radius and you can not modify DefaultButtonStyler. Then, at this point you can create a decorator class to alter the existing styling functionality:
In initialization, CornerRadiusButtonStyler takes a decoratee object, wraps it and in style(_:) function, it alters its behaviour by adding a cornerRadius to the button.