Delphi Cookbook
Third Edition
Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development
Daniele Spinetti
Daniele Teti
BIRMINGHAM - MUMBAI
Delphi CookbookThird Edition
Copyright 2018 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 authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Merint Mathew
Acquisition Editor: Alok Dhuri
Content Development Editor: Priyanka Sawant
Technical Editor: Ruvika Rao
Copy Editor: Safis Editing
Project Coordinator: Vaidehi Sawant
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Graphics: Jason Monteiro
Production Coordinator: Shraddha Falebhai
First published: September 2014
Second edition: June 2016
Third edition: July 2018
Production reference: 1310718
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-130-4
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
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 service@packtpub.com 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.
Contributors
About the authors
Daniele Spinetti is a software architect living in Rome. He is an Embarcadero MVP. Delphi/Object Pascal is his favorite tool/programming language, and he is a lead and active member of several projects in the open source community. In his tutoring activities (conferences and training), he likes to talk about innovative topics in software architectures. He's a huge fan of design patterns and TDD.
Daniele Teti is a software architect, trainer, and consultant with over 20 years of experience. He drives the development of the most popular Delphi open source project on GitHubDelphiMVCFramework. He's also a huge fan of design patterns, machine learning, and AI. Daniele is the CEO of BIT Time Professionals, an Italian company specializing in high-level consultancy, training, development, and machine learning systems.
About the reviewer
Primo Gabrijeli started coding in Pascal on 8-bit micros in the 1980s, and he never looked back. In the last 20 years, he was mostly programming high-availability server applications used in the broadcasting industry. He's also an avid writer and has written several hundred articles, and he is a frequent speaker at Delphi conferences, where he likes to talk about complicated topics, ranging from memory management to creating custom compilers.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
To my family, to the memory of my grandfather, and to my friends. I want to thank each of them for their love, support, and inspiration.
I would like to extend my gratitude to Daniele Teti. I work closely with Daniele for several years and I learned many things from him, but the most important it's like thinking, asking the right questions and correctly approaching a problem. I am proud to work with him.
I want to leave you a funny anecdote from when I was a boy:
Mom: Daniele, dinner's ready!! C'mon! Daddy's hungry!
Me: I'm coming, just a moment!
Mom: Daniele hurry up, all the day that you're at computer, it does not feed you!
Preface
If you've been a software developer for a long time, you certainly know how useful a conversation can be with a colleague who has already done something similar to what you are doing and can explain it, as they will have faced the same problem. It is not possible to put all the possible situations that a developer can face in a book, but many problems are similar, at least in principle. This is the reason this book is organized as a cookbook: just like a combination of foods can be adapted and modified to be appropriate for different types of dinner, a programming recipe can provide the idea to solve many different problems. This book is an advanced guide that will help Delphi developers get more skilled in their everyday job. The everyday job, and the quality of your deliverables, is what is contributing to the quality of your professional life. If it does not make sense, reinventing the wheel repeatedly, especially when working with a well-established tool, such as Delphi. The focus of the book is to provide readers with comprehensive and detailed examples on how effectively the Delphi software can be designed and written. All the recipes in the book are the result of years of development, training, and consultancy activities in many different fields of IT, from small systems with thousands of installations to large systems commissioned by big companies or by governments. It is not a magic book that will solve all your development problems (if you find it, tell me, please!), but it could be helpful to get a different point of view on a specific problem, or a hint on how to solve problems. Armed with the knowledge of advanced concepts, such as high-order functions and anonymous methods, generics and enumerable, extended RTTI and duck typing, LiveBindings, multi-threading, FireMonkey, mobile development, server-side development, and IoT, you will be pleasantly surprised how quickly and easily you can use Delphi to write high-quality, clean, readable, fast, maintainable, and extensible code. I read too many boring programming books, so I tried to maintain a relaxed and light exposition. A small applicability scenario that describes a situation where a particular technology, approach, or design pattern can be used successfully introduces all the recipes. The recipes are not very complex, because otherwise the book may become thousands of pages long, but also not trivial, because the IT books landscape is already full of simple examples with few direct applicabilities. I tried to do a good tradeoff, and I hope to be able to do it. Every time I start reading a new book, I ask myself,
Next page