0

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;
    }
}
2
  • both OpportunitiesA and OpportunitiesB could extend a OpportunitiesBase class that has the common implementation of createContract - no helper class required
    – cropredy
    Jun 5, 2020 at 16:01
  • @cropredy Why not add that as an answer?
    – sfdcfox
    Jun 5, 2020 at 16:02

1 Answer 1

3

You could use a base (super) class that implements IOpportunities that includes the common method implementation

public abstract class OpportunitiesBase implements IOpportunities {


   virtual void createContract(Opportunity opp) {
     // shared logic here
   }  
   abstract calculateDiscount();  // subclasses must implement this concretely

 }

 public OpportunitiesA extends OpportunitiesBase implements IOpportunities {

    public override calculateDiscount() {
      // unique logic for discount calc to OpportunitiesA
    }
  } 

  public OpportunitiesB extends OpportunitiesBase implements IOpportunities {

    public override calculateDiscount() {
      // unique logic for discount calc to OpportunitiesB
    }
  }

And invoke the common logic:

new OpportunitesA().createContract(someOpp);
2
  • I was considering this as well. What is the downside of having OpportunitiesA implementing IOpportunitiesA (same with b) and making the OpportunitiesBase just virtual? Would that work as well?
    – Andrew L.
    Jun 5, 2020 at 16:56
  • You could do that too
    – cropredy
    Jun 5, 2020 at 19:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .