From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Enhancing a type without modifying it

Enhancing a type without modifying it - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Enhancing a type without modifying it

- [Instructor] The decorator design pattern allows adding new responsibilities to objects dynamically without modifying the code of the underlying types. The decorator is a flexible alternative to subclassing. It can be implemented by wrapping the object to be enhanced. A decorator has the same interface as the wrapped type and it can wrap further decorators using recursive composition to add new responsibilities. Each decorator delegates to the wrapped decorator instance and the base decorate class delegates to the wrapped object. Another way to extent an existing type is by using swift type extensions. Type extensions don't require modifying the original type. Besides, we don't have to introduce new wrappers either. Instead, we can enhance the given type by adding the needed methods and calculated properties to that extension. I'm going to show you how to implement the decorator via both object wrapping and swift extensions. One of the common pitfalls of the decorator is adding…

Contents