{"id":1756,"date":"2026-10-03T09:00:00","date_gmt":"2026-10-03T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/"},"modified":"2026-11-15T11:31:31","modified_gmt":"2026-11-15T06:01:31","slug":"apex-design-patterns","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/","title":{"rendered":"Apex Design Patterns: Singleton, Strategy, Factory and More"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Design Patterns are proven ways to organise Apex code so it stays easy to change, test and reuse as your org grows. A pattern is a named solution to a common problem, such as &#8220;only build this object once&#8221; or &#8220;pick a different rule for each customer type&#8221;. Once your team shares these names, code reviews and designs become much faster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn the Singleton, Strategy, Factory, Facade and Decorator patterns with real Apex code. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-classes-and-objects-constructors\/\">Apex classes and objects<\/a>, <a href=\"https:\/\/swapnilzambare.com\/blog\/static-vs-instance-apex\/\">static vs instance Apex<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-trigger-handler-pattern\/\">the trigger handler pattern<\/a>. This is the first post of Level 10: Advanced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Apex Design Patterns?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Design Patterns are general object-oriented patterns adapted to Salesforce limits and transactions. Each one describes the problem, the classes involved and how they work together. You still write normal Apex, but in a shape other developers recognise at once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of building with Lego instruction books. Every model is different, yet the same small techniques repeat: a strong base, a hinge, a sliding door. Once you know those techniques by name, you build faster and other builders understand your model.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Design Patterns Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without structure, Apex grows into long classes full of if statements. Here is why Apex Design Patterns matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, discount rules differ for students, schools and corporate clients.<\/li>\n\n<li>Also, the same settings record is read many times in one transaction.<\/li>\n\n<li>Next, new payment providers arrive every year, so code must change easily.<\/li>\n\n<li>Tests are simpler when each class has one clear job.<\/li>\n\n<li>Finally, interviewers often ask candidates to name and explain patterns.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So patterns keep growing code clean, and they also help in interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Design Patterns Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using Apex Design Patterns follows a simple habit. First, spot the problem, such as repeated if-else blocks or repeated queries. Next, match it to a known pattern, such as Strategy for changing rules. Then define an interface or a small class for each role in the pattern. After that, move the logic into those classes and connect them, often through a factory. Finally, write tests for each class on its own.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-1-1024x640.png\" alt=\"Apex Design Patterns diagram showing spot, match, define, connect and test\" class=\"wp-image-1754\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How to bring a design pattern into your code.<\/figcaption><\/figure>\n\n\n<h3 class=\"wp-block-heading\">Singleton<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Singleton makes sure only one instance of a class exists per transaction. So it suits settings or caches that should be loaded once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Strategy<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Strategy puts each version of a rule in its own class behind one interface. As a result, you add a new rule by adding a class, not by editing old code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Factory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Factory decides which class to create, so callers never use new directly. Also, it can read the class name from custom metadata or Type.forName.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Facade<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Facade offers one simple method that hides several steps. For example, enrolStudent() can check seats, create records and send an email.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Decorator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Decorator wraps an object to add behaviour, such as display fields for a UI. In Apex, wrapper classes around sObjects often follow this idea.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Design Patterns at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below sums up each pattern and when it helps.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Pattern<\/th><th>Problem it solves<\/th><th>Bright Path example<\/th><\/tr><\/thead><tbody><tr><td>Singleton<\/td><td>Load something once<\/td><td>Course settings<\/td><\/tr><tr><td>Strategy<\/td><td>Rules that vary<\/td><td>Discount per customer type<\/td><\/tr><tr><td>Factory<\/td><td>Choose the class at runtime<\/td><td>Pick the discount strategy<\/td><\/tr><tr><td>Facade<\/td><td>Many steps, one call<\/td><td>Enrol a student<\/td><\/tr><tr><td>Decorator<\/td><td>Add behaviour around data<\/td><td>Course wrapper for LWC<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Strategy Pattern vs If-Else Logic<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares a Strategy design with a long if-else block.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Strategy pattern<\/th><th>If-else block<\/th><\/tr><\/thead><tbody><tr><td>Adding a rule<\/td><td>New class<\/td><td>Edit existing method<\/td><\/tr><tr><td>Risk to old rules<\/td><td>Low<\/td><td>High<\/td><\/tr><tr><td>Testing<\/td><td>One class at a time<\/td><td>Whole method each time<\/td><\/tr><tr><td>Readability<\/td><td>Small, focused classes<\/td><td>One long method<\/td><\/tr><tr><td>Best for<\/td><td>Rules that keep growing<\/td><td>Two or three fixed cases<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is practical. Keep simple if-else for two or three stable cases. Switch to Strategy once rules grow or change often.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-2-1024x640.png\" alt=\"Apex Design Patterns diagram comparing the Strategy pattern with if-else logic\" class=\"wp-image-1755\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Strategy pattern vs if-else logic.<\/figcaption><\/figure>\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Design Patterns Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s apply Apex Design Patterns to course discounts at Bright Path Tutors. We combine Strategy, Factory and Singleton in one small feature.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Strategy Interface and Classes<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface DiscountStrategy {\n    Decimal apply(Decimal fee);\n}\n\npublic class StudentDiscount implements DiscountStrategy {\n    public Decimal apply(Decimal fee) { return fee * 0.90; }   \/\/ 10% off\n}\n\npublic class CorporateDiscount implements DiscountStrategy {\n    public Decimal apply(Decimal fee) { return fee * 0.80; }   \/\/ 20% off\n}\n\npublic class NoDiscount implements DiscountStrategy {\n    public Decimal apply(Decimal fee) { return fee; }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each class holds one rule behind the same interface. So adding a &#8220;School&#8221; rule later means one new class and no edits elsewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: A Singleton Factory<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DiscountFactory {\n    private static DiscountFactory instance;\n    private Map&lt;String, DiscountStrategy&gt; strategies;\n\n    private DiscountFactory() {\n        strategies = new Map&lt;String, DiscountStrategy&gt;{\n            'Student'   =&gt; new StudentDiscount(),\n            'Corporate' =&gt; new CorporateDiscount()\n        };\n    }\n\n    public static DiscountFactory getInstance() {\n        if (instance == null) { instance = new DiscountFactory(); }\n        return instance;\n    }\n\n    public DiscountStrategy forType(String customerType) {\n        DiscountStrategy s = strategies.get(customerType);\n        return s != null ? s : new NoDiscount();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The private constructor stops other code from calling new, so getInstance() returns the same factory all transaction. Then forType() picks the right strategy and falls back to NoDiscount.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use It and See the Result<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Decimal fee = 5000;\nfor (String t : new List&lt;String&gt;{ 'Student', 'Corporate', 'Guest' }) {\n    Decimal price = DiscountFactory.getInstance().forType(t).apply(fee);\n    System.debug(t + ' pays ' + price);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the debug output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student pays 4500.00\nCorporate pays 4000.00\nGuest pays 5000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The caller never knows which class did the work. Because of this, the discount logic can grow without touching this loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Design Patterns and Governor Limits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Patterns must still respect limits. So never query inside a strategy that runs in a loop, and load data once, often through a Singleton. Static variables live for one transaction only, so a Singleton is not a cross-user cache.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Design Patterns<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adding patterns to simple code that does not need them.<\/li>\n\n<li>Treating a static Singleton as if it lasts across transactions.<\/li>\n\n<li>Putting SOQL inside strategy methods called in loops.<\/li>\n\n<li>Creating classes directly instead of through the factory.<\/li>\n\n<li>Naming classes after patterns instead of business meaning.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Design Patterns<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start simple, then refactor into a pattern when the need is clear.<\/li>\n\n<li>Program to interfaces, so classes can be swapped in tests.<\/li>\n\n<li>Keep each class small, with one job.<\/li>\n\n<li>Store mapping data in custom metadata when admins need to change it.<\/li>\n\n<li>Document the pattern in a class comment for new team members.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add a School discount of 15% and make the factory read class names from a custom metadata type. Hint: use Type.forName(className).newInstance() and cast to DiscountStrategy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on Apex Design Patterns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pattern questions show up in nearly every senior developer interview. Here are the 21 questions asked most about Apex Design Patterns.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Apex Design Patterns<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is a design pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a proven, named solution to a common design problem, so teams share one vocabulary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What does the Singleton pattern do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It keeps one instance of a class per transaction, so data loads only once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: How do you build a Singleton in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use a private constructor, a private static instance and a public static getInstance method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What is the Strategy pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It puts each version of a rule in its own class behind one interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: What is a Factory?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a class that decides which object to create, so callers avoid new.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What is a Facade?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is one simple method that hides several steps behind it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What is a wrapper class in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a class that combines an sObject with extra fields, which follows the Decorator idea.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on Apex Design Patterns<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: How long does an Apex Singleton live?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It lives for one transaction, because static variables reset after each one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How do you add a new rule with Strategy?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new class that implements the interface, then register it in the factory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How can a factory create a class from a name?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call Type.forName(name).newInstance() and cast it to the interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Which pattern fits &#8220;different tax rules per country&#8221;?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Strategy, so each country&#8217;s rule lives in its own class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do interfaces help testing?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can pass a fake class into the code, so tests control the behaviour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Where should SOQL live in pattern-based code?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In selector or service classes, so strategies stay free of queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Is the trigger handler a design pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, because it separates trigger events from business logic in a repeatable shape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on Apex Design Patterns<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is dependency injection in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Passing objects into a class instead of creating them inside, so you can swap them easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How do custom metadata and Strategy work together?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Custom metadata stores which class to use, so admins can change rules without code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: When is a pattern over-engineering?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the code is small and stable, because extra classes only add reading time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: How do patterns help with governor limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They centralise queries and DML, so you can bulkify in one place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: What is the Unit of Work pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It collects all DML for a transaction and commits it once in the right order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: How do you explain a pattern in an interview?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">State the problem, the classes involved, a real example and one trade-off.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: Which patterns appear in Apex Enterprise Patterns?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Service, Domain, Selector and Unit of Work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/apex-dev\/references\/apex-dev\/apex-classes.html\" target=\"_blank\" rel=\"noopener noreferrer\">Classes, Objects, and Interfaces (Apex Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/apex-dev\/references\/apex-dev\/apex-classes-interfaces.html\" target=\"_blank\" rel=\"noopener noreferrer\">Interfaces (Apex Developer Guide)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Design Patterns give names to proven code shapes.<\/li>\n\n<li>Singleton loads once per transaction.<\/li>\n\n<li>Strategy and Factory make changing rules easy.<\/li>\n\n<li>Facade hides many steps, and Decorator adds behaviour.<\/li>\n\n<li>Use patterns when code grows, not before.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Patterns scale up into full layers for large apps. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-enterprise-patterns-service-selector\/\">Apex Enterprise Patterns<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Design Patterns are proven ways to organise Apex code so it stays easy to change, test and reuse as your org grows. A&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1753,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96,244],"tags":[68,98],"swapz_series":[],"class_list":["post-1756","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-security-design-patterns","tag-advanced","tag-apex"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Design Patterns Explained with Examples<\/title>\n<meta name=\"description\" content=\"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Design Patterns Explained with Examples\" \/>\n<meta property=\"og:description\" content=\"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-03T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:01:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Apex Design Patterns: Singleton, Strategy, Factory and More\",\"datePublished\":\"2026-10-03T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:01:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/\"},\"wordCount\":1529,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-design-patterns-featured.png\",\"keywords\":[\"Advanced\",\"Apex\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Security and Design Patterns\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/\",\"name\":\"Apex Design Patterns Explained with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-design-patterns-featured.png\",\"datePublished\":\"2026-10-03T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:01:31+00:00\",\"description\":\"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-design-patterns-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-design-patterns-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Design Patterns featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-design-patterns\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Design Patterns: Singleton, Strategy, Factory and More\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Design Patterns Explained with Examples","description":"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/","og_locale":"en_US","og_type":"article","og_title":"Apex Design Patterns Explained with Examples","og_description":"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-03T03:30:00+00:00","article_modified_time":"2026-11-15T06:01:31+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Apex Design Patterns: Singleton, Strategy, Factory and More","datePublished":"2026-10-03T03:30:00+00:00","dateModified":"2026-11-15T06:01:31+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/"},"wordCount":1529,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png","keywords":["Advanced","Apex"],"articleSection":["Salesforce","Salesforce Developer","Security and Design Patterns"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/","name":"Apex Design Patterns Explained with Examples","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png","datePublished":"2026-10-03T03:30:00+00:00","dateModified":"2026-11-15T06:01:31+00:00","description":"Apex Design Patterns explained simply: Singleton, Strategy, Factory, Facade and Decorator with real Apex code, when to use each and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-design-patterns-featured.png","width":2400,"height":1260,"caption":"Apex Design Patterns featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Design Patterns: Singleton, Strategy, Factory and More"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1756"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":2339,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1756\/revisions\/2339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1753"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1756"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}