Book Image

Swift 2 Design Patterns

By : Julien Lange
Book Image

Swift 2 Design Patterns

By: Julien Lange

Overview of this book

Table of Contents (15 chapters)
Swift 2 Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Static and dynamic composition


We can have several possible implementations. The classic way to design this is to differentiate these implementations in subclasses. In this case, we will provide an interface from where our classes will implement this interface.

This solution consists of a static composition. Indeed, once the implementation class of an object is chosen, we can no longer change it. The following diagram is the implementation of an object by heritage:

Another way is to separate the implementation in another object. The implementation parts are managed by an instance of the ConcreteImplementationA class or by the ConcreteImplementationB class. This reference is referred by the implementation attribute. This instance can then be easily substituted by another instance at runtime. This composition is dynamic.

The following UML class diagram shows us clearly how to structure your objects using a dynamic composition. The ConcreteImplementation class can be switched at runtime, without...