Apex - 接口
接口就像一个 Apex 类,其中没有实现任何方法。 它只包含方法签名,但每个方法的主体是空的。 要使用接口,另一个类必须通过为接口中包含的所有方法提供主体来实现它。
接口主要用于为代码提供抽象层。 它们将实现与方法的声明分开。
让我们以我们的公司为例。 假设我们需要向高级客户和普通客户提供折扣,并且两者的折扣会有所不同。
我们将创建一个名为DiscountProcessor的接口。
// 接口
public interface DiscountProcessor {
Double percentageDiscountTobeApplied(); // method signature only
}
// 优质客户级别
public class PremiumCustomer implements DiscountProcessor {
//方法调用
public Double percentageDiscountTobeApplied () {
// 对于高级客户,折扣应为 30%
return 0.30;
}
}
// 普通客户等级
public class NormalCustomer implements DiscountProcessor {
// 方法调用
public Double percentageDiscountTobeApplied () {
// 对于普通客户,折扣应为 10%
return 0.10;
}
}
当您实现接口时,必须实现该接口的方法。 如果不实现接口方法,就会抛出错误。 当您想让开发人员强制执行方法时,您应该使用接口。
Batch Apex 的标准 Salesforce 接口
SFDC 确实有 Database.Batchable、Schedulable 等标准接口。例如,如果您实现 Database.Batchable 接口,则必须实现该接口中定义的三个方法 - Start、Execute 和 Finish。
下面是标准 Salesforce 提供的 Database.Batchable 接口的示例,该接口向用户发送带有批次状态的电子邮件。 该接口有 3 个方法,Start、Execute 和 Finish。 使用此接口,我们可以实现 Batchable 功能,并且它还提供 BatchableContext 变量,我们可以使用该变量来获取有关正在执行的 Batch 的更多信息并执行其他功能。
global class CustomerProessingBatch implements Database.Batchable<sobject7>,
Schedulable {
// 在此处添加您的电子邮件地址
global String [] email = new String[] {'test@test.com'};
// Start Method
global Database.Querylocator start (Database.BatchableContext BC) {
// 这是一个查询,它将确定记录的范围并获取相同的记录
return Database.getQueryLocator('Select id, Name, APEX_Customer_Status__c,
APEX_Customer_Decscription__c From APEX_Customer__c WHERE createdDate = today
&& APEX_Active__c = true');
}
// Execute method
global void execute (Database.BatchableContext BC, List<sobject> scope) {
List<apex_customer__c> customerList = new List<apex_customer__c>();
List<apex_customer__c> updtaedCustomerList = new List<apex_customer__c>();
for (sObject objScope: scope) {
// 从通用 sOject 到 APEX_Customer__c 的类型转换
APEX_Customer__c newObjScope = (APEX_Customer__c)objScope ;
newObjScope.APEX_Customer_Decscription__c = 'Updated Via Batch Job';
newObjScope.APEX_Customer_Status__c = 'Processed';
// Add records to the List
updtaedCustomerList.add(newObjScope);
}
// Check if List is empty or not
if (updtaedCustomerList != null && updtaedCustomerList.size()>0) {
// Update the Records
Database.update(updtaedCustomerList); System.debug('List Size
'+updtaedCustomerList.size());
}
}
// Finish Method
global void finish(Database.BatchableContext BC) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// get the job Id
AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors,
a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById,
a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()];
System.debug('$$$ Jobid is'+BC.getJobId());
// 下面的代码将向用户发送有关状态的电子邮件
mail.setToAddresses(email);
// Add here your email address
mail.setReplyTo('test@test.com');
mail.setSenderDisplayName('Apex Batch Processing Module');
mail.setSubject('Batch Processing '+a.Status);
mail.setPlainTextBody('The Batch Apex job processed
'+a.TotalJobItems+'batches with '+a.NumberOfErrors+'failures'+'Job Item
processed are'+a.JobItemsProcessed);
Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});
}
// 安排课程的 Scheduler 方法
global void execute(SchedulableContext sc) {
CustomerProessingBatch conInstance = new CustomerProessingBatch();
database.executebatch(conInstance,100);
}
}
要执行此类,您必须在开发人员控制台中运行以下代码。
CustomerProessingBatch objBatch = new CustomerProessingBatch (); Database.executeBatch(objBatch);

