[英]How to refactor a “library of functions” class that is tightly coupled with the business objects?
我有一個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
調用者將提供自己的數據並接收結果。 從而Calc
將成為供其他人使用的靜態函數庫。 但這是最好的方法嗎?
我該如何重構以最大程度地減少耦合? 請記住,要保留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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.