Design Patterns Swift

Tags: Swift

Discover the modern implementation of design patterns with Swift

Last updated 2022-01-10 | 4.5

- Recognize and apply design patterns
- Refactor existing designs to use design patterns
- Reason about applicability and usability of design patterns

What you'll learn

Recognize and apply design patterns
Refactor existing designs to use design patterns
Reason about applicability and usability of design patterns
Implement each pattern in a coding exercise

* Requirements

* Good understanding of Swift
* Familiarity with latest Swift features
* Good understanding of object-oriented design principles

Description

Course Overview

This course provides a comprehensive overview of Design Patterns in Swift from a practical perspective. This course in particular covers patterns with the use of:

  • The latest versions of the Swift programming language
  • Use of modern programming approaches: dependency injection, reactive programming and more
  • Use of modern developer tools
  • Discussions of pattern variations and alternative approaches

This course provides an overview of all the Gang of Four (GoF) design patterns as outlined in their seminal book, together with modern-day variations, adjustments, discussions of intrinsic use of patterns in the language.

What are Design Patterns?

Design Patterns are reusable solutions to common programming problems. They were popularized with the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm (who are commonly known as a Gang of Four, hence the GoF acronym).

The original book was written using C++ and Smalltalk as examples, but since then, design patterns have been adapted to every programming language imaginable: Swift, C#, Java, PHP and even programming languages that aren't strictly object-oriented, such as JavaScript.

The appeal of design patterns is immortal: we see them in libraries, some of them are intrinsic in programming languages, and you probably use them on a daily basis even if you don't realize they are there.

What Patterns Does This Course Cover?

This course covers all the GoF design patterns. In fact, here's the full list of what is covered:

  • SOLID Design Principles: Single Responsibility Principle, Open-Closed Principle, Liskov Substitution Principle, Interface Segregation Principle and Dependency Inversion Principle
  • Creational Design Patterns: Builder, Factories (Factory Method and Abstract Factory), Prototype and Singleton
  • Structrural Design Patterns: Adapter, Bridge, Composite, Decorator, Façade, Flyweight and Proxy
  • Behavioral Design Patterns: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template Method and Visitor

Who Is the Course For?

This course is for Swift developers who want to see not just textbook examples of design patterns, but also the different variations and tricks that can be applied to implement design patterns in a modern way.

Presentation Style

This course is presented as a (very large) series of live demonstrations. All demos are single-file, so you can download the file attached to the lesson and run it in CLion, XCode or another IDE of your choice (or just on the command line).

This course does not use UML class diagrams; all of demos are live coding. I use Visual Studio Code for the demos.

Who this course is for:

  • Beginner and experienced developers
  • Anyone interested in design patterns

Course content

26 sections • 136 lectures

Introduction Preview 05:35

A taste of things to come...

Overview Preview 00:52

What are SOLID principles, where do they come from and why do we care?

Single Responsibility Principle Preview 07:33

A look at the Single Responsibility Principle, which states that a class should only have one reason to change. Also tied to the concept of Separation of Concerns which is basically stating the same thing.

Open-Closed Principle Preview 19:45

A discussion of the Open-Closed Principle, which states that classes should be open for extension, but closed for modification. In other words, you should extend functionality using interfaces and inheritance rather than jumping back into already-written/tested code and adding to it or changing it.

Liskov Substitution Principle Preview 07:22

The Liskov Substitution Principle states that subtypes should be substitutable for their base types.

Interface Segregation Principle Preview 08:41

The Interface Segregation Principle is simple: don't throw everything in the kitchen sink into a protocol because then all its users will have to implement things they do not need. Instead, split the protocol into several smaller ones.

Dependency Inversion Principle Preview 10:32

Not to be confused with dependency injection, dependency inversion specifies that high-level modules should not depend on low-level ones; both should depend on abstractions. Confusing, huh?

Summary Preview 02:26

A summary of the things we've learned in this section of the course.

Overview Preview 01:20

A discussion of the Builder pattern and what it's used for.

Life Without Builder Preview 03:20

A look at why you'd want to have a builder in the first place.

Builder Preview 09:10

We implement a simple builder for constructing trees of HTML elements.

Fluent Builder Preview 02:29

We make the builder fluent by returning self from builder methods.

Faceted Builder Preview 08:28

We look at a more complicated builder facade that exposes several sub-builders (builder facets) for building up parts of an object in a fluent manner.

Builder Coding Exercise

Summary Preview 01:01

A summary of the things we've learned about the Builder pattern.

Overview Preview 02:21

A discussion of the general concept of factories and the two design patterns: Factory Methods and Abstract Factory.

Point Example Preview 03:18

A scenario where having a factory interface actually makes sense.

Factory Method Preview 03:54

Implementing a factory method, as an alternative to an initializer, is easy.

Factory Preview 02:48

When you want all the factory methods in a separate class.

Inner Factory Preview 04:13

An external factory needs the created object's constructor to be public. But what if you want it to be private? The solution is simple: stick a factory into the class whose instance it creates!

Abstract Factory Preview 11:27

Sometimes, you want abstract factories with abstract objects; we support DIP but break OCP in the process.

Factory Coding Exercise

Summary Preview 01:04

A summary of the things we've learned in this module.

Overview Preview 01:54

A discussion of the Prototype factory (not to be confused with a rather good game of the same name) and what it's used for.

Copy Constructors Preview 11:03

A copying approach straight from the land of C++.

Explicit Deep Copy Interface Preview 05:11

What do you think about the idea of simply defining a protocol for deep-copyable reference types? Sounds like a simple idea, but we're going to encounter a problem with casting the result of out own initializer to Self. Don't worry, there is a fix for this.

Prototype Coding Exercise

Summary Preview 00:52

A summary of all the things we've learned about the prototype pattern.

Overview Preview 02:33

Ahh, the much maligned Singleton? Is it really that evil? Let's find out...

Singleton Implementation Preview 08:50

Avoiding all the philosophical nonsense surrounding double-checked locking (it’s not thread-safe) and implementations involving inner static classes, we simply look at the safest possible singleton. But just because it's safe doesn't mean it's useful.

Testability Issues Preview 06:10

The singleton works fine, so what's the problem? Turns out, hard reference to a type means we cannot fake it in our tests. Oops!

Singleton in Dependency Injection Preview 05:57

The only socially acceptable way of using a singleton is with a DI framework. 
Typically, marking a component as a singleton is trivial.

Monostate Preview 04:52

A variation on a Singleton pattern, the Monostate lets the client instantiate as many copies of the singleton class as they want, but all those copies refer to the same static data. Is this a good idea? Let’s find out!

Singleton Coding Exercise

Summary Preview 01:04

A summary of all that we've learned about the Singleton. As you can see, it's not really that evil provided it can be substituted by a different type (polymorphism). Also, lifetime management is best left to a specialized system (i.e. a DI container).

Overview Preview 01:12

A look at the Adapter design pattern.

Vector/Raster Demo Preview 09:30

We are going to build a simple adapter for the rending of vector data where only a raster renderer is available. 

Adapter Caching Preview 07:48

An adapter can generate a large number of temporary objects. Caching helps us avoid doing extra work more than once.

Adapter Coding Exercise

Summary Preview 00:56

A summary of all the things we've learned about the Adapter pattern.

Overview Preview 01:46

A discussion of the Bridge pattern and what it's used for.

Bridge Preview 07:19

A simple illustration of how to build a bridge.

Bridge Coding Exercise

Summary Preview 00:21

A summary of all the important things we've learned in this section of the course.

Overview Preview 01:22

A discussion of what the Composite pattern is for and how it's used.

Geometric Shapes Preview 07:19

Let's implement the Composite pattern by considering individual geometric shapes as well as grouping of shapes.

Neural Networks Preview 09:58

Let's apply the Composite pattern to the implementation of simple neural networks (individual neurons and layers of neurons).

Composite Coding Exercise

Summary Preview 01:38

A summary of all the things we've learned about the Composite design pattern.

Overview Preview 01:21

A look at the Decorator design pattern.

Custom String Builder Preview 05:38

Building strings is a common concern. Let's see how we can have its de facto inheritor as a Decorator.

Multiple Inheritance Preview 03:20

When you implement pseudo-multiple inheritance using interfaces, you quite often end up implementing the Decorator pattern.

Dynamic Decorator Composition Preview 06:52

A look at how to make decorators-of-decorators.

Static Decorator Composition Preview 05:00

Can decorators be composed as nested generic type arguments? They can, but things aren't as rosy in Swift as they are in C++.

Decorator Coding Exercise

Summary Preview 01:13

A summary of all the things we've learned about the Decorator design pattern.

Overview Preview 01:47

A look at the Façade design pattern. Also an explanation of that weird letter Ç.

Façade Preview 06:30

An illustration of what a typical Facade looks like.

Façade Coding Exercise

Summary Preview 00:42

A summary of the things we've learned about the Facade design pattern.

Overview Preview 01:58

A discussion of the Flyweight design pattern and what it's used for.

Repeating User Names Preview 08:15

An example of string space optimization when storing users' names.

Text Formatting Preview 10:04

Text formatting is a classic example of a Fliweight. Instead of keeping formatting flags for every single character in a line of text, let's implement ranges!

Flyweight Coding Exercise

Summary Preview 00:36

A summary of all the things we've learned about the Flyweight design pattern.

Overview Preview 01:29

A look at the Proxy design pattern.

Protection Proxy Preview 03:34

Let's implement a proxy which adds access control to the object.

Property Proxy Preview 05:11

One very common scenario is where developers replace ordinary properties with Property<T>-typed objects. Let's build out own implementation and discuss what it's for.

Proxy vs. Decorator Preview 01:27

A comparison of the Proxy and Decorator design patterns.

Proxy Coding Exercise

Summary Preview 00:38

A summary of the things we've learned about the Proxy design pattern.

Overview Preview 02:16

A look at the Chain of Responsibility design pattern and discussion of what it's used for.

Command Query Separation Preview 01:28

Brief discussion of the concept of Command Query Separation (CQS).

Method Chain Preview 11:36

Chain of Responsibility implemented by chaining method calls together.

Broker Chain Preview 15:16

A more sophisticated approach to implementing the chain of responsibility.

Chain of Responsibility Coding Exercise

Summary Preview 00:45

A summary of the things we've learned about the Chain of Responsibility design pattern.

Overview Preview 01:49

A look at the Command design pattern.

Command Preview 07:51

Let's implement the Command design pattern in a simple scenario.

Undo Operations Preview 03:14

A simple demonstration of how to implement Undo functionality while using Command.

Command Coding Exercise

Summary Preview 00:38

A summary of all the things we've learned about the Command design pattern.

Overview Preview 02:24

An overview of the Interpreter design pattern, which actually brings with it a whole field of Computer Science typically called Compiler Theory.

Handmade Interpreter: Lexing Preview 09:40

Lexing is the process of splitting textual input into lexical tokens.

Handmade Interpreter: Parsing Preview 10:03

Parsing is the process of converting a series of tokens into an Abstract Syntax Tree (AST).

Interpreter Coding Exercise

Summary Preview 00:31

A summary of the things we've learned about the Interpreter design pattern.

Overview Preview 01:12

A look at the Iterator design pattern.

Iterator Object Preview 13:12

A look at how to build a handmade iterator.

Array-Backed Properties Preview 07:08

What happens if you want to iterate the value of every single property (or a specific range of properties) in a particular class? Well, this is rather easy: just make a single array-typed backing field for every single property!

Iterator Coding Exercise

Summary Preview 00:44

A summary of all the things we've learned about iterators in Swift.

Overview Preview 01:13

A look at the Mediator design pattern.

Chat Room Preview 10:20

A classic example of the Mediator pattern is a chat room. Well, let's build one!

Mediator Coding Exercise

Summary Preview 00:58

A summary of the things we've learned about the Mediator design pattern.

Overview Preview 01:17

A look at the Memento design pattern.

Memento Preview 06:18

Let's turn back to our classic BankAccount example and implement the Memento pattern.

Undo and Redo Preview 06:50

If we keep every single change as a Memento stored internally, we can easily implement Undo/Redo operations.

Memento Coding Exercise

Summary Preview 00:33

A summary of the things we've learned about the Memento design pattern.

Overview Preview 01:24

A discussion on what the Null Object is useful for. Note this is not a GoF pattern.

Null Object Preview 06:44

A demonstration of a very simple implementation of a Null Object.

Null Object Coding Exercise

Summary Preview 01:02

A summary of all the things we've learned about the Null Object design pattern.

Overview Preview 01:09

A look at the Observer design pattern.

Events Preview 14:40

Unlike other languages (e.g., C#), Swift doesn't have a built-in 'event' type. Lucky for us, it's really easy to make one.

Property Observers Preview 03:43

Swift supports property observers. Let's learn how to use them.

Handling Dependent Observable Properties Preview 16:01

If you have get-only properties dependent upon other properties, you don't have a way of generating willSet/didSet calls. How can we deal with this? By rolling our own .NET-style property change notification mechanism!

Observer Coding Exercise

Summary Preview 00:47

A summary of all the things we've learned about the Observer design pattern.

Overview Preview 01:27

A look at the State design pattern.

Handmade State Machine Preview 07:00

Let's hand-roll a simple finite state machine.

State Coding Exercise

Summary Preview 00:54

A summary of the things we've learned about the State design pattern.

Overview Preview 01:08

A look at the strategy design pattern.

Dynamic Strategy Preview 10:11

An implementation of dynamic strategy pattern which lets us change strategies at runtime.

Static Strategy Preview 03:17

A static implementation of the Strategy pattern forces us to choose the strategy to use at compile time.

Strategy Coding Exercise

Summary Preview 00:29

A summary of the things we've learned about the Strategy design pattern.

Overview Preview 01:35

A look at the Template Method design pattern.

Template Method Preview 07:33

Let's write a template method for a typical board game.

Template Method Coding Exercise

Summary Preview 00:22

A summary of the things we've learned about the Template Method design pattern.

Overview Preview 02:46

A look at the Visitor design pattern.

Intrusive Expression Printing Preview 06:29

Let's break OCP to support expression printing.

What is Dispatch? Preview 02:17

A discussion of the concept of Dispatch, which you need to understand before building visitors.

Reflection-Based Printing Preview 05:53

Another approach to printing expressions, this time by using reflection and checking against types. Not very efficient!

Classic Visitor (Double Dispatch) Preview 09:24

Finally, we implement the classic Visitor pattern using double dispatch.

Visitor Coding Exercise

Summary Preview 01:01

A summary of the things we've learned about the Visitor design pattern.

Creational Patterns Summary Preview 04:14

A summary of all the Creational patterns that we've met in this course: Builder, Factory (Factory Method, Abstract Factory), Singleton and Prototype.

Structural Patterns Summary Preview 03:06

A summary of all the Structural design patterns we've met in this course: Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy.

Behavioral Patterns Summary Preview 06:11

A summary of all the Behavioral design patterns we've met in this course: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template Method and Visitor.

End of Course Preview 01:08

A few last words before we end the course... I'm so sad it's over! Ah well, there's always cake.

Bonus Lecture: Other Courses at a Discount Preview 00:03

Did you enjoy this course? I have other courses here on Udemy. Check them out!