Chapter 1. Design Patterns
Design patterns have long been considered some of the most reliable and useful approaches to solving common software design problems. Patterns provide general and reusable solutions to frequently occurring development issues, such as how to add functionality to an object without changing its structure or how best to construct complex objects.
There are several advantages to applying patterns, not least the way that this approach assists the developer in following best practices and how it simplifies the management of large projects. These benefits are achieved by providing overall software structures (patterns) that can be reused to solve similar problems. This is not to say that code can be simply cut and pasted from one project to another but that the concepts themselves can be used over and over again in many different situations.
There are a good many other benefits to applying programming patterns, all of which will be covered at some point in the book, but here are one or two that are worthy of mention now:
- Patterns provide an efficient common language between developers working in teams. When a developer describes a structure as say, an adapter or a facade, other developers will understand what this means and will immediately recognize the structure and purpose of the code.
- The added layers of abstraction that patterns provide make modifications and alterations to code that is already under development much easier. There are even patterns designed for these very situations.
- Patterns can be applied at many scales, from the overall architectural structure of a project right down to the manufacturing of the most basic object.
- The application of patterns can vastly reduce the amount of inline commentary and general documentation required, as patterns also act as their own description. The name of a class or interface alone can explain its purpose and place within a pattern.
The Android development platform lends itself nicely to the employment of patterns, as not only are applications created largely in Java, but the SDK contains many APIs that make use of patterns themselves, such as factory interfaces for creating objects and builders for constructing them. Simple patterns such as Singletons are even available as a template class type. In this book, we shall see not only how to put together our own, large-scale patterns but also how make use of these built-in structures to encourage best practice and simplify coding.
In this chapter, we will begin by taking a brief look at how the book as a whole will pan out, the patterns we will be using, the order in which we will approach them, and the demonstration app that we will be building to see how patterns can be applied in real-world situations. This will be followed by a quick examination of the SDK and which components will best assist us on our journey, in particular, the role that the support library provides, enabling us to develop for many platform versions at once. There is no better way to learn than actual experience, and so the rest of the chapter will be taken up by developing a very simple demonstration app and employing our first pattern, the factory pattern, and its associated abstract factory pattern.
In this chapter, you will learn the following:
- How patterns are categorized and which patterns are covered here
- The purpose of the book's demonstration app
- How to target platform versions
- What support libraries do
- What a factory pattern is and how to construct one
- How to follow a UML class diagram
- How to test an app on both real and virtual devices
- How to monitor an app during runtime
- How to use simple debugging tools to test code
- What an abstract factory pattern is and how to use one