# Code reuse with service layers, inheritance and modules

*Platform Developer II Study Guide · Performance · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/platform-developer-ii/performance/code-reuse-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.

- Certification: Salesforce Platform Developer II
- Exam section: Performance
- Study guide: https://www.certifyforce.com/study-guide/platform-developer-ii

**Official exam objective:**

> Identify scenarios where code reuse is applicable and how the reuse should be implemented.

## 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

| Fact | Detail | Source |
|---|---|---|
| Single inheritance | Extend one class; implement many interfaces | [Extending a Class](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm) |
| `override` needs | A `virtual` (or abstract) parent class | [Defining Apex Classes](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm) |
| Callable method | `call(String action, Map<String, Object> args)` returns Object | [Callable Interface](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_System_Callable.htm) |
| Service naming | General business terms, not a specific caller | [Learn Service Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_sl/apex_patterns_sl_learn_sl_principles) |
| Service state | Stateless; DML encapsulated in the service method | [Learn Service Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_sl/apex_patterns_sl_learn_sl_principles) |
| Service security | `with sharing`; elevate briefly when required | [Learn Service Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_sl/apex_patterns_sl_learn_sl_principles) |
| Selector benefit | Predictable minimum field set and one place for criteria | [Learn Selector Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_dsl/apex_patterns_dsl_learn_selector_l_principles) |
| Invocable method | Static, public or global, outer class, one per class, at most one input | [InvocableMethod Annotation](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm) |
| LWC library import | `c/folderName` main file only; re-export supplemental files | [Share JavaScript Code](https://developer.salesforce.com/docs/platform/lwc/guide/js-share-code.html) |
| `apex:include` | Inserts an entire page; content can't vary | [Templating with Visualforce](https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_templates_intro.htm) |

## Check yourself

- 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`?

## Sources

- [Learn Service Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_sl/apex_patterns_sl_learn_sl_principles)
- [Learn Selector Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_dsl/apex_patterns_dsl_learn_selector_l_principles)
- [Apply Domain Layer Principles](https://trailhead.salesforce.com/content/learn/modules/apex_patterns_dsl/apex_patterns_dsl_apply_dl_principles)
- [Extending a Class](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm)
- [Defining Apex Classes](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm)
- [Interfaces](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_interfaces.htm)
- [Callable Interface](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_System_Callable.htm)
- [Type Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_type.htm)
- [InvocableMethod Annotation](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm)
- [Share JavaScript Code](https://developer.salesforce.com/docs/platform/lwc/guide/js-share-code.html)
- [Templating with Visualforce](https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_templates_intro.htm)
- [Defining Templates with apex:composition](https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_templates_composition.htm)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 11 linked practice questions, and unlimited timed mock exams are included with the Platform Developer II pack (USD $19): https://www.certifyforce.com/practice-exams
