Hands-On Design Patterns with Delphi
eBook - ePub

Hands-On Design Patterns with Delphi

Build applications using idiomatic, extensible, and concurrent design patterns in Delphi

Primož Gabrijelčič

Share book
  1. 476 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Design Patterns with Delphi

Build applications using idiomatic, extensible, and concurrent design patterns in Delphi

Primož Gabrijelčič

Book details
Book preview
Table of contents
Citations

About This Book

Get up to speed with creational, structural, behavioral and concurrent patterns in Delphi to write clear, concise and effective code

Key Features

  • Delve into the core patterns and components of Delphi in order to master your application's design
  • Brush up on tricks, techniques, and best practices to solve common design and architectural challenges
  • Choose the right patterns to improve your program's efficiency and productivity

Book Description

Design patterns have proven to be the go-to solution for many common programming scenarios. This book focuses on design patterns applied to the Delphi language. The book will provide you with insights into the language and its capabilities of a runtime library.

You'll start by exploring a variety of design patterns and understanding them through real-world examples. This will entail a short explanation of the concept of design patterns and the original set of the 'Gang of Four' patterns, which will help you in structuring your designs efficiently. Next, you'll cover the most important 'anti-patterns' (essentially bad software development practices) to aid you in steering clear of problems during programming. You'll then learn about the eight most important patterns for each creational, structural, and behavioral type. After this, you'll be introduced to the concept of 'concurrency' patterns, which are design patterns specifically related to multithreading and parallel computation. These will enable you to develop and improve an interface between items and harmonize shared memories within threads. Toward the concluding chapters, you'll explore design patterns specific to program design and other categories of patterns that do not fall under the 'design' umbrella.

By the end of this book, you'll be able to address common design problems encountered while developing applications and feel confident while building scalable projects.

What you will learn

  • Gain insights into the concept of design patterns
  • Study modern programming techniques with Delphi
  • Keep up to date with the latest additions and program design techniques in Delphi
  • Get to grips with various modern multithreading approaches
  • Discover creational, structural, behavioral, and concurrent patterns
  • Determine how to break a design problem down into its component parts

Who this book is for

Hands-On Design Patterns with Delphi is aimed at beginner-level Delphi developers who want to build scalable and robust applications. Basic knowledge of Delphi is a must.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Hands-On Design Patterns with Delphi an online PDF/ePUB?
Yes, you can access Hands-On Design Patterns with Delphi by Primož Gabrijelčič in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789342437
Edition
1

Section 1: Design Pattern Essentials

The objective of this part is to introduce the reader to the patterns, provide some historical background, and establish the context.
This section will comprise the following chapters:
Chapter 1, Introduction to Patterns

Introduction to patterns

Patterns are everywhere! In architecture, patterns help architects plan buildings and discuss their projects. In programming, they help programmers organize programs and think about the code. They also help to create beautiful knitwear, and help people navigate safely through traffic—in short, they affect your everyday life.
The human brain is a pattern—finding analog computer, so it is not surprising that we humans like to base our life around patterns. We programmers are especially fond of organized, pattern—based thinking.
There are different areas of programming where patterns can be applied, from organizational aspects to coding. This book deals mostly with a subset of programming patterns, namely design patterns. Before we start describing and implementing different design patterns, however, I'd like to talk to you a bit about the history of patterns, their best points, and how they are often misused in practice.
This chapter will cover the following topics:
  • What design patterns are
  • Why patterns are useful
  • The difference between patterns and idioms
  • The origins of design patterns
  • The classification of common patterns
  • Pattern misuse and anti-patterns

Patterns in programming

The concept of a pattern is simple to define. A pattern is something that you did in the past, was successful, and can be applied to multiple situations. Patterns capture experiences in software development that have been proven to work again and again, and thus provide a solution to specific problems. They are not invented: they arise from practical experience.
When many programmers are trying to solve similar problems, they arrive again and again at a solution that works best. Such a solution is later distilled into a solution template, something that we programmers then use to approach similar problems in the future. Such solution templates are often called patterns.
Good patterns are problem and language agnostic. In other words, they apply to C++ and Delphi, and to Haskell and Smalltalk. In practice, as it turns out, lots of patterns are at least partially specific to a particular environment. Lots of them, for example, work best with object-oriented programming (OOP) languages and do not work with functional languages.
In programming, patterns serve a dual role. Besides being a template for problem solving, they also provide a common vocabulary that programmers around the world can use to discuss problems. It is much simpler to say, for example, that we will use an observer pattern to notify subsystems of state changes than it is to talk at length about how that part will be implemented. Using patterns as a base for discussion therefore forces us to talk about implementation concepts, and not about the detailed implementation specifics.
It is important to note that patterns provide only a template for a solution and not a detailed recipe. You will still have to take care of the code and make sure that the pattern implementation makes sense and works well with the rest of the program.
Programming patterns can be split into three groups that cover different abstraction levels. At the very top, we talk about architectural patterns. They deal with program organization as a whole, with a wide, top-down view, but they do not deal with implementation. For example, the famous Model-View-ViewModel approach is an architectural pattern that deals with a user interface-business logic split.
Architectural patterns are not a topic of this book, but still I'll dedicate some space to them in Chapter 11, Other Kinds of Patterns.
A bit lower down the abstraction scale are design patterns. They describe the run—time behavior of a program. When we use design patterns, we are trying to solve a specific problem in code, but we don't want to go fully to the code level. Design patterns will be the topic of the first ten chapters.
Patterns that work fully on the code level are called idioms. Idioms are usually language specific and provide templates for commonly encountered coding problems. For example, a standard way of creating/destroying objects in Delphi is an idiom, as is iterating over an enumerable container with the for..in construct.
Idioms are not the topic of this book. I will, however, mention the most important Delphi idioms, while talking about their specific implementation for some of the patterns.

Patterns are useful

This is not a book about the theory behind patterns; rather, this book focuses on the aspects of their implementation. Before I scare you all off with all this talk about design patterns, their history, modern advances, anti-patterns, and so on, I have decided to present a very simple pattern using an example. A few lines of code should explain why a pattern—based approach to problem solving can be a good thing.
In the code archive for this chapter, you'll find a simple console application called DesignPatternExample. Inside, you'll find an implementation of a sparse array, as shown in the following code fragment:
type
TSparseRec = record
IsEmpty: boolean;
Value : integer;
end;

TSparseArray = TArray<TSparseRec>;
Each array index can either be empty (in which case IsEmpty will be set to True), or it can contain a value (in which case IsEmpty will be set to False and Value contains the value).
If we have a variable of the data: TSparseArray type, we can iterate over it with the following code:
for i := Low(data) to High(data) do
if not data[i].IsEmpty then
Process(data[i].Value);
When you need a similar iteration in some other part of the program, you have to type this short fragment again. Of course, you could also be smart and just copy and paste the first two lines (for and if). This is simple but problematic, because it leads to the copy and paste anti-pattern, which I'll discuss later in this chapter.
For now, let's imagine the following hypothetical scenario. Let's say that at some point, you start introducing nullable types into this code. We already have ready to use nullable types available in the Spring4D library (https://bitbucket.org/sglienke/spring4d), and it was suggested that they will appear in the next major Delphi release after 10.2 Tokyo, so this is definitely something that could happen.
In Spring4D, nullable types are implemented as a Nullable<T> record, which is partially shown in the following code:
type
Nullable<T> = record
...
property HasValue: Boolean read GetHasValue;
property Value: T read GetValue;
end;
As far as we know, Delphi's implementation will expose the same properties: HasValue and Value.

You can then redefine TSparseArray as an array of Nullable<integer>, as the following code:
type
TSparseArray = TArray<Nullable<integer>>;
This is all well and good, but we now have to fix all the places in the code where IsEmpty is called and replace it with HasValue. We also have to change the program logic in all of these places. If the code was testing the result of IsEmpty, we would have to use not HasValue and vice versa. This is all very tedious and error prone. When making such a change in a big program, you can easily forget to insert or remove the not, and that breaks the program.
Wouldn't it be much better if there were only one place in the program when that for/if iteration construct was implemented? We would only have to correct code at that one location and— voila!—the program would be working again. Welcome to the Iterator pattern!
We'll discuss this pattern at length in Chapter 7, Iterator, Visitor, Observer, and Memento. For now, I will just give you a practical example.
The simplest way to add an iterator pattern to TScatteredArray is to use a method that accepts such an array and an iteration method, that is, a piece of code that is executed for each non empty element of the array. As the next code example shows, this is simple to achieve with Delphi's anonymous methods:
procedure Iterate(const data: TSparseArray; const iterator: TProc<integer>);
var
i: Integer;
begin
for i := Low(data) to High(data) do
if not data[i].IsEmpty then
iterator(data[i].Value);
end;
In this example, data is the sparse array that we want to array over, and iterator represents the anonymous method ...

Table of contents