lundi 29 juin 2015

OOP - Where does the responsibility go

I have a class named as A and another Class named as B. Class A contains Class B as a property.

I have a business logic. Based on the value of a property in Class B, I have to calculate the value of another property in Class A. To do this calculation, I have to call a SOAP service, get a value and then, based on the value of a property in Class B, i have to do a mathematical calculation on the returned value from the SOAP service and then set the value for property in Class A.

public Class A{
  public string Property1{get;set;}
  public int Property2{get;set;}
  public B Property3{get;set;}
}

public class B{
 public string Property1{get;set;}
 public string Property2{get;set;}
}

The logic in pseudocode

  1. If classB.property1 = "Something1" then call soap service,get some integer value, perfome some arithmatical calculation using a constant, and assign the result to classA.Property2
  2. Else If classB.property1 = "Something2" then call soap service,get some integer value, and assign the result to classA.Property2
  3. Else then set classA.Property2 to a default value.

I dont like the If else, it doesnt follow the OCP. I was thinking of doing this with a decorator. The second option is to use a builder pattern and encapsulate the if else logic, but still it would break the OCP. Or a completely new Domain service that would perform this?But still the OCP is being broken if If else even if i chose to go the Domain service way.

The above logic is being performed in a business logic class.

So where does the responsibility for above logic of calculation go? Also how to avoid that if else?

Aucun commentaire:

Enregistrer un commentaire