Pro Design Patterns in Swift

Book description

The Swift programming language has transformed the world of iOS development and started a new age of modern Cocoa development. Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development projects to master level.

This book will teach you those design patterns that have always been present at some level in your code, but may not have been recognized, acknowledged, or fully utilized. Implementation of specific pattern approaches will prove their value to any Swift developer.

Best-selling author Adam Freeman explains how to get the most from design patterns. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need.

Pro Design Patterns in Swift brings design patterns to life and shows you how to bring structure and scale to your Swift code with a practical, no-nonsense approach.

In this book, you’ll learn:

  • Gain a solid, practical understanding of why and when design patterns are useful
  • Master classic patterns like singleton, abstract factory, chain of responsibility, and observer
  • Discover less well-known patterns like memento, composite, command, and mediator
  • Understand how Swift provides essential language features for writing well-structured code that is easy to understand, simple to test and readily maintainable.
  • Learn how to consume the Cocoa API to implement classic design patterns
  • Build on your existing programming knowledge to get up and running with design patterns in Swift quickly and effectively
  • Table of contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewer
    9. Part I: Getting Ready
      1. Chapter 1: Understanding Design Patterns
        1. Putting Design Patterns into Context
          1. Introducing Design Patterns
          2. Understanding the Structure of a Design Pattern
          3. Quantifying the Value of Design Patterns
          4. Using a Design Pattern After the Problem Occurred
          5. Understanding the Limitations of Design Patterns
        2. About This Book
          1. What Do You Need to Know?
          2. What Software Do You Need?
          3. What Is the Structure of This Book?
          4. Where Can You Get the Example Code?
        3. Summary
      2. Chapter 2: Getting Used to Xcode
        1. Working with Xcode Playgrounds
          1. Creating a Playground
          2. Displaying the Value History of a Variable
          3. Using the Value Timeline
          4. Displaying UI Components in a Playground
        2. Working with OS X Command Line Tool Projects
          1. Creating a Command-Line Project
          2. Understanding the Xcode Layout
          3. Adding a New Swift File
        3. Summary
      3. Chapter 3: Creating the SportsStore App
        1. Creating an Unstructured iOS App Project
          1. Creating the Project
          2. Understanding the Xcode Layout
          3. Defining the Data
        2. Creating the Basic Layout
          1. Adding the Basic Components
          2. Configuring Auto Layout
          3. Testing the Basic Layout
        3. Implementing the Total Label
          1. Creating the Reference
          2. Updating the Display
        4. Implementing the Table Cells
          1. Defining the Custom Table Cell and Layout
          2. Setting the Table Cell Layout Constraints
          3. Creating the Table Cell Class and Outlets
          4. Implementing the Data Source Protocol
          5. Registering the Data Source
          6. Testing the Data Source
        5. Handling the Editing Actions
          1. Handling the Events
        6. Testing the SportsStore App
        7. Summary
    10. Part II: The Creation Patterns
      1. Chapter 4: The Object Template Pattern
        1. Preparing the Example Project
        2. Understanding the Problem Addressed by the Pattern
        3. Understanding the Object Template Pattern
        4. Implementing the Object Template Pattern
        5. Understanding the Benefits of the Pattern
          1. The Benefit of Decoupling
          2. The Benefit of Encapsulation
          3. The Benefit of an Evolving Public Presentation
        6. Understanding the Pitfalls of the Pattern
        7. Examples of the Object Template Pattern in Cocoa
        8. Applying the Pattern to the SportsStore App
          1. Preparing the Example Application
          2. Creating the Product Class
          3. Applying the Product Class
          4. Expanding the Summary Display
        9. Summary
      2. Chapter 5: The Prototype Pattern
        1. Understanding the Problem Addressed by the Pattern
          1. Incurring Expensive Initializations
          2. Creating Template Dependencies
        2. Understanding the Prototype Pattern
        3. Implementing the Prototype Pattern
          1. Cloning Reference Types
          2. Understanding Shallow and Deep Copying
          3. Copying Arrays
        4. Understanding the Benefits of the Prototype Pattern
          1. Avoiding Expensive Initializations
          2. Separating Object Creation from Object Use
        5. Understanding the Pitfalls of the Prototype Pattern
          1. Understanding the Deep vs. Shallow Pitfall
          2. Understanding the Exposure Distortion Pitfall
          3. Understanding the Nonstandard Protocol Pitfall
        6. Examples of the Prototype Pattern in Cocoa
          1. Using Cocoa Arrays
          2. Using the NSCopying Property Attribute
        7. Applying the Pattern to the SportsStore App
          1. Preparing the Example Application
          2. Implementing NSCopying in the Product Class
          3. Creating the Logger Class
          4. Logging Changes in the View Controller
          5. Testing the Changes
        8. Summary
      3. Chapter 6: The Singleton Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
          1. Understanding the Shared Resource Encapsulation Problem
        3. Understanding the Singleton Pattern
        4. Implementing the Singleton Pattern
          1. The Quick Singleton Implementation
          2. Creating a Conventional Singleton Implementation
          3. Dealing with Concurrency
        5. Understanding the Pitfalls of the Singleton Pattern
          1. Understanding the Leakage Pitfall
          2. Understanding the Shared Code File Pitfall
          3. Understanding the Concurrency Pitfalls
        6. Examples of the Singleton Pattern in Cocoa
        7. Applying the Pattern to the SportsStore Application
          1. Protecting the Data Array
          2. Protecting the Callback
          3. Defining the Singleton
        8. Summary
      4. Chapter 7: The Object Pool Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Object Pool Pattern
        4. Implementing the Object Pool Pattern
          1. Defining the Pool Class
          2. Consuming the Pool Class
        5. Understanding the Pitfalls of the Object Pool Pattern
        6. Examples of the Object Pool Pattern in Cocoa
        7. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Creating the (Fake) Server
          3. Creating the Object Pool
          4. Applying the Object Pool
        8. Summary
      5. Chapter 8: Object Pool Variations
        1. Preparing the Example Project
        2. Understanding the Object Pool Pattern Variations
          1. Understanding the Object Creation Strategy
          2. Understanding the Object Reuse Strategy
          3. Understanding the Empty Pool Strategy
          4. Understanding the Allocation Strategy
        3. Understanding the Pitfalls of the Pattern Variations
          1. Understanding the Expectation Gap Pitfall
          2. Understanding the Over- and Under-utilization Pitfalls
        4. Examples of the Pattern Variations in Cocoa
        5. Applying a Pattern Variation to SportsStore
        6. Summary
      6. Chapter 9: The Factory Method Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Factory Method Pattern
        4. Implementing the Factory Method Pattern
          1. Defining a Global Factory Method
          2. Using a Base Class
        5. Variations on the Factory Method Pattern
        6. Understanding the Pitfalls of the Pattern
        7. Examples of the Factory Method Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Implementing the Factory Method Pattern
          3. Consuming the Factory Method Pattern
        9. Summary
      7. Chapter 10: Abstract Factory Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Abstract Factory Pattern
        4. Implementing the Abstract Factory Pattern
          1. Creating the Abstract Factory
          2. Creating the Concrete Factories
          3. Completing the Abstract Factory
          4. Consuming the Abstract Factory Pattern
        5. Variations on the Abstract Factory Pattern
          1. Hiding the Abstract Factory Class
          2. Applying the Singleton Pattern to the Concrete Factories
          3. Applying the Prototype Pattern to the Implementation Classes
        6. Understanding the Pitfalls of the Pattern
        7. Examples of the Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Implementation Protocols and Classes
          3. Defining the Abstract and Concrete Factory Classes
          4. Consuming the Factories and Implementation Classes
        9. Summary
      8. Chapter 11: The Builder Pattern
        1. Preparing the Example Project
          1. Understanding the Problems That the Pattern Solves
        2. Understanding the Builder Pattern
        3. Implementing the Builder Pattern
          1. Defining the Builder Class
          2. Consuming the Builder
          3. Understanding the Impact of the Pattern
        4. Variations on the Builder Pattern
        5. Understanding the Pitfalls of the Builder Pattern
        6. Examples of the Builder Pattern in Cocoa
        7. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Builder Class
          3. Using the Builder Class
        8. Summary
    11. Part III: The Structural Patterns
      1. Chapter 12: The Adapter Pattern
        1. Preparing the Example Project
          1. Creating the Data Sources
          2. Defining the Application
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Adapter Pattern
        4. Implementing the Adapter Pattern
        5. Variations on the Adapter Pattern
          1. Defining an Adapter as a Wrapper Class
          2. Creating a Two-Way Adapter
        6. Understanding the Pitfalls of the Adapter Pattern
        7. Examples of the Adapter Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Adapter Class
          3. Using the Adapted Functionality
        9. Summary
      2. Chapter 13: The Bridge Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Bridge Pattern
        4. Implementing the Bridge Pattern
          1. Dealing with the Messages
          2. Dealing with the Channels
          3. Creating the Bridge
          4. Adding a New Message Type and Channel
        5. Variations on the Bridge Pattern
          1. Collapsing the Bridge
        6. Understanding the Pitfalls of the Bridge Pattern
        7. Examples of the Bridge Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Understanding the Problem
          3. Defining the Bridge Class
        9. Summary
      3. Chapter 14: The Decorator Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Decorator Pattern
        4. Implementing the Decorator Pattern
        5. Variations on the Decorator Pattern
          1. Creating Decorators with New Functionality
          2. Creating Consolidated Decorators
        6. Understanding the Pitfalls of the Decorator Pattern
          1. The Side-Effect Pitfall
        7. Examples of the Decorator Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Creating the Decorators
          3. Applying the Decorators
        9. Summary
      4. Chapter 15: The Composite Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Composite Pattern
        4. Implementing the Composite Pattern
          1. Applying the Pattern
        5. Understanding the Pitfalls of the Composite Pattern
        6. Examples of the Composite Pattern in Cocoa
        7. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Composite Class
          3. Applying the Pattern
        8. Summary
      5. Chapter 16: The Façade Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Façade Pattern
        4. Implementing the Façade Pattern
          1. Applying the Façade
        5. Variations on the Façade Pattern
        6. Understanding the Pitfalls of the Façade Pattern
        7. Examples of the Façade Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Creating the Façade Class
          3. Applying the Façade Class
        9. Summary
      6. Chapter 17: The Flyweight Pattern
        1. Preparing the Example Project
        2. Understanding the Problems That the Pattern Solves
        3. Understanding the Flyweight Pattern
        4. Implementing the Flyweight Pattern
          1. Creating the Flyweight Protocol
          2. Creating the Flyweight Implementation Class
          3. Adding Concurrency Protections
          4. Creating the Flyweight Factory Class
          5. Applying the Flyweight
        5. Variations on the Flyweight Pattern
        6. Understanding the Pitfalls of the Flyweight Pattern
          1. Understanding the Extrinsic Duplication Pitfall
          2. Understanding the Mutable Extrinsic Data Pitfall
          3. Understanding the Concurrent Access Pitfall
          4. Understanding the Over-optimization Pitfall
          5. Understanding the Misapplication Pitfall
        7. Examples of the Flyweight Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Creating the Flyweight Protocol and Implementation Class
          3. Creating the Flyweight Factory
          4. Applying the Flyweight
        9. Summary
      7. Chapter 18: The Proxy Pattern
        1. Preparing the Example Project
        2. Understanding the Problems That the Pattern Solves
          1. Understanding the Remote Object Problem
          2. Understanding the Expensive Operation Problem
          3. Understanding the Restricted Access Problem
        3. Understanding the Proxy Pattern
          1. Solving the Remote Object Problem
          2. Solving the Expensive Operation Problem
          3. Solving the Restricted Access Problem
        4. Implementing the Proxy Pattern
          1. Implementing the Remote Object Proxy
          2. Implementing the Expensive Operation Proxy
          3. Implementing the Access Restriction Proxy
        5. Variations on the Proxy Pattern
          1. Implementing a Reference Counting Proxy
        6. Understanding the Pitfalls of the Proxy Pattern
        7. Examples of the Proxy Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Protocol, Factory Method, and Proxy Class
          3. Updating the Product Data Store
          4. Sending Stock Level Updates
        9. Summary
    12. Part IV: The Behavioral Patterns
      1. Chapter 19: The Chain of Responsibility Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Chain of Responsibility Pattern
        4. Implementing the Chain of Responsibility Pattern
          1. Creating and Providing the Chain of Responsibility
          2. Applying the Chain of Responsibility Pattern
        5. Variations on the Chain of Responsibility Pattern
          1. Applying the Factory Method Pattern
          2. Indicating Whether Responsibility Was Taken for the Request
          3. Notifying All Links in the Chain
        6. Understanding the Pitfalls of the Pattern
        7. Examples of the Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Defining the Chain and its Links
        9. Summary
      2. Chapter 20: The Command Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Command Pattern
        4. Implementing the Command Pattern
          1. Defining the Command Protocol
          2. Defining the Command Implementation Class
          3. Applying the Command Pattern
          4. Applying Concurrent Protections
          5. Using the Undo Feature
        5. Variations on the Command Pattern
          1. Creating Composite Commands
          2. Using Commands as Macros
          3. Using Closures as Commands
        6. Understanding the Pitfalls of the Command Pattern
        7. Examples of the Command Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Implementing the Undo Feature
        9. Summary
      3. Chapter 21: The Mediator Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Mediator Pattern
        4. Implementing the Mediator Pattern
          1. Defining the Meditator Class
          2. Conforming to the Peer Protocol
          3. Implementing Concurrency Protections
        5. Variations on the Mediator Pattern
          1. Putting More Logic Into the Mediator
          2. Generalizing the Mediator-Peer Relationship
        6. Understanding the Pitfalls of the Mediator Pattern
          1. The Single Protocol Pitfall
        7. Examples of the Mediator Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
        9. Summary
      4. Chapter 22: The Observer Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Observer Pattern
        4. Implementing the Observer Pattern
          1. Creating the Base Subject Class
          2. Conforming to the Subject Protocol
          3. Conforming to the Observer Protocol
          4. Consuming the Pattern
        5. Variations on the Observer Pattern
          1. Generalizing Notifications
          2. Using Weak References
          3. Dealing with Short-Lived Subjects
        6. Understanding the Pitfalls of the Observer Pattern
        7. Examples of the Observer Pattern in Cocoa
          1. User Interface Events
          2. Observing Property Changes
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Applying the Pattern
        9. Summary
      5. Chapter 23: The Memento Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Memento Pattern
        4. Implementing the Memento Pattern
          1. Implementing the Memento Class
          2. Using the Memento
        5. Variations on the Memento Pattern
        6. Understanding the Pitfalls of the Memento Pattern
        7. Examples of the Memento Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
          1. Preparing the Example Application
          2. Implementing the Pattern
        9. Summary
      6. Chapter 24: The Strategy Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Strategy Pattern
        4. Implementing the Strategy Pattern
          1. Defining the Strategies and the Context Class
          2. Consuming the Pattern
        5. Variations on the Strategy Pattern
        6. Understanding the Pitfalls of the Strategy Pattern
        7. Examples of the Strategy Pattern in Cocoa
          1. Cocoa Protocol-Based Strategies
          2. Cocoa Selector-Based Strategies
        8. Applying the Pattern to the SportsStore Application
        9. Summary
      7. Chapter 25: The Visitor Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Visitor Pattern
        4. Implementing the Visitor Pattern
          1. Conforming to the Shape Protocol
          2. Creating the Visitors
          3. Applying the Visitors
        5. Variations on the Visitor Pattern
        6. Understanding the Pitfalls of the Visitor Pattern
        7. Examples of the Visitor Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
        9. Summary
      8. Chapter 26: The Template Method Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the Template Method Pattern
        4. Implementing the Template Method Pattern
        5. Variations on the Template Method Pattern
        6. Understanding the Pitfalls of the Pattern
        7. Examples of the Template Method Pattern in Cocoa
        8. Applying the Pattern to the SportsStore Application
        9. Summary
    13. Part V: The MVC Pattern
      1. Chapter 27: The Model/View/Controller Pattern
        1. Preparing the Example Project
        2. Understanding the Problem That the Pattern Solves
        3. Understanding the MVC Pattern
          1. Understanding the MVC Application Sections
        4. Implementing the MVC Pattern
          1. Defining the Common Code
          2. Defining the Framework
          3. Creating the Model
          4. Defining the View
          5. Defining the Controller
          6. Completing the Framework
          7. Running the Application
          8. Extending the Application
        5. Variations on the MVC Pattern
        6. Understanding the Pitfalls of the MVC Pattern
        7. Examples of the MVC Pattern in Cocoa
        8. Summary
    14. Index

    Product information

    • Title: Pro Design Patterns in Swift
    • Author(s):
    • Release date: January 2015
    • Publisher(s): Apress
    • ISBN: 9781484203941