0

All objetcs (p1, p2 buyer, seller) are from the same class and initially buyer and seller objects are null. Based on the type of the document I need assign p1 and p2 to either seller or buyer.

Class EmailSenderUtil{
public void sendPendingActionEmail(Document Type, PartyType p1, PartyType p2){
  PartyType buyer = null;
  PartyType seller = null;
  /// some other computations

  else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer(p1, p2, buyer, seller);
     // set some other variable unique to this type
  }
}

private void assignInitiatorAsBuyer(PartyType p1, PartyType p2, PartyType buyer, PartyType seller) {
        buyer = p1;
        seller = p2;
    }
private void assignInitiatorAsSeller(PartyType p1, PartyType p2, PartyType buyer, PartyType seller) {
        buyer = p2;
        seller = p1;
    }
}

I can assign these variables in 2 lines but as the else if conditions are alot(10) inorder to reduce the redundancy I moved this assignment inside a method. But as Java is pass by value the assignments are not reflected to the parent method.

Would be much obliged If I could know is there any elegant way to perform this operation rather than copying the same assignment several times.

5
  • 2
    Don't pass buyer and seller as argument. Use this.buyer and this.sellerin your methods. Commented Mar 27, 2019 at 8:05
  • 1
    Where are these methods? Assigning to instance variables (this.buyer) instead of local variables is not an option? Commented Mar 27, 2019 at 8:06
  • 2
    Java passes by value. There is no way to directly pass by reference. You would need to wrap your variables in a class, pass that class and set the values inside of the wrapper. Commented Mar 27, 2019 at 8:06
  • 1
    Are any of the four objects class variables in the class where you want to add the methods? Commented Mar 27, 2019 at 8:07
  • I have updated the question. All the methods are defined inside a util class. Thanks for the answers, ill be able to use the this. approach as well. Commented Mar 27, 2019 at 8:40

2 Answers 2

2

After the edits provided in the question, i think we can now go with the approach where we can remove buyer and seller from the method, and use this.buyer=p1

Class EmailSenderUtil{
   PartyType buyer = null;
   PartyType seller = null;
    public void sendPendingActionEmail(Document Type, PartyType p1, PartyType p2){
 // .....
 // rest of the code

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
    assignInitiatorAsBuyer(p1, p2, buyer, seller);
  }
}

private void assignInitiatorAsBuyer(PartyType p1, PartyType p2) {
    this.buyer = p1;
    this.seller = p2;
}
private void assignInitiatorAsSeller(PartyType p1, PartyType p2) {
    this.buyer = p2;
    this.seller = p1;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Editing the code. i thought these 2 were instance variables, as in my edited answer
yeah.. i saw that.. that is why i changed my code.. my code is only valid if the 2 variables are instance variables, as in my code.
In the question, the methods are not static, but in case they are, I can think of an alternative approach where in we can make the function return buyer and seller (both, like as an object or a list), and they can be received in the else if block when calling the function. I will update the code for this also in some time.
I now spotted in a comment from OP that this is OK so no need to update the question anymore. Sorry for all the noice :)
0

If the methods are already present in PartyType use these:

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer();
 // set some other variable unique to this type
}

private void assignInitiatorAsBuyer() {
        this.buyer = this.p1;
        this.seller = this.p2;
}
private void assignInitiatorAsSeller() {
        this.buyer = this.p2;
        this.seller = this.p1;
}

Or if it's used in other class use like this:

else if(documentType.equals(DocumentType.REQUESTFORQUOTATION)) {
      assignInitiatorAsBuyer(partyTypeObj);
 // set some other variable unique to this type
}

private void assignInitiatorAsBuyer(PartyTypeObjects partyTypeObj) {
        partyTypeObj.buyer = partyTypeObj.p1;
        partyTypeObj.seller = partyTypeObj.p2;
}
private void assignInitiatorAsSeller(PartyTypeObjects partyTypeObj) {
        partyTypeObj.buyer = partyTypeObj.p2;
        partyTypeObj.seller = partyTypeObj.p1;
}

3 Comments

In 2nd approach, I think we have p1, p2, buyer and seller are objects of type PartyType. They are not the members of the class Partytype, so how can we write partyType.p1, partyType.seller.
Thanks!! so now I assumed that some class like partyTypeObjects holds p1, p2, buyer, seller and we can use this object to achieve the work, because according to the question: All objects (p1, p2 buyer, seller) are from the same class and initially buyer and seller objects are null.
Also, with the current changes made in the code in the question, the 1st approach will also get impacted, as it contains only buyer and seller as instance members.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.