Common Apex Design Patterns

Goal of article is to summarize and keep reminding the theory of apex design patterns here: However, we use all of them, but we easily forget what is called what and sometimes to even use it.

Here are a list of design patterns, some of which are standard object-oriented patterns in a context, and some of which are specific patterns.

  • Singleton – minimizing object instantiation for improved performance and to mitigate impact of governor limits.

Example, use a global static variable, before assigning it to a value check if it has already a value.

  • Strategy – defining a family of algorithms, enscapsulating each one and making them interchangeable and selectable at runtime

Covers use of interfaces, Aggregation (Has A) relationship, Method Overloading.

  • Decorator – extending the functionality of an sObject in Apex

Use of wrapper classes inside wrapper class and perform required operation when it is get;set; in vf page instead when page is loading. We can say it lazy loading. Used in VF page mostly.

  • Facade – simplifying the execution of classes with complex interfaces (e.g. web service callouts)

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.

  • Composite – The Composite design pattern can be used to represent an expression in Apex regardless of expression complexity, whilst mitigating the impact of governor limits that can result from recursions.

Example1: 1 OR (2 AND 3)

Expression expr = new OrComposite();

expr.add(new Variable(‘1’));

Expression expr2 = new AndComposite();


expr2.add(new Variable(‘2’));

expr2.add(new Variable(‘3’));

Example 2: 1 OR (2 AND 3)

Expression expr = (new OrComposite())
    .add(new Variable('1'))
    .add((new AndComposite())
        .add(new Variable('2'))
        .add(new Variable('3'))




  • Bulk State Transition – efficiently tracking the change of a field value in a trigger and executing functionality based on this change

Apex trigger best practices – Use controller class & helper classes; pass bulk list in method; perform operation on changes and on bulk record.

9 thoughts on “Common Apex Design Patterns”

  1. obviously like your web site however you have to check the spelling on quite a few of your posts.
    A number of them are rife with spelling issues and I find
    it very bothersome to inform the truth on the
    other hand I’ll surely come back again.

  2. I have been exploring for a bit for any high quality articles or weblog
    posts on this sort of space . Exploring in Yahoo I at last stumbled upon this site.
    Studying this info So i am happy to show that I have a very good uncanny feeling I discovered exactly what I needed.
    I most indisputably will make sure to don?t disregard this site and give it a look on a constant basis.
    ps4 games allenferguson ps4 games

  3. Hiya! I know this is kinda off topic however I’d figured
    I’d ask. Would you be interested in trading links or maybe guest writing a
    blog post or vice-versa? My website covers a lot of the same subjects as yours and I think we could greatly benefit from each other.
    If you’re interested feel free to send me an email.

    I look forward to hearing from you! Superb blog
    by the way!

  4. Fantastic blog! Do you have any suggestions for aspiring writers?

    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go
    for a paid option? There are so many options out there that I’m completely overwhelmed ..
    Any suggestions? Thanks a lot!

  5. Greetings! I know this is kinda off topic but I was wondering if you knew
    where I could find a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot! asmr 0mniartist

  6. With havin so much content and articles do you ever run into any
    problems of plagorism or copyright infringement?
    My website has a lot of completely unique content I’ve either authored myself or outsourced but it looks
    like a lot of it is popping it up all over the web
    without my agreement. Do you know any techniques to help stop
    content from being stolen? I’d certainly appreciate it.
    asmr 0mniartist

Comments are closed.