我有一个Calc
类,我认为它被错误地放置在我正在使用的代码库中。有关类结构,请参阅下面的代码。
目前
Spec
类充当数据的存储,类似于C结构Calc
类是计算函数类的库,当这些类需要进行一些计算时,它会作为其他类的一部分实例化。Calc
类还包含Spec
类,因此它可以使用Spec
的变量进行计算Plot
类是表示图的业务对象的示例Controller
类是一个业务对象,表示我的应用程序中的某种控制器,其中进行了一些计算并以文本形式显示在用户页面"View"中,还创建了一个图形,该图形也在"View"页面上显示给用户
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 for local
$this->calc->spec->a = 5;
$this->calc->spec->b = 5;
$this->calc->calcParameter();
print "total is {$this->calc->spec->c}<br>n";
//later this format is used by JS to display computational results
$plotJSON = json_encode($this->calc);
//calc for plot
$this->plot->calc->spec->a = 7;
$this->plot->calc->spec->b = 143;
$this->plot->calcPlot();
print "total is {$this->plot->calc->spec->c}<br>n";
//later this format is used by JS to display a 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 are ' + plot.calc.spec.c + ' plot points<br>');
</script>
";
}
}
//runs the above
(new Controller())->doBusinessLogic();
JS是这样使用的:
var plot = JSON.parse(document.getElementById('plot').innerHTML);
document.write('JS says - there are ' + plot.calc.spec.c + ' plot points<br>');
问题
我的印象是Calc
类的位置不正确(设计不正确(,因此,它被注入到不同的地方只是为了进行计算。我认为Calc
应该是一个没有参数的类,而Spec
不应该是Calc
的一部分。并且Calc
必须只包含进行计算的函数,Calc
的调用方将提供自己的数据并接收结果。从而CCD_ 15将成为供其他人使用的函数的静态库。但这是最好的方式吗?
如何重构以最小化耦合?请记住,JSON格式要么要保留,要么要注意代码中的任何更改都可能需要更改JS代码。
在我深入重构之前,我现在的设计工作。是否需要进行重构?只是检查一下。
好吧,根据我目前看到的情况,Calc类应该根本不存在。calcParameter
方法应该是Spec类的成员。
Plot
的calcPlot
方法也应该是Spec类的一个成员(它完全处理当前编写的Spec字段。(不过,如果它对其Spec
对象做了其他有趣的事情,您可能仍然希望保留Plot
。
像这样:
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 for local
$this->spec->a = 5;
$this->spec->b = 5;
$this->spec->calcParameter();
print "total is {$this->spec->c}<br>n";
//later this format is used by JS to display computational results
$plotJSON = json_encode($this->spec);
//spec for plot
$this->plot->spec->a = 7;
$this->plot->spec->b = 143;
$this->plot->calcPlot();
print "total is {$this->plot->spec->c}<br>n";
//later this format is used by JS to display a 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 are ' + plot.spec.c + ' plot points<br>');
</script>
";
}
}
//runs the above
(new Controller())->doBusinessLogic();