About Me

My photo
PLANO, Texas, United States
Showing posts with label #Salesforce_Design-patterns. Show all posts
Showing posts with label #Salesforce_Design-patterns. Show all posts

Saturday, January 9, 2016

Apex Design Patterns

A design pattern is a general reusable solution to a commonly occurring problem within a given context. With the help of design, we can avoid much error well in advance. Design Patter ensures the best practices of coding.

The following are a list of design patterns in Apex:
  1. Singleton
  2. Strategy
  3. Decorator 
  4. Facade
  5. Composite
  6. Bulk State Transition 

Singleton

  • Repeated execution of a class within a single transaction may lead to exceed the governor limit. This pattern is to reduce repeated instantiation of a class.
  • Problem: Developers often write inefficient code that can cause repeated instantiate of objects. This can result in inefficient, poorly performing code, and potentially the breaching of governor limits. This most commonly occurs in triggers, as they can operate against a set of records.
  • Implementation: In order to implement a Singleton pattern in apex, the class must instantiate only once. It is implemented by:
    • Creating a class with a method that creates a new instance of the class if it doesn't already exist.
    • If it already exists, then simply return a reference to the object.
    • For example, use a global static variable, before assigning it to value check if it has already a value.

Strategy:

  • The Strategy pattern (aka the policy pattern) attempts to solve the issue where you need to provide multiple solutions for the same problem so that one can be selected at run-time.
  • Use Case: You need to provide a geographical-based search engine solution where the implementing code can choose the search provider at run-time.
  • Implementation: In Order to implement a Strategy pattern in apex, you need to define a family algorithms. It is implemented by:
    • Creating the interface class with methods that will be implemented by other classes 
    • Covers use of interfaces, Aggregation (Has A) relationship, Method Overloading.

Decorator:

  • In some scenarios, we need to show the value only in UI level, but not to store that value in Database. Here we have a Decorator Design pattern to solve this kind of problem. This pattern provides the decorator class which wraps the apex class to provide the extended functionality for the sObject
  • Use Case: Whenever the user needs to select the records and process the selected records however select checkbox is not saved.
  • Implementation: In order to implement a Decorator pattern in apex, we need to aware that this is not a true implementation the intention is to add the behavior at the run time. It is implemented by:
    • Use of wrapper classes inside wrapper class and perform required operation when it is get;set; in vf page instead when the page is loading. We can say it lazy loading. Used in VF page mostly. 

Bulk State Transition:

  • The bulk state transition requires a trigger that allows only the eligible record that has changed its state and a utility class to implement the logic. The trigger needs to be framed by its events and the DML type. 
  • Use Case: Whenever a developer needs to write the trigger we always consider as bulk and we use the Bulk State Transition Pattern.
  • Implementation: In order to implement a Bulk State Transition in apex, use proper filter to check the eligible records using the Trigger Context variable and call the utility class which contacts the method that handles the operation on the bulk operation.

Facade – 

  • The intention of the facade design pattern is to convert complex Apex classes into simplified classes or interfaces where groups of classes are either wrapped into one class or only one class is used to delegate responsibilities to the other classes.
  • Use Case: Simplifying the execution of classes with complex interfaces (e.g. web service callouts)
  • Implementation: Maintainability of Apex code by simplifying the execution of one or complex classes with a facade class. For example, just create a simple class where place code to execute methods/functions of a complex class.