Book Image

Apex Design Patterns

By : Anshul Verma, Jitendra Zaa
Book Image

Apex Design Patterns

By: Anshul Verma, Jitendra Zaa

Overview of this book

Apex is an on-demand programming language providing a complete set of features for building business applications – including data models and objects to manage data. Apex being a proprietor programming language from Salesforce to be worked with multi tenant environment is a lot different than traditional OOPs languages like Java and C#. It acts as a workflow engine for managing collaboration of the data between users, a user interface model to handle forms and other interactions, and a SOAP API for programmatic access and integration. Apex Design Patterns gives you an insight to several problematic situations that can arise while developing on Force.com platform and the usage of Design patterns to solve them. Packed with real life examples, it gives you a walkthrough from learning design patterns that Apex can offer us, to implementing the appropriate ones in your own application. Furthermore, we learn about the creational patterns that deal with object creation mechanism and structural patterns that helps to identify the relationship between entities. Also, the behavioural and concurrency patterns are put forward explaining the communication between objects and multi-threaded programming paradigm respectively. We later on, deal with the issues regarding structuring of classes, instantiating or how to give a dynamic behaviour at a runtime, with the help of anti-patterns. We learn the basic OOPs principal in polymorphic and modular way to enhance its capability. Also, best practices of writing Apex code are explained to differentiate between the implementation of appropriate patterns. This book will also explain some unique patterns that could be applied to get around governor limits. By the end of this book, you will be a maestro in developing your applications on Force.com for Salesforce
Table of Contents (12 chapters)
Apex Design Patterns
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface

Preface

It was spring 2015; we were both discussing our presentation for Dreamforce 2015. During the discussion, we stumbled on the topic of the usage of design patterns in Apex. We observed that design patterns are still not extensively used in the Apex programming arena. We think that a few prominent factors for this situation are that there are not enough resources available around the design pattern implementation in Apex. Secondly, most of the Apex programmers who started their development career with Force.com have less exposure to other development platforms. Thirdly, some of the design patterns simply do not apply to Force.com development platforms.

As per our observation, the usage and mention of design patterns in day-to-day programming is reduced in Apex as compared to other development platforms. Developers coming from other programming backgrounds mostly have a working experience with design patterns and it's implicit for them to use it in their code style and design.

We realized that it would be even better to develop content around the design pattern implementation in a language that Apex developers can easily relate to. We positively think that by providing scenarios that are specific to Apex development use cases, developers will be able to understand and implement the design patterns in Apex with ease.

To ensure that we are able to provide information uniformly to all Apex developers irrespective of their experience level, this book also includes general programming best practices, programming principles, OOP concepts, and the usual pitfalls at appropriate places.

In line with our past experience with design pattern learning and implementation is the problem of memorizing the design patterns and situations where they fit in. However, in this book, we took real-life scenarios to explain a situation where an appropriate design pattern will be applied. One can easily relate the design pattern with an example given in this book to identify when it is the time to use that pattern.

Some open source GitHub repository links, Salesforce official developer blogs, and other tips have been shared throughout the book, which can be very helpful in Force.com project development.

What this book covers

Chapter 1, An Introduction to Apex Design Pattern, covers the basics of inheritance, abstract classes, polymorphism, cohesion and coupling, and object-oriented design principles.

Chapter 2, Creational Patterns, focuses on different ways to instantiate objects to achieve code reusability, ease of maintenance, and loose coupling.

Chapter 3, Structural Patterns, explains the design patterns that can solve many situations and problems, which occur during project development in Apex, that demand a change in functionalities and cause a ripple effect on the rest of the system.

Chapter 4, Behavioral Patterns, provides a glimpse of some problems, such as double dispatch and code maintenance issues, and explains how to use appropriate design patterns to fix them.

Chapter 5, Handling Concurrency in Apex, discusses the design solution for the race condition in concurrency, which can result in data corruption without anyone noticing it.

Chapter 6, Anti-patterns and Best Practices, shows some common pitfalls observed during application development in Apex and suggested solutions.

What you need for this book

We need an Internet-enabled computer that is capable of running modern browsers supported by Salesforce. We can use the developer console in a browser to write the Apex code in Salesforce.

Interested readers can also use their favorite IDEs, such as Sublime Text, Eclipse, Welkins, Atom, Aside, Cloud9, or BrainEngine.

You will also need a free Salesforce developer account to try all the code snippets and examples provided in this book.

Who this book is for

This book is intended for all Apex developers who want to learn and enhance their Apex code designing skills by applying various design patterns. It can be considered as a stepping stone for beginners or intermediate-level Apex programmers by filling their arsenal with powerful design patterns.

Conventions

In this book, you will find a number of text styles 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 AdminGoldSupportImpl and AdminPlatinumSupportImpl classes are written in the same way."

A block of code is set as follows:

public class Mario {
    public void ability(){
        System.debug('I can Walk');
    }
    public void info(){
        System.debug('I am Mario');
    }
}

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

$ mkdir css

$ cd css

New terms and important words are shown in bold. 

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

For this book we have outlined the shortcuts for the Mac OX platform if you are using the Windows version you can find the relevant shortcuts on the WebStorm help page https://www.jetbrains.com/webstorm/help/keyboard-shortcuts-by-category.html.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in 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 at 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 this book 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

Downloading the color images of this book 

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ApexDesignPatterns_ColorImages.pdf.

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 could 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 to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted 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

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.