SCJP(OCPJP) 考古題解析 第36題(Drag&Drop)

Given:

public class Doubler {
    public static int doubleMe(Holder h) {
        return h.getAmount() * 2;
    }
}

And:

public class Holder {
    int amount = 10;
    public void doubleAmount() { amount = Doubler.doubleMe(this); }
    public int getAmount() { return amount; }
    // more code here
}

Place the code framgmets in position to reduce the coupling between Doubler and Holder.

public class Doubler {
        public static int doubleMe( Place here     int     h){
                return Place here      h      * 2;
        }
}

public class Holder {
        int amount = 10;
        public void doubleAmount(){amount = Doubler.doubleMe( Place here  amount );}
        public int getAmount() { return amount; }
        // more code here
}


Code Fragments
void Holder int Doubler
h.getAmount() h this amount



解析:


要降低耦合度

就要減少Doubler跟Holder對彼此之間的關聯



首先第一個參數部分

是參數的型態的話,也只能夠放Holder、int、Doubler

但都要降低耦合度了,所以不可能是Holder

放Doubler的話,下面那個*2前面就沒有選擇了

所以只能放int



第二個變數部分

既然第一個都放int了

Doubler Class也沒其他變數了

h就是最好的答案



第三個丟入參數部分

還能有其他選擇嗎?

就是amount了

不只從刪去法得到結果

光看Holder裡的方法名稱

就知道這裡面有要對amount做運算

除了把amount丟進去運算

還能有其他方法能讓amount有作用嗎




0 意見:

張貼留言