Exploring Design Patterns

Introduction

Design patterns are solutions to common software design problems. Since they’re patterns, you can adapt them to various contexts that fit the problem space they’re designed for. These patterns are reliable since they’ve been tested and proven to work over many years. Many design patterns are available, each designed to solve specific problems. For mobile app development, it’s essential to know some of these patterns, as they address common problems.

Exploring Practical Examples

Consider the scenario where you order food at a restaurant. The way you make the request is similar to how everyone else does it. You choose from predefined meals or customize your order with toppings and beverages. The server sends your request to the kitchen and returns with your order. The other person does the same and receives a different dish based on their choices. You can model how the food is created with a factory pattern in software. This pattern creates different types of objects (foods) depending on the order requested. You can apply this pattern anywhere you need to use a single interface to get different kinds of objects.

Advantages of Design Patterns

Having well-known and accepted patterns makes code easy to read and understand. These patterns simplify documentation and communication since they define terminologies and techniques. Anyone who knows about a pattern or looks it up for the first time will be able to understand your code easily.

Design patterns are tried and tested, guaranteeing a certain level of code quality. They help solve problems easily and speed up development since many professionals have already tested them. Using design patterns promotes elements of SOLID principles in object-oriented programming, making software scalable, testable, and easy to maintain. You don’t have to reinvent the wheel when so many suitable design patterns are available.

Disadvantages of Design Patterns

Design patterns focus on solving specific structural, creational, and behavioral problems without much consideration for efficiency. This means these patterns aren’t necessarily the most efficient, even though they may be excellent solutions to their specific problems. There’s a tendency to apply them extensively without considering that some apps may have other conditions and use cases that would make some design patterns unsuitable. They may lead to the creation of more classes and files, affecting the complexity of the code.

Common Design Patterns in Mobile App Development

Some problems are common in mobile app development, and these are some common design patterns frequently used to solve them:

  • Observer: Establishes a relationship between two or more objects where one object is interested in the other object’s state. It’s used in notification systems to ensure the subscriber is always updated about the publisher’s state.
  • Adapter: Also known as wrapper, this pattern transforms one interface to another, allowing two incompatible objects to communicate.
  • Singleton: This pattern ensures that only one instance of an object is available throughout the app’s lifetime.
  • Builder: This pattern is designed to construct complex objects from simple objects one step at a time.
  • Factory: This pattern provides a way to create different kinds of objects from a single interface.

Reviewing Types of Design Patterns

Design patterns can be grouped into three main types:

  • Creational: Focuses on how objects are instantiated. They can be either class-creational patterns or object-creational patterns.
  • Structural: Defines how a class is composed and structured. The main goal of most of these patterns is to increase the functionality of the class(es) involved without changing much of its composition.
  • Behavioral: Focuses on how classes communicate with each other.

In this lesson, you’ll learn about three of these design patterns and apply them in an Android mobile app context. You’ll start with the singleton pattern.

See forum comments
Download course materials from Github
Previous: Introduction Next: Learning Singleton Pattern