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č

Compartir libro
  1. 476 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Hands-On Design Patterns with Delphi

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

Primož Gabrijelčič

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Hands-On Design Patterns with Delphi un PDF/ePUB en línea?
Sí, puedes acceder a Hands-On Design Patterns with Delphi de Primož Gabrijelčič en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming Languages. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2019
ISBN
9781789342437
Edición
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 ...

Índice