Apex Design Patterns - Jitendra Zaa - E-Book

Apex Design Patterns E-Book

Jitendra Zaa

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Harness the power of Apex design patterns to build robust and scalable code architectures on the Force.com platform

About This Book

  • Apply Creational, Structural and behavioural patterns in Apex to fix governor limit issues.
  • Have a grasp of the anti patterns to be taken care in Apex which could have adverse effect on the application.
  • The authors, Jitendra Zaa is a salesforce MVP and Anshul Verma has 12+ years of experience in the area of application development.

Who This Book Is For

If you are a competent developer with working knowledge of Apex, and now want to deep dive into the world of Apex design patterns to optimize the application performance, then this book is for you. Prior knowledge of Salesforce and Force.com platform is recommended.

What You Will Learn

  • Apply OOPs principal in Apex to design a robust and efficient solution to address various facets to a business problem
  • Get to grips with the benefits and applicability of using different design patterns in Apex
  • Solve problems while instantiating, structuring and giving dynamic behavior to Apex classes
  • Understand the implementation of creational, structural, behavioral, concurrency and anti-patterns in your application
  • Follow the Apex best practices to resolve governor limit issues
  • Get clued up about the Inheritance, abstract classes, polymorphism in Apex to deal with the object mechanism
  • Master various design patterns and determine the best out of them
  • Explore the anti patterns that could not be applied to Apex and their appropriate solutions

In Detail

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

Style and approach

This book is a step-by-step guide, complete with well-tested programs and real world situations to solve your common occurring problems in Apex design by using the anti-patterns. It gets crackling from exploring every appropriate solution to comparing the best one as per OOps principal.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 241

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Apex Design Patterns
Credits
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book 
Errata
Piracy
Questions
1. An Introduction to Apex Design Pattern
Innovation
Design patterns and learning
Object-Oriented Programming (OOP)
OOPs in play
Static and dynamic dispatch
An interface
An abstract class
Advantages of design patterns
Design patterns and Apex
Gang of Four (GoF)
The SOLID principle
The single responsibility principle (SRP)
The open closed principle (OCP)
The Liskov substitution principle (LSP)
The interface segregation principle (ISP)
The dependency inversion principle (DIP)
Summary
2. Creational Patterns
Factory method pattern
Reflection in Apex
Abstract factory pattern
A new perspective on the abstract factory pattern
The singleton pattern
The builder pattern
The difference between the abstract factory method and the builder pattern
The prototype pattern
The shallow and deep clone
Summary
3. Structural Patterns
The facade pattern
Other use cases
Points to consider
The adapter pattern
Points to consider
The bridge pattern
Points to consider
The composite pattern
Other use cases
Points to consider
The decorator pattern
Other use cases
Points to consider
The flyweight pattern
Other use cases
Points to consider
Summary
4. Behavioral Patterns
The chain of responsibility pattern
Points to consider
The command pattern
Some points to note
The interpreter pattern
Points to consider
The iterator pattern
Points to consider
The visitor pattern
Single dispatch
Double-dispatch
Guidelines to implementing the visitor pattern
Points to consider
The memento pattern
Points to consider
The observer pattern
Point to consider
The state pattern 
Points to consider
The strategy pattern
Points to consider
Summary
5. Handling Concurrency in Apex
Understanding concurrency issues
Reproducing concurrency problems in Apex
The active object pattern
Points to consider
Summary
6. Anti-patterns and Best Practices
Over usage of formula fields
The deterministic formula field
The nondeterministic formula field
Functional decomposition
Ignoring the equals() and hashcode() methods while performing object comparison
Circular dependencies
Circular dependency in Apex classes
Circular dependency in triggers
Other examples
The ignoring toString() method
Avoid the God class
Error hiding
Hard-coding
Magic strings and numbers
Inheritance hell
SOQL inside a loop
DML inside a loop
Summary

Apex Design Patterns

Apex Design Patterns

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: April 2016

Production reference: 1220416

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78217-365-6

www.packtpub.com

Credits

Authors

Jitendra Zaa

Anshul Verma

Copy Editor

Rashmi Sawant

Reviewer

John M. Daniel

Project Coordinator

 Nidhi Joshi

Commissioning Editor

Julian Ursell

Proofreader

Safis Editing

Acquisition Editor

Nadeem Bagban

Indexer

Priya Sane

Content Development Editor

Aishwarya Pandere

Production Coordinator

Shantanu N. Zagade

Technical Editor

Madhunikita Sunil Chindarkar

Cover Work

Shantanu N. Zagade

About the Authors

Anshul Verma has been working on the Salesforce platform since 2006. Prior to that, he has done extensive development using MS technologies on web, desktop, and mobile applications. He possesses a tremendous understanding of enterprise-scale systems and has worked in designing intricate systems with high scalability, performance, and robustness. He has been a Dreamforce speaker and is a regular contributor to Stack Exchange and other developer communities. He has four Salesforce certifications and is currently working as a project manager and technical architect where he is responsible for managing customer success and delivering high-quality solutions to his clients. He has conducted various training sessions in his current organization and trained over 50 new hires. He is very popular with his training batches and can be often found sharing his knowledge with his team and peers. He owns and  maintains his blog (http://mightycoder.blogspot.com/), and you can follow him on Twitter at @toanshulverma.

Jitendra Zaa has been working on the Salesforce platform since 2008. He has extensively worked on Java and .NET-based enterprise applications. He also has experience in working with multiple JavaScript libraries, web frameworks, ETL tools, and databases. He is an expert in designing and implementing integrations of Salesforce with external systems. He is a regular speaker at the world’s biggest developer event, Dreamforce, mostly in developer track. Because of his contributions to the Salesforce community, he has also been awarded the Salesforce MVP title.  He has more than eight Salesforce certifications and works as a Salesforce technical architect. He owns one of the most viewed Salesforce developer blogs  (http://www.JitendraZaa.com), formerly, http://Shivasoft.in. You can follow him on Twitter at @JitendraZaa.

About the Reviewer

John M. Daniel has been working in the technology sector for over 20+ years.  During that time, he has worked with a variety of technologies and project roles. Currently, he works at FinancialForce.com with their Consulting/Product Services teams. He currently holds multiple certifications from Salesforce.com, including the Platform Developer I and II certifications and the Advanced Developer certification. He is currently in the process of attaining the Certified Technical Architect certification. He loves to spend time with his family, swim at the beach, and work on various open source projects, such as ApexDocs and ApexUML.  He co-leads his local area Salesforce Developers User Group and can be found on Twitter at @ImJohnMDaniel.

John has been a technical reviewer for Force.com Enterprise Architecture, by Andrew Fawcett and Learning Apex Programming, by Matt Kaufman and Michael Wicherski, both by Packt Publishing.

I would like to thank my wife, Allison, for always giving me the freedom to pursue my interests.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free access for Packt account holders

Get notified! Find out when new books are published by following @PacktEnterprise on Twitter or the Packt Enterprise Facebook page.

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 <[email protected]>, 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:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.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 WindowsZipeg / iZip / UnRarX for Mac7-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 <[email protected]> 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 <[email protected]>, and we will do our best to address the problem.

Chapter 1. An Introduction to Apex Design Pattern

We assume that you are reading this book because you have encountered situations where you have had the same design requirement multiple times, or seen a repetitive problem that has always been solved with the same solution. Those design solutions that are applied again and again to the same type of problem are also known as design patterns.

To better understand the application of design patterns in everyday life, let's take a look at the example of the ATM machine. Every ATM machine needs to have a slot where an ATM card can be inserted, there is a keypad to enter a secret pin, and there is a panel from where the cash can be dispensed. This core functionality of an ATM machine can be considered as a design pattern. Some banks need extra functionalities, such as bill pay or mobile recharge, and they can simply extend this design pattern as per their requirements.

No one would like to reinvent the wheel if an industry-proven solution already exists for a problem, and using such a proven solution would save them time and effort. Furthermore, it also ensures a scalable, robust, and future-ready solution.

In this book, we will discuss the challenges in application development using Apex, common repetitive problems, and most accepted solutions. We have structured our content considering the Apex development platform and blended it with day-to-day challenges that we face during development.

We will first explain the common concepts of application development, which are the building blocks for design patterns. Understanding these concepts is very important as all the design patterns are combinations of one or more principles explained in a later section.

Innovation

It is not necessary that design patterns should fulfill all requirements so why can't we innovate our own better solution?

Yes, innovation is necessary; and certainly, we can come up with a better design and solution or even a new design pattern. Let's take the preceding example; instead of entering a secret pin, a user can use voice recognition for authentication purposes'. It may look cool to some people, but for many it may be a security concern. Speech recognition may not work because of different accents and so on. My point here is that innovation comes at a cost. In software industry, we don't always have the privilege of time because of project timelines and other dependencies.

Design patterns are age-tested and recommended techniques to address a given problem. They not only help in solving the problem at hand, but also address various possible offshoots of the central problem. Design patterns have also evolved as per requirements and will continue to do so.

Design patterns and learning

We are not suggesting that you have to use only design patterns. Every problem is unique and a design pattern can solve only some part of the problem. However, you need to think, design, and come up with your own version of an extended design. Design patterns are very generic and basic; they mostly guide you through the flow of the creation of an object or manipulate the behavior at runtime or structure your code. A design pattern is not a finished code solution, but  it is a recommended code structure. Choosing the right design pattern for a given problem is very important and needs thorough understanding. With increasing use of design patterns, you can further enhance your development skills and understand how to best structure your code.

Object-Oriented Programming (OOP)

Any language that supports the following four pillars of Object-Oriented Programming is known as an Object-Oriented Programming (OOP) language:

Inheritance: This is the ability to extend an existing class to add a new functionalityPolymorphism: This is the ability to perform different actions by calling the same methodAbstraction: This is the ability to hide complex implementationEncapsulation: This is the ability to bind data attributes and behavior together