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

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 any developer and software architect must have.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text as are shown as follows: "AbstractExpression: It defines an interpret() method that is common to all node in the abstract syntax tree."

A block of code is set as follows:

class WalkMoveStrategy:IMoveStrategy{
  func performMove() {
    print("I am walking")
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

class WalkMoveStrategy:IMoveStrategy{
  func performMove() {
    print("I am walking")
  }
}

Any console output is written as follows:

Printing checkPoint....
Level: 0   Weapon: gun   Points: 1200 
Level: 1   Weapon: tommy gun   Points: 2250 
Level: 2   Weapon: bazooka   Points: 2400 
Level: 4   Weapon: knife   Points: 3000 
Total Points: 8850

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on the Show the Test navigator button on the left-hand side."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Swift_2_Design_patterns_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

eBooks, discount offers, and more

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.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.