I'm trying to figure out the best pattern for the code below (example: not actual code). IOpportunitiesA and IOpportunitiesB are extending the base interface and they have a similar method, createContract(Opportunity opp) that initially would have the same exact functionality. This method is not enforced by the base, IOpportunities as it's not needed on all types.
I'm trying to avoid a lot of duplicate code. It's possible I would have several scenarios as below.
I'm leaning on using a helper class with methods (eg. createContract(Opportunity opp)) that can be called with default functionality from OpportunitiesA and OpportunitiesB.
I'm wondering if there an other pattern that is more robust or is a helper class the way to go.
Thanks in advance.
public interface IOpportunities {
void calculateDiscount();
}
public interface IOpportunitiesA extends IOpportunities {
void methodOpportunitiesA();
// A and B will do this
void createContract(Opportunity opp);
}
public interface IOpportunitiesB extends IOpportunities {
void methodOpportunitiesB();
// A and B will do this
void createContract(Opportunity opp);
}
public interface IOpportunitiesC extends IOpportunities {
void methodOpportunitiesC();
// C doesn't care about his method, createContract(Opportunity opp);
}
public OpportunitiesA implements IOpportunitiesA {
public void calculateDiscount() {}
public void methodOpportunitiesA() {}
// Same exact initial functionality shared between A and B
public void createContract(Opportunity opp) {
Contract contract = new Contract(
AccountId = opp.AccountId;
);
insert contract;
}
}
public OpportunitiesB implements IOpportunitiesB {
public void calculateDiscount() {}
public void methodOpportunitiesB() {}
// Same exact initial functionality shared between A and B
public void createContract(Opportunity opp) {
Contract contract = new Contract(
AccountId = opp.AccountId;
);
insert contract;
}
}
OpportunitiesA
andOpportunitiesB
could extend aOpportunitiesBase
class that has the common implementation ofcreateContract
- no helper class required