Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Swift 2 Design Patterns
Swift 2 Design Patterns
Swift 2 Design Patterns
Ebook442 pages4 hours

Swift 2 Design Patterns

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book is intended for competent Swift developers who want to apply enduring design patterns with Swift to structure and scale their application code.
LanguageEnglish
Release dateOct 28, 2015
ISBN9781785886119
Swift 2 Design Patterns

Related to Swift 2 Design Patterns

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Swift 2 Design Patterns

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Swift 2 Design Patterns - Lange Julien

    Table of Contents

    Swift 2 Design Patterns

    Credits

    About the Author

    About the Reviewers

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    Why subscribe?

    Free access for Packt account holders

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book

    Errata

    Piracy

    eBooks, discount offers, and more

    Questions

    1. Creational Patterns

    The prototype pattern

    Roles

    Design

    Participants

    Collaborations

    Illustration

    Implementation

    Usage

    The factory method pattern

    Roles

    Design

    Participants

    Illustration

    Implementation

    Usage

    The singleton pattern

    Roles

    Design

    Participants

    Collaborations

    Illustration

    Implementation

    Usage

    The abstract factory pattern

    Roles

    Design

    Participants

    Collaborations

    Illustration

    Implementation

    Usage

    The builder pattern

    Roles

    Design

    Participants

    Collaborations

    Illustration

    Implementation

    Usage

    Implementation using closures

    Summary

    2. Structural Patterns – Decorator, Proxy, and Bridge

    Static and dynamic composition

    The decorator pattern

    Roles

    Design

    Participants

    Collaboration

    Sample

    Implementation

    Usage

    The proxy pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Usage

    The bridge pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Usage

    Summary

    3. Structural Patterns – Composite and Flyweight

    The composite pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Implementation of the VODComponent

    Implementation of the VODItem leaf

    Implementation of the VODCategory composite

    Usage

    The flyweight pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Usage

    Performance results

    Summary

    4. Structural Patterns – Adapter and Facade

    The adapter pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementing our first prototype

    Implementation

    Implementation of our adaptees

    Implementation of the SamSingAdapter class

    Implementation of the PearAdapter class

    The facade pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation of the facade

    Summary

    5. Behavioral Patterns – Strategy, State, and Template Method

    The strategy pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The state pattern

    Role

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The template method

    Role

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Summary

    6. Behavioral Patterns – Chain of Responsibility and Command

    The chain of responsibility pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The command pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Comparison between the chain of responsibility and command patterns

    Summary

    7. Behavioral Patterns – Iterator, Mediator, and Observer

    The iterator pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The mediator pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The observer pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Summary

    8. Behavioral Patterns – Visitor, Interpreter, and Memento

    The visitor pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The interpreter pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    The memento pattern

    Roles

    Design

    Participants

    Collaboration

    Illustration

    Implementation

    Comparing the three patterns

    Summary

    Index

    Swift 2 Design Patterns


    Swift 2 Design Patterns

    Copyright © 2015 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    First published: October 2015

    Production reference: 1231015

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-78588-761-1

    www.packtpub.com

    Credits

    Author

    Julien Lange

    Reviewers

    Vladimir Pouzanov

    Victor Sigler

    Ye Xiaodong

    Acquisition Editor

    Tushar Gupta

    Content Development Editor

    Shali Deeraj

    Technical Editor

    Saurabh Malhotra

    Copy Editors

    Rashmi Sawant

    Sneha Singh

    Project Coordinator

    Kinjal Bari

    Proofreader

    Safis Editing

    Indexer

    Hemangini Bari

    Graphics

    Disha Haria

    Jason Monteiro

    Abhinash Sahu

    Production Coordinator

    Komal Ramchandani

    Cover Work

    Komal Ramchandani

    About the Author

    Julien Lange is a 34-year-old IT expert in software engineering. He started developing on an Amstrad CPC464 with the BASIC language when he was 7 years old. He later learned Visual Basic 3/4, then VB.NET, and then C#. For several years until the end of his education, he developed and maintained several e-business websites based on PHP and ASP.NET. After his graduation, he continued to learn more and more about software, which included software architecture and project management, and always tried to acquire new skills. Since 2011, he has been working as an IT project manager on the lead management middleware of DSI Axa France (a French insurance company). This middleware is based on the SOA architecture. As this middleware is consumed by frontend users exposed on the Internet, performance is the top priority each time he delivers a new release of the system. Scalability and robustness are really important in his everyday work.

    He first developed an interest in mobile development in 2009. After discovering the massive potential of iPhone games and software, he decided to find an improved game engine, which would allow him to concentrate only on the main purpose of a game: developing a game and not a game engine. His choice was Unity 3D. Later, he took some time to learn the native iOS Objective-C language, which was quite difficult for him due to its language particularity and finally learned Swift since its first apparition in 2014. In addition to his main work as an IT consultant, he created iXGaming.com in December 2010. He currently has several projects in mind, including a game based on a French board game and a new website that delivers new services to developers. He is searching for a few partners to work with.

    After reviewing several books by Packt Publishing, I decided to write a book for them. I would especially like to thank all the members of Packt Publishing, including Shali, Tushar, and Sam. I would also thank, every reviewer and especially Victor Sigler for their times reviewing this book. Also, I would to thank my family, and most of all my wife, for allowing me to take some time to write this book. I would also like to thank a few people with whom I work everyday: Adrien D, Alain De L, Juliette O., Raphael D, Stephane D, Helmi C, and Christelle P.

    About the Reviewers

    Vladimir Pouzanov is a systems engineer and embedded enthusiast. He has spent countless hours hacking different mobile hardware, porting Linux to various devices that were not supposed to run it, and toying around outside the iOS sandbox. He has also been a professional iOS consultant and has been developing applications based on iOS ever since the first Apple iPhones became available. Later on, he switched his professional interest to systems engineering and cloud computing, but he still keeps a close eye on the mobile and embedded world.

    I'd like to give credit to my wife for her amazing support when I was working on the review and sharing my attention between her, our daughter, and the book.

    Victor Sigler is an iOS software engineer with experience of developing consumer and enterprise mobile applications. He loves everything that is related to Apple. He is passionate about Swift and the world of programming contests. He enjoys writing about iOS development on his blog at http://www.vsigler.com and helps people solve their queries on Stack Overflow. He can be found on Twitter at @Vkt0r.

    Ye Xiaodong is a full-stack software engineer and technical director of zai360.com, an O2O company that provides a recyclables collection service for Chinese family customers on a periodical basis. He has 7 years of application development experience working for start-ups and leading companies across the world on iOS, Android, Windows Phone, Symbian, and Meego. He has developed lots of iOS applications; designed, created, and maintained iOS libraries and Xcode plugins; and contributed to open source projects, as he is passionate about bringing the latest features to applications. He has worked as a technical reviewer for the book Mastering Swift, Packt Publishing.

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    For support files and downloads related to your book, please visit www.PacktPub.com.

    Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

    At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

    https://www2.packtpub.com/books/subscription/packtlib

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Free access for Packt account holders

    If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

    Preface

    This book will help you understand when and how to implement the 23 patterns, as described by the gang of four (GoF), with the new language provided by Apple: Swift.

    The main idea behind this book is to make it a reference book for implementing a specific pattern. This is the reason why I have divided this book into three categories: creational, structural, and behavioral patterns. For each category, you will find a chapter with a common structure: roles, a UML class diagram, participants, collaboration, illustration, and implementation with Swift.

    This structure is an easy way to find the answers that you may ask yourself. In this book, I will first introduce you to the five creational patterns, followed by seven structural patterns, and then conclude with the eleven behavioral patterns, as defined by the GoF.

    What this book covers

    Chapter 1, Creational Patterns, introduces you to the five patterns of a creational pattern category: the prototype, factory method, singleton, abstract factory, and builder patterns.

    Chapter 2, Structural Patterns – Decorator, Proxy, and Bridge, introduces you to structural patterns and helps you explore the decorator, proxy, and bridge patterns.

    Chapter 3, Structural Patterns – Composite and Flyweight, teaches you how to deal with the structure of multiple objects using the composite and flyweight patterns.

    Chapter 4, Structural Patterns – Adapter and Facade, teaches you how to join the two types that were not designed to work with each other together, thanks to the adapter pattern. Then, you'll learn how the facade pattern can help you simplify the interface of a set of complex systems.

    Chapter 5, Behavioral Patterns – Strategy, State, and Template Method, introduces you to the behavioral patterns. In this chapter, we will discuss the strategy, state, and template method patterns.

    Chapter 6, Behavioral Patterns – Chain of Responsibility and Command, introduces you to two other behavioral patterns that are concerned with passing requests to an appropriate object that will then execute the action.

    Chapter 7, Behavioral Patterns – Iterator, Mediator, and Observer, provides a way to implement communication between objects while keeping their independence and anonymity.

    Chapter 8, Behavioral Patterns – Visitor, Interpreter, and Memento, concludes with the discovery and implementation of the 23 patterns, as defined by the GoF.

    What you need for this book

    The only requirement for this book is to have Xcode 7 installed on your Mac computer. All the code provided has been written, compiled, and tested with Swift 2. You don't need any other software to follow the examples provided in this book. All samples in these book have been written using a OSX Command line tool project or a Playground file.

    Who this book is for

    This book is intended for beginners as well as senior developers who want to take the next step in the software engineering industry.

    This book will help you learn about the difference between a low-level programmer and a programmer with design pattern knowledge. You can apply this knowledge with Swift in Xcode to design scalable and flexible apps or games for iOS and Mac. As design patterns are not the exclusive reserve of Swift, but can be used by any other language, this is the knowledge that

    Enjoying the preview?
    Page 1 of 1