Key Concepts:
-
Dynamic Class Instantiation:
-
Apex allows you to instantiate classes dynamically based on their names. This is particularly useful when you have to interact with different types of classes but don't want to explicitly reference each class.
-
-
Dynamic Method Invocation:
-
You can invoke methods dynamically by using
Type
andMethod
classes, and theReflection
mechanism in Apex, enabling you to call methods based on the method name at runtime.
-
-
Use of
Type.forName
:-
Apex has the
Type
class, which allows you to reference a class by its string name. UsingType.forName('Namespace.ClassName')
, you can dynamically create objects or invoke methods.
-
-
Polymorphism:
-
You can leverage polymorphism to call methods on different classes that implement the same interface, even if the exact class isn’t known until runtime.
Why Use Dynamic Class Invocation in Apex?
Dynamic class invocation provides flexibility and scalability in your code. Here are a few reasons to use it:
-
Handling Multiple Classes:
-
In scenarios where you need to process different sObjects (like
Account
,Contact
, etc.) dynamically, you can use dynamic class invocation to call a handler for each specific sObject type without explicitly writing the code for each type.
Example:
-
Instead of writing separate methods for processing
Account
,Contact
,Opportunity
, etc., you can dynamically call the respective handler classes based on the type of the incoming object.
-
-
Scalability:
-
When the business logic requires flexibility to handle many types of records (sObjects) that implement a common interface, dynamic invocation allows you to scale easily by adding new classes without changing existing code.
-
-
Reduced Code Duplication:
-
Dynamic invocations allow you to avoid duplicating code for each class. Instead of writing individual logic for each class, you can write a generic handler and invoke class methods dynamically based on the object type.
-
-
Flexibility in Test Environments:
-
It can be useful in testing and writing reusable components. For instance, if you want to dynamically test multiple classes without changing code for each, you can invoke the appropriate class or method dynamically based on the context.
How It Works:
Invoke Handler:
The
invokeHandler
method receives aList<SObject>
and iterates through it.For each
SObject
in the list, the method checks its type (sObjectType
).It then checks if a handler exists for that type in the
handlerMap
. If found, it callsprocessRecord
on the handler and passes the entire list ofSObject
s (sObjList
).
Handler Classes:
Each handler (like
AccountHandler
orContactHandler
) now processes a list ofSObject
s.Inside the
processRecord
method, we loop through the passed list and cast theSObject
to the appropriate object type (Account
orContact
).This allows the handler to work with multiple records at once.
Why the List:
By passing the entire
List<SObject>
to the handler, you enable the handler to process multiple records at once, which is often more efficient (e.g., when performing DML operations or aggregating data for a batch).
Comments
Post a Comment
Please Write your comment here.