学習ノートの再構築


リード
過剰設計とは、コードの柔軟性と複雑さが必要以上であることを意味します.彼らがこのようにしたのは、今日、明日のニーズに適応するために、案をより柔軟で複雑に設計することを望んでいるからです.予想される需要配分が現実にならなければ、無駄な時間とお金になるだろう.この問題を回避するために,システムの各部分を担当する.しかし、誰もが自分の小天地で働いていて、他のコードが自分の必要な機能を完成したかどうかを見ることは少なく、最後に大量の重複コードを生成します.
モデルは万霊薬で、モデルは柔軟で精巧で、非常に優雅な対象向けの設計方法を代表している.モードの強さは、簡単なコードの作成方法を無視しやすい.短くて簡単でストレートなコードに集中すると、過剰な設計を減らすことができます.
デザインが不足しています.これは過剰なデザインよりずっとよく見られます.発生原因は以下の通りである.
  • プログラマーには時間がなく、時間が抽出されていないか、再構築が許可されていない.
  • プログラマーは良いソフトウェア設計の面で知識が不足している.
  • プログラマーは、既存のシステムに新しい機能を迅速に追加するように要求される.
  • プログラマーは、同時に多くのプロジェクトを行わなければならないと付け加えた.
  • 長期的な設計不足は、次のような結果をもたらす可能性があります.
  • システムの1.0バージョンはすぐに提供されましたが、コードの品質が悪いです.
  • システムの2.0バージョンも配信されましたが、品質の悪いコードが遅くなりました.
  • は、将来のバージョンを納品しようとすると、劣悪なコードが倍増するにつれて、開発もますます遅くなり、最後にシステム、プログラマー、さらには大学をこのような状況に陥れる過程全体に自信を失った.
  • が4.0版になったか、その後、私たちはそれがだめだと気づいて、逆転を考え始めました.
  • テスト駆動と継続的な再構築は、過剰な設計と設計不足の確率を低減するのに役立ちます.
    進化的な設計は、ソフトウェア設計に関する資料が優れたソリューションの講義に集中しているが、これらのソリューションの進化過程は重視されていない.進化によって得られた設計構造は少し役に立ちますが、設計がどのように発展したのか分かりません.プロジェクトの中で誤った応用に陥ったり、過度な設計の誤解に陥ったりする可能性があります.
    優れたソフトウェア設計の進化過程を理解することは、優れた設計自体を学ぶよりも価値があります.
    Extract Method
    Turn the fragment into a method whos name explains the purpose of the method.
    このコードを独立したメソッドに入れ,メソッド名にメソッドの用途を説明させる.
        void printOwing(double amount) {
            printBanner();
    
            // print details
            System.out.println("name" + _name);
            System.out.println("amount" + _amount);
        }
        void printOwing(double amount) {
            printBanner();
            printDetails(amount);
        }
    
        void printDetails(double amount) {
            System.out.println("name" + _name);
            System.out.println("amount" + _amount);
        }

    Inline Method
    Put the method's body into the body of its callers and remove the method.
        int getRating() {
            return (moreThanFiveLateDeliveries()) ? 2 : 1;
        }
    
        boolean moreThanFiveLateDeliveries() {
            return _numberOfLateDeliveries > 5;
        }
        int getRating() {
            return (_numberOfLateDeliveries > 5) ? 2 : 1;
        }

    InlineTemp
            double basePrice = anOrder.basePrice();
            return (basePrice > 1000);
            return (anOrder.basePrice() > 1000);

    Replace Temp with Query
        public double getPrice () {
            double basePrice = _quantity * _itemPrice;
            if (basePrice > 1000)
                return basePrice * 0.95;
            else
                return basePrice * 0.98;
        }
        public double getPrice () {
            if (basePrice() > 1000)
                return basePrice() * 0.95;
            else
                return basePrice() * 0.98;
        }
    
        double basePrice() {
            return _quantity * _itemPrice;
        }

    Introduce Explaining Variable
            if ((platform.toUpperCase().indexOf("MAC") > -1)
                    && browser.toUpperCase().indexOf("IE") > -1
                    && wasInitialized() && resize > 0) {
                // do something
            }
            final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
            final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
            final boolean wasResized = resize > 0;
            if (isMacOs && isIEBrowser && wasInitialized() && wasResized) {
                // do something
            }

    Split Temporary Variable
            double temp = 2 * (_height + _width);
            System.out.println(temp);
            temp = _height * _width;
            System.out.println(temp);
            final double perimeter = 2 * (_height + _width);
            System.out.println(perimeter);
            final double area = _height * _width;
            System.out.println(area);

    Remove Assignments to Parameters
        int discount(int inputVal, int quantity, int yearToDate) {
            if (inputVal > 50)
                inputVal -= 2;
            if (quantity > 100)
                inputVal -= 1;
            if (yearToDate > 10000)
                inputVal -= 4;
            return inputVal;
        }
        int discount(int inputVal, int quantity, int yearToDate) {
            int result = inputVal;
            if (inputVal > 50)
                result -= 2;
            if (quantity > 100)
                result -= 1;
            if (yearToDate > 10000)
                result -= 4;
            return result;
        }

    Replace Method with Method Object
    class Account {
    
        int gamma(int inputVal, int quantity, int yearToDate) {
            int importantValue1 = (inputVal * quantity) + dalta();
            int importantValue2 = (inputVal * yearToDate) + 100;
            if ((yearToDate - importantValue1) > 100)
                importantValue2 -= 20;
            int importantValue3 = importantValue2 * 7;
            // and so on
            return importantValue3 - 2 * importantValue1;
        }
    }
    class Account {
    
        int gamma(int inputVal, int quantity, int yearToDate) {
            return new Gamma(this, inputVal, quantity, yearToDate).compute();
        }
    }
    
    class Gamma {
        private Account _account;
        private int inputVal;
        private int quantity;
        private int yearToDate;
    
        public Gamma(Account _account, int inputVal, int quantity, int yearToDate) {
            this._account = _account;
            this.inputVal = inputVal;
            this.quantity = quantity;
            this.yearToDate = yearToDate;
        }
    
        int compute() {
            int importantValue1 = (inputVal * quantity) + _account.dalta();
            int importantValue2 = (inputVal * yearToDate) + 100;
            if ((yearToDate - importantValue1) > 100)
                importantValue2 -= 20;
            int importantValue3 = importantValue2 * 7;
            // and so on
            return importantValue3 - 2 * importantValue1;
        }
    }

    Substitute Algorithm
        String foundPerson(String[] people) {
            for (int i = 0; i < people.length; i++) {
                if (people[i].equals("Don")) {
                    return "Don";
                }
                if (people[i].equals("Jonh")) {
                    return "Jonh";
                }
                if (people[i].equals("Kent")) {
                    return "Kent";
                }
            }
            return "";
        }
        String foundPerson(String[] people) {
            List<String> candidates = Arrays.asList(
                    new String[] { "Don", "Jonh", "Kent" });
            for (int i = 0; i < people.length; i++) {
                if (candidates.contains(people[i]))
                    return people[i];
            }
            return "";
        }

    ……