Code reuse with service layers, inheritance and modules
Where duplicated logic should live and which reuse mechanism fits: a bulkified, stateless service layer and selector classes in Apex, interfaces versus virtual and abstract inheritance, System.Callable across packages, LWC API module components, Visualforce templates and trigger-framework base classes.
Identify scenarios where code reuse is applicable and how the reuse should be implemented.
~8 min read · 11 practice questions ·
What the exam expects
- Recognize duplicated logic and move it to a service layer that LWC controllers, Queueable and Batch jobs, Apex REST resources and invocable actions all call.
- Design service and selector methods that are bulkified, stateless, task-named and security-aware.
- Choose an interface, a virtual class or an abstract class, and fix code that won't compile because the parent isn't extensible.
- Call code in another package without a compile-time dependency using
System.Callable. - Share client code between Lightning web components with an API module component, and share Visualforce layout with
apex:composition.
Key terms: service layer (classes that encapsulate business tasks for many callers), selector (a class that centralizes queries, typically around one object), polymorphism (one call behaving differently depending on the object's class), template method pattern (a base class owns the flow and subclasses override hooks), API module component (an LWC folder whose main JavaScript file exports shared code).
Key facts
- Detail
- Extend one class; implement many interfaces
override needs- Detail
- A
virtual(or abstract) parent class
- Detail
call(String action, Map<String, Object> args)returns Object
- Detail
- General business terms, not a specific caller
- Detail
- Stateless; DML encapsulated in the service method
- Detail
with sharing; elevate briefly when required
- Detail
- Predictable minimum field set and one place for criteria
- Detail
- Static, public or global, outer class, one per class, at most one input
- Detail
c/folderNamemain file only; re-export supplemental files
apex:include- Detail
- Inserts an entire page; content can't vary
Sources for this section (8)
- Extending a Classdeveloper.salesforce.com (opens in a new tab)
- Defining Apex Classesdeveloper.salesforce.com (opens in a new tab)
- Callable Interfacedeveloper.salesforce.com (opens in a new tab)
- Learn Service Layer Principlestrailhead.salesforce.com (opens in a new tab)
- Learn Selector Layer Principlestrailhead.salesforce.com (opens in a new tab)
- InvocableMethod Annotationdeveloper.salesforce.com (opens in a new tab)
- Share JavaScript Codedeveloper.salesforce.com (opens in a new tab)
- Templating with Visualforcedeveloper.salesforce.com (opens in a new tab)
- Why should an Apex service class be stateless?
- When do you choose an interface over a base class?
- Why does `class CsvExporter extends BaseExporter` fail when `BaseExporter` has no modifier?
- Which file of an LWC library can other components import?
- When is `apex:composition` better than `apex:include`?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (11 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams