php - How to refactor a "library of functions" class that is tightly coupled with the business objects? -


i have calc class think improperly placed in codebase working with. class structure see code below.

currently

  • spec class acts storage of data, similar c struct
  • calc class library of computational functions class instantiated part of other classes, whenever classes need computation. calc class houses spec class, can computations using variables of spec.
  • plot class example of business object representing graph.
  • controller class business object representing controller of sorts in application, computations made , displayed in text user page "view", , graph created displayed user on "view" page.
class spec {     public $a;     public $b;     public $c; }  class calc {     public $spec;      function __construct()     {         $this->spec = new spec();     }      function calcparameter()     {         $this->spec->c = $this->spec->a + $this->spec->b;     } }  class plot {     public $calc;      function __construct()     {         $this->calc = new calc();     }      function calcplot()     {         $this->calc->spec->c = $this->calc->spec->a * $this->calc->spec->b;     } }   class controller {     public $calc;     public $plot;      function __construct()     {         $this->calc = new calc();         $this->plot = new plot();     }      function dobusinesslogic()     {          //calc local         $this->calc->spec->a = 5;         $this->calc->spec->b = 5;         $this->calc->calcparameter();         print "total {$this->calc->spec->c}<br>\n";          //later format used js display computational results         $plotjson = json_encode($this->calc);            //calc plot         $this->plot->calc->spec->a = 7;         $this->plot->calc->spec->b = 143;         $this->plot->calcplot();         print "total {$this->plot->calc->spec->c}<br>\n";          //later format used js display plot         $plotjson = json_encode($this->plot);          print "         <div id='plot' style='display:none'>$plotjson</div>         <script>             var plot = json.parse(document.getelementbyid('plot').innerhtml);             document.write('js says - there ' + plot.calc.spec.c + ' plot points<br>');         </script>         ";     }   }  //runs above (new controller())->dobusinesslogic(); 

js used so:

var plot = json.parse(document.getelementbyid('plot').innerhtml); document.write('js says - there ' + plot.calc.spec.c + ' plot points<br>'); 

question

i under impression calc class not correctly placed (not designed properly), , such, injected various places computations. thinking calc should class no parameters , spec should not part of calc. , calc must contain functions computations, , callers of calc supply own data , receive results. thereby calc become static library of functions used others. best way?

how can refactor minimize coupling? keeping in mind json format either preserved, or being mindful changes in code may require changes js code well.

before go knee-deep refactoring this, design have works. there need refactor @ all? checking.

okay, based on see far, calc class should not exist @ all. calcparameter method should member of spec class.

the calcplot method of plot should member of spec class (it deals entirely spec fields written.) still might want keep plot around though if other interesting things spec object.

like this:

class spec {     public $a;     public $b;     public $c;      function calcparameter() {         $this->c = $this->a + $this->b;     }      function calcplot() {         $this->c = $this->a * $this->b;     } }  class plot {     public $spec;      function __construct() {         $this->spec = new spec();     }      function calcplot() {         $this->spec->calcplot()     } }   class controller {     public $spec;     public $plot;      function __construct() {         $this->spec = new spec();         $this->plot = new plot();     }      function dobusinesslogic() {         //spec local         $this->spec->a = 5;         $this->spec->b = 5;         $this->spec->calcparameter();         print "total {$this->spec->c}<br>\n";          //later format used js display computational results         $plotjson = json_encode($this->spec);           //spec plot         $this->plot->spec->a = 7;         $this->plot->spec->b = 143;         $this->plot->calcplot();         print "total {$this->plot->spec->c}<br>\n";          //later format used js display plot         $plotjson = json_encode($this->plot);          print "         <div id='plot' style='display:none'>$plotjson</div>         <script>             var plot = json.parse(document.getelementbyid('plot').innerhtml);             document.write('js says - there ' + plot.spec.c + ' plot points<br>');         </script>         ";     }   }  //runs above (new controller())->dobusinesslogic(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -