JavaScript:S.O.L.I.D五大原則之開閉原則OCP

來源:互聯網
上載者:User

JavaScript:S.O.L.I.D五大原則之開閉原則OCP
前言

本章我們要講解的是S.O.L.I.D五大原則JavaScript語言實現的第2篇,開閉原則OCP(The Open/Closed Principle )。

開閉原則的描述是:

Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.軟體實體(類,模組,方法等等)應當對擴充開放,對修改關閉,即軟體實體應當在不修改的前提下擴充。

open for extension(對擴充開放)的意思是說當新需求出現的時候,可以通過擴充現有模型達到目的。而Close for modification(對修改關閉)的意思是說不允許對該實體做任何修改,說白了,就是這些需要執行多樣行為的實體應該設計成不需要修改就可以實現各種的變化,堅持開閉原則有利於用最少的代碼進行項目維護。

 

問題代碼

為了直觀地描述,我們來舉個例子示範一下,下屬代碼是動態展示question列表的代碼(沒有使用開閉原則)。

// 問題類型var AnswerType = {    Choice: 0,    Input: 1};// 問題實體function question(label, answerType, choices) {    return {        label: label,        answerType: answerType,        choices: choices // 這裡的choices是選擇性參數    };}var view = (function () {    // render一個問題    function renderQuestion(target, question) {        var questionWrapper = document.createElement('div');        questionWrapper.className = 'question';        var questionLabel = document.createElement('div');        questionLabel.className = 'question-label';        var label = document.createTextNode(question.label);        questionLabel.appendChild(label);        var answer = document.createElement('div');        answer.className = 'question-input';        // 根據不同的類型展示不同的代碼:分別是下拉式功能表和輸入框兩種        if (question.answerType === AnswerType.Choice) {            var input = document.createElement('select');            var len = question.choices.length;            for (var i = 0; i < len; i++) {                var option = document.createElement('option');                option.text = question.choices[i];                option.value = question.choices[i];                input.appendChild(option);            }        }        else if (question.answerType === AnswerType.Input) {            var input = document.createElement('input');            input.type = 'text';        }        answer.appendChild(input);        questionWrapper.appendChild(questionLabel);        questionWrapper.appendChild(answer);        target.appendChild(questionWrapper);    }    return {        // 遍曆所有的問題列表進行展示        render: function (target, questions) {            for (var i = 0; i < questions.length; i++) {                renderQuestion(target, questions[i]);            };        }    };})();var questions = [                question('Have you used tobacco products within the last 30 days?', AnswerType.Choice, ['Yes', 'No']),                question('What medications are you currently using?', AnswerType.Input)                ];var questionRegion = document.getElementById('questions');view.render(questionRegion, questions);

上面的代碼,view對象裡包含一個render方法用來展示question列表,展示的時候根據不同的question類型使用不同的展示方式,一個question包含一個label和一個問題類型以及choices的選項(如果是選擇類型的話)。如果問題類型是Choice那就根據選項生產一個下拉式功能表,如果類型是Input,那就簡單地展示input輸入框。

該代碼有一個限制,就是如果再增加一個question類型的話,那就需要再次修改renderQuestion裡的條件陳述式,這明顯違反了開閉原則。

重構代碼

讓我們來重構一下這個代碼,以便在出現新question類型的情況下允許擴充view對象的render能力,而不需要修改view對象內部的代碼。

先來建立一個通用的questionCreator函數:

function questionCreator(spec, my) {    var that = {};    my = my || {};    my.label = spec.label;    my.renderInput = function () {        throw not implemented;         // 這裡renderInput沒有實現,主要目的是讓各自問題類型的實現代碼去覆蓋整個方法    };    that.render = function (target) {        var questionWrapper = document.createElement('div');        questionWrapper.className = 'question';        var questionLabel = document.createElement('div');        questionLabel.className = 'question-label';        var label = document.createTextNode(spec.label);        questionLabel.appendChild(label);        var answer = my.renderInput();        // 該render方法是同樣的粗合理代碼        // 唯一的不同就是上面的一句my.renderInput()        // 因為不同的問題類型有不同的實現        questionWrapper.appendChild(questionLabel);        questionWrapper.appendChild(answer);        return questionWrapper;    };    return that;}

該代碼的作用組合要是render一個問題,同時提供一個未實現的renderInput方法以便其他function可以覆蓋,以使用不同的問題類型,我們繼續看一下每個問題類型的實現代碼:

function choiceQuestionCreator(spec) {    var my = {},that = questionCreator(spec, my);                // choice類型的renderInput實現    my.renderInput = function () {        var input = document.createElement('select');        var len = spec.choices.length;        for (var i = 0; i < len; i++) {            var option = document.createElement('option');            option.text = spec.choices[i];            option.value = spec.choices[i];            input.appendChild(option);        }        return input;    };    return that;}function inputQuestionCreator(spec) {    var my = {},that = questionCreator(spec, my);    // input類型的renderInput實現    my.renderInput = function () {        var input = document.createElement('input');        input.type = 'text';        return input;    };    return that;}

choiceQuestionCreator函數和inputQuestionCreator函數分別對應下拉式功能表和input輸入框的renderInput實現,通過內部調用統一的questionCreator(spec, my)然後返回that對象(同一類型哦)。

view對象的代碼就很固定了。

var view = {    render: function(target, questions) {        for (var i = 0; i < questions.length; i++) {            target.appendChild(questions[i].render());        }    }};

所以我們聲明問題的時候只需要這樣做,就OK了:

var questions = [    choiceQuestionCreator({    label: 'Have you used tobacco products within the last 30 days?',    choices: ['Yes', 'No']  }),    inputQuestionCreator({    label: 'What medications are you currently using?'  })    ];

最終的使用代碼,我們可以這樣來用:

var questionRegion = document.getElementById('questions');view.render(questionRegion, questions);

 

重構後的最終代碼

上面的代碼裡應用了一些技術點,我們來逐一看一下:

  1. 首先,questionCreator方法的建立,可以讓我們使用模板方法模式將處理問題的功能delegat給針對每個問題類型的擴充代碼renderInput上。
  2. 其次,我們用一個私人的spec屬性替換掉了前面question方法的建構函式屬性,因為我們封裝了render行為進行操作,不再需要把這些屬性暴露給外部代碼了。
  3. 第三,我們為每個問題類型建立一個對象進行各自的代碼實現,但每個實現裡都必須包含renderInput方法以便覆蓋questionCreator方法裡的renderInput代碼,這就是我們常說的策略模式。

    通過重構,我們可以去除不必要的問題類型的枚舉AnswerType,而且可以讓choices作為choiceQuestionCreator函數的必選參數(之前的版本是一個選擇性參數)。

    總結

    重構以後的版本的view對象可以很清晰地進行新的擴充了,為不同的問題類型擴充新的對象,然後聲明questions集合的時候再裡面指定類型就行了,view對象本身不再修改任何改變,從而達到了開閉原則的要求。

    另:懂C#的話,不知道看了上面的代碼後是否和多態的實現有些類似?其實上述的代碼用原型也是可以實現的,大家可以自行研究一下。

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.