You are previewing Swift 2 Design Patterns.
O'Reilly logo
Swift 2 Design Patterns

Book Description

Build robust and scalable iOS and Mac OS X game applications

About This Book

  • Learn to use and implement the 23 Gang of Four design patterns using Swift 2

  • Design and architect your code for Swift application development

  • Understand the role, generic UML design, and participants in the class diagram of the pattern by implementing them in a step-by-step approach

  • Who This Book Is For

    This book is intended for competent Swift developers who want to apply enduring design patterns with Swift to structure and scale their application code.

    What You Will Learn

  • Choose the appropriate pattern depending on the problem to be solved

  • Understand the generic class diagram of each of the 23 GoF patterns and how each object participates in the pattern

  • Use Swift to implement these patterns even though the language doesn’t provide all of the object-oriented programming concepts such as abstract class, interface, and so on

  • Architect your software to avoid the overuse of memory, time spent on calculations, or high network traffic

  • Find the best way to organize your code to make it more secure from the outside world

  • Prepare your code to make it more flexible when the consumer changes or the third-party component hidden code changes

  • Structure your code to change the algorithm to apply at runtime

  • Deliver Flyweight responsibility to your objects

  • In Detail

    Swift is a multi-paradigm language. It has expressive features familiar to those used to work with modern functional languages, while also keeping the object-oriented features of Objective-C. It remains compatible with Apple’s legacy codes and frameworks. A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. Knowledge about design patterns is also one of the best ways to make you different compared to other low-level developers.

    This book shows you how to use Swift 2 to learn about 23 Gang of Four (GoF) design patterns, and is organized into three categories. The book will present you the five creational patterns, followed by the seven structural patterns, and finishing with the 11 behavioral patterns as defined by the GoF. Each chapter will introduce the pattern by defining its role, which common problems the pattern should be used for, its generic UML representation, how each objects presented in the class diagram participate in the pattern, and what the role of each of these objects is. The book then presents you with a concrete case as an illustration that will be used to implement the pattern using Swift.

    Style and approach

    A step-by-step tutorial completed with screenshots and code highlights wherever necessary. Each chapter discusses one or more patterns with its definitions and a simple-to-follow illustration case using a playground or XCText project to implement it with Swift.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Swift 2 Design Patterns
      1. Table of Contents
      2. Swift 2 Design Patterns
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. eBooks, discount offers, and more
          6. Questions
      8. 1. Creational Patterns
        1. The prototype pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaborations
          5. Illustration
          6. Implementation
          7. Usage
        2. The factory method pattern
          1. Roles
          2. Design
          3. Participants
          4. Illustration
          5. Implementation
          6. Usage
        3. The singleton pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaborations
          5. Illustration
          6. Implementation
          7. Usage
        4. The abstract factory pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaborations
          5. Illustration
          6. Implementation
          7. Usage
        5. The builder pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaborations
          5. Illustration
          6. Implementation
          7. Usage
          8. Implementation using closures
        6. Summary
      9. 2. Structural Patterns – Decorator, Proxy, and Bridge
        1. Static and dynamic composition
        2. The decorator pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Sample
          6. Implementation
          7. Usage
        3. The proxy pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
          7. Usage
        4. The bridge pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
          7. Usage
        5. Summary
      10. 3. Structural Patterns – Composite and Flyweight
        1. The composite pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
            1. Implementation of the VODComponent
            2. Implementation of the VODItem leaf
            3. Implementation of the VODCategory composite
          7. Usage
        2. The flyweight pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
          7. Usage
          8. Performance results
        3. Summary
      11. 4. Structural Patterns – Adapter and Facade
        1. The adapter pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
            1. Implementing our first prototype
            2. Implementation
            3. Implementation of our adaptees
            4. Implementation of the SamSingAdapter class
            5. Implementation of the PearAdapter class
        2. The facade pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
            1. Implementation of the facade
        3. Summary
      12. 5. Behavioral Patterns – Strategy, State, and Template Method
        1. The strategy pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        2. The state pattern
          1. Role
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        3. The template method
          1. Role
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        4. Summary
      13. 6. Behavioral Patterns – Chain of Responsibility and Command
        1. The chain of responsibility pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        2. The command pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        3. Comparison between the chain of responsibility and command patterns
        4. Summary
      14. 7. Behavioral Patterns – Iterator, Mediator, and Observer
        1. The iterator pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        2. The mediator pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        3. The observer pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        4. Summary
      15. 8. Behavioral Patterns – Visitor, Interpreter, and Memento
        1. The visitor pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        2. The interpreter pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        3. The memento pattern
          1. Roles
          2. Design
          3. Participants
          4. Collaboration
          5. Illustration
          6. Implementation
        4. Comparing the three patterns
        5. Summary
      16. Index