Book Image

Delphi Cookbook

By : Daniele Teti
Book Image

Delphi Cookbook

By: Daniele Teti

Overview of this book

Table of Contents (14 chapters)
Delphi Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 already did something similar to what you are doing, and can discuss it as he/she may have faced the same problem. It is not possible to include all the possible situations that a developer may face in a book, but most problems are similar at least in principle. This is the reason this book is organized as a cookbook; just like how a combination of foods can be adapted and modified to be appropriate for different types of meals. A programming recipe can provide the idea to solve many different problems.

This book is an advanced-level guide that will help Delphi developers become experts in their every day job. The every day job and the quality of your deliverables is what contribute to the quality of your professional life. It does not make sense to reinvent the wheel repeatedly, especially when working with a well-established tool such as Delphi. The focus of this 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 this book are a result of years of development, training, and consultancy activities in the most different fields of the IT industry, from small systems with thousands of installations to large systems commissioned by any big company or government. It is not a magic book that will solve all your development problems (if you find it, tell me please!), but it can be a valid source of help 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 enumerables, extended RTTI and duck typing, LiveBindings, multithreading, FireMonkey, mobile development, server-side development and many more, you will be pleasantly surprised as to how quickly and easily you can use Delphi to write high quality, clean, readable, maintainable, and extensible code.

I have read too many boring programming books, so I tried to maintain a relaxed and light exposition. A small applicability scenario, which describes a situation where a particular technology, approach, or design pattern can be used successfully, introduces all the recipes. The recipes are not too complex because otherwise the book may consist of thousands of pages; however, it is also not trivial because the IT books landscape is already full of trivial examples with a few direct applications. I tried to do a good trade-off and hope I succeeded.

Every time I start to read a new book, I ask myself, "Will the author have something interesting to say?", "How much will this book change my point of view on the topics it talks about?", or "Will it be worth the time to read this book?". Now that I'm on the other side of the river, I worked hard to put as much good quality content as possible in this book, which I hope will match your expectations.

On a final note, writing hundreds of pages about advanced programming is not an easy task. However, I am very pleased to have done it, and I hope you will enjoy reading it as much as I enjoyed writing it.

What this book covers

Chapter 1, Delphi Basics, talks about a set of general approaches that should not be ignored by any Delphi programmer. Some recipes are simple, while some are not, but all of them should be deeply understood. By the end of this chapter, you will be able to use some of the fundamental Delphi techniques related to the RTL, VCL, and OS integration.

Chapter 2, Become a Delphi Language Ninja, focuses on the Object Pascal language. A programming language is the way you talk to the machine, so you must be fluent and should know all the possibilities offered. This chapter talks about higher-order functions, practical utilization of the extended RTTI, regular expressions, and other things useful to augment the power of your code and lower the amount of time spent on debugging.

Chapter 3, Going Cross Platform with FireMonkey, is dedicated to the FireMonkey framework in general. What you will learn from this chapter can be used in many of the platforms FireMonkey supports. Moreover, you will learn about nontrivial LiveBindings utilizations.

Chapter 4, The Thousand Faces of Multithreading, is one of the most complex chapters. It talks about thread synchronization and the mechanisms used to obtain this synchronization, including TMonitor, thread-safe queues, and TEvent. By the end of this chapter, you will be able to create and communicate with background threads, leaving the main thread free to update your GUI (or communicate with the OS).

Chapter 5, Putting Delphi on the Server, focuses on how well Delphi can behave when running on a server. Some people think that Delphi is a client-only tool, but it is not true; the number of Delphi server-side systems running all over the world prove it! In this chapter, we'll show how to create powerful servers that offer services over a network. Then, in one of the recipes, we'll also implement a JavaScript client that brings the database data to the user's browser. The techniques explained in this chapter open a range of possibilities, especially in the mobile and web area.

Chapter 6, Riding the Mobile Revolution with FireMonkey, is dedicated to mobile development with Delphi and FireMonkey. If you are interested in mobile development, I think that this will be your favorite chapter! Mobiles are everywhere and this chapter will explain how to write software for your Android or iOS device, what are the best practices to use, how to save your data on your mobile device, how to retrieve and update remote data, and how to integrate with a mobile operating system.

Chapter 7, Using Specific Platform Features, shows you how to integrate your app with the underlying mobile operating systems beyond what FireMonkey offers. You will learn how to import Java and Objective-C libraries in your app and use the SDK classes from your Object Pascal code.

What you need for this book

This book talks about Delphi, so you need Delphi. Not all recipes are available in all the Delphi editions. Typically, the mobile projects can be compiled only if you have Delphi Enterprise or better (or Delphi Professional plus the mobile add-on, or RAD Studio professional, or better). All the projects are compiled and tested on Delphi XE6. Many of the recipes can also be compiled on older versions.

If you want to run the mobile app on a phone or tablet, you can use the Android emulator or iOS simulator, but it is strongly recommended that you use an actual device to see how the app really behaves. To deploy an iOS app on your device, you also need an Apple computer with Mac OS X. More information is provided in the related chapters.

Who this book is for

This book aims to help professional Delphi developers in their day-to-day jobs. This book will teach you about the newest Delphi technologies and its hidden gems. It is not a book for a newbie, but the practical approach will help you reach a new level in your Delphi skills. An experienced developer will benefit from this book because nontrivial problems are solved using the best practices. Where more than one way is available or the topics are too vast to be explained in the available pages, references are provided that allow interested readers to go deeper in that field. It is a book that'll hold on to your desk for the next few years.

Conventions

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

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The TStyleManager.StyleNames property contains all names of the available styles."

A block of code is set as follows:

LogMessage('Your message goes here for SUCCESS', 
         EVENTLOG_SUCCESS, 0, 1);

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

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  TStyleManager.TrySetStyle('Iceberg Classico');
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

Any command-line input or output is written as follows:

C:\<ExeProjectPath>\WindowsService.exe /install
C:\<ExeProjectPath>\WindowsServiceOrGUI.exe /GUI

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on Start, wait for the confirmation, and the service should start to write its logfile."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on 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 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 files e-mailed directly to you.

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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.