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.

The object Decorator

The object Decorator - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

The object Decorator

- [Instructor] Decorators attach additional responsibilities to objects. The decorator is correctly implemented if we don't have to modify the types that are used to create them. Also, there is no need to refactor existing code that uses these objects. In other words, by decorating an object, you won't affect the other object of the same type. We're going to implement two different flavors of the decorator pattern. Let's start with the object decorator. We're going to decorate the user default style. User default doesn't have methods to store and retrieve date instances. So, we'll add the set date for key, and date for key methods. The decorator has to implement the same interface as the tag it is going to decorate. This lets us replace the original object with the decorated one, and the other way around. So, I declare a new class that inherits from user defaults and I'm going to call it user defaults decorator. It inherits from user defaults. Next, I add a private property of type…

Contents