簡體   English   中英

如何重構與業務對象緊密結合的“功能庫”類?

[英]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類的成員。

PlotcalcPlot方法也應該是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM