無題

          拿個學位是騙自己的。學問是一輩子的。

          統計

          留言簿(3)

          閱讀排行榜

          評論排行榜

          今天OO考試的一個題

          題目是要求描述算術表達式,操作符僅限于+,-,*,/四種二元操作符,操作數僅限于整數。

          回來想了一下這個題其實是可以用composite模式來做的,UML靜態類圖如下:



          相應的Java代碼如下(沒有考慮除數為零的異常):
          1interface Exp {
          2    float getRes();
          3}

           1public class Two_e_Exp implements Exp {
           2    private Exp A;
           3
           4    private Exp B;
           5
           6    private char op;
           7
           8    /**
           9     * @param a
          10     * @param b
          11     * @param op
          12     */

          13    public Two_e_Exp(Exp a, Exp b, char op) {
          14        A = a;
          15        B = b;
          16        this.op = op;
          17    }

          18
          19    /**
          20     * @return a
          21     */

          22    public Exp getA() {
          23        return A;
          24    }

          25
          26    /**
          27     * @param a
          28     *            要設置的 a
          29     */

          30    public void setA(Exp a) {
          31        A = a;
          32    }

          33
          34    /**
          35     * @return b
          36     */

          37    public Exp getB() {
          38        return B;
          39    }

          40
          41    /**
          42     * @param b
          43     *            要設置的 b
          44     */

          45    public void setB(Exp b) {
          46        B = b;
          47    }

          48
          49    /**
          50     * @return op
          51     */

          52    public char getOp() {
          53        return op;
          54    }

          55
          56    /**
          57     * @param op
          58     *            要設置的 op
          59     */

          60    public void setOp(char op) {
          61        this.op = op;
          62    }

          63
          64    public float getRes() {
          65        // case '+'
          66        float res = A.getRes() + B.getRes();
          67        switch (op) {
          68            case '-'{
          69                res = A.getRes() - B.getRes();
          70                break;
          71            }

          72            case '*'{
          73                res = A.getRes() * B.getRes();
          74                break;
          75            }

          76            case '/'{
          77                res = A.getRes() / B.getRes();
          78                break;
          79            }

          80        }

          81        return res;
          82    }

          83
          84}

           1public class IntNumber implements Exp {
           2    private int number;
           3    
           4    /**
           5     * @param number
           6     */

           7    public IntNumber(int number) {
           8        this.number = number;
           9    }

          10    
          11    /**
          12     * @return number
          13     */

          14    public int getNumber() {
          15        return number;
          16    }

          17
          18    /**
          19     * @param number 要設置的 number
          20     */

          21    public void setNumber(int number) {
          22        this.number = number;
          23    }

          24
          25    public float getRes() {
          26        return number;
          27    }

          28
          29}

          一個簡單的測試程序:
           1public class Main {
           2
           3    /**
           4     * @param args
           5     */

           6    public static void main(String[] args) {
           7        Exp e1 =new Two_e_Exp(new IntNumber(1),new IntNumber(2),'+');
           8        Exp e2 =new Two_e_Exp(new IntNumber(3),new IntNumber(4),'*');
           9        Exp e =new Two_e_Exp(e2,e1,'/');
          10        // (3*4)/(1+2)=4
          11        System.out.println(e.getRes());
          12        ((Two_e_Exp)e1).setA(new IntNumber(2));
          13        // (3*4)/(2+2)=3
          14        System.out.println(e.getRes());
          15        ((Two_e_Exp)e).setA(new Two_e_Exp(new IntNumber(12),new IntNumber(12),'+'));
          16        //(12+12)/(2+2)=6
          17        System.out.println(e.getRes());
          18        //(12+12)/(2+(5-3))=6
          19        ((Two_e_Exp)e1).setB(new Two_e_Exp(new IntNumber(5),new IntNumber(3),'-'));
          20        System.out.println(e.getRes());
          21    }

          22
          23}

          posted on 2007-07-13 23:15 閱讀(1425) 評論(5)  編輯  收藏 所屬分類: My Program

          評論

          # re: 今天OO考試的一個題 2007-07-15 12:29 劉甘泉

          就模式來說,用bridge好的多,題目主要包括兩種抽象,數字實體和操作實體,然后進行組合  回復  更多評論   

          # re: 今天OO考試的一個題 2007-07-16 00:29

          @劉甘泉
          是否可以說詳細點嗎?謝謝!
            回復  更多評論   

          # re: 今天OO考試的一個題 2007-07-16 09:29 GHawk

          可以更進一步Refactoring到Interpreter模式,把switch(op)去掉,給擴展運算符提供支持。  回復  更多評論   

          # re: 今天OO考試的一個題 2007-07-16 10:41 劉甘泉

          把你的代碼修改了一下,把操作分離出來。
          Exp.java
          1/**
          2 * User: liugq
          3 * Date: 2007-7-16
          4 * Time: 10:15:12
          5 */

          6public interface Exp {
          7    float getRes();
          8}

          9
          ExpAbstract.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:29:26
           5 */

           6public interface ExpAbstract extends Exp {
           7    public  Operation getOp();
           8
           9    public  void setOp(Operation op);
          10
          11    public  Exp getA();
          12
          13    public  void setA(Exp a);
          14
          15    public  Exp getB();
          16
          17    public  void setB(Exp b);
          18}

          19
          IntNumber.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:18:22
           5 */

           6public class IntNumber implements Exp {
           7    private int number;
           8
           9    public IntNumber(int number) {
          10        this.number = number;
          11    }

          12
          13    public int getNumber() {
          14        return number;
          15    }

          16
          17    public void setNumber(int number) {
          18        this.number = number;
          19    }

          20
          21    public float getRes() {
          22        return number;
          23    }

          24}

          25
          MultiplyOp.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:20:46
           5 */

           6public class MultiplyOp implements Operation {
           7    private static Operation instance = new MultiplyOp();
           8
           9    public static Operation newInstance() {
          10        return instance;
          11    }

          12
          13    public float execute(Exp a, Exp b) {
          14        return a.getRes() * b.getRes();
          15    }

          16}

          17
          Operation.java
           
          1/**
          2 * User: liugq
          3 * Date: 2007-7-16
          4 * Time: 10:16:33
          5 */

          6public interface Operation {
          7    float execute(Exp a,Exp b);
          8}

          9
          PlusOp.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:19:37
           5 */

           6public class PlusOp implements Operation {
           7    private static Operation instance = new PlusOp();
           8
           9    public static Operation newInstance() {
          10        return instance;
          11    }

          12
          13    public float execute(Exp a, Exp b) {
          14        return a.getRes() + b.getRes();
          15    }

          16}

          17
          OpFactory.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:22:18
           5 */

           6public class OpFactory {
           7    public static Operation getPlus() {
           8        return PlusOp.newInstance();
           9    }

          10
          11    public static Operation getMultiply() {
          12        return MultiplyOp.newInstance();
          13    }

          14}

          15
          Two_e_Exp.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:15:41
           5 */

           6public class Two_e_Exp implements ExpAbstract {
           7    private Exp A;
           8    private Exp B;
           9    private Operation op;
          10
          11    public Two_e_Exp(Exp a, Exp b, Operation op) {
          12        A = a;
          13        B = b;
          14        this.op = op;
          15    }

          16
          17    public Operation getOp() {
          18        return op;
          19    }

          20
          21    public void setOp(Operation op) {
          22        this.op = op;
          23    }

          24
          25    public Exp getA() {
          26        return A;
          27    }

          28
          29    public void setA(Exp a) {
          30        A = a;
          31    }

          32
          33    public Exp getB() {
          34        return B;
          35    }

          36
          37    public void setB(Exp b) {
          38        B = b;
          39    }

          40
          41    public float getRes() {
          42        return op.execute(A, B);
          43    }

          44}

          45
          test.java
           1/**
           2 * User: liugq
           3 * Date: 2007-7-16
           4 * Time: 10:27:02
           5 */

           6public class test {
           7    public static void main(String[] args) {
           8        ExpAbstract e1 = new Two_e_Exp(new IntNumber(1), new IntNumber(2),
           9                OpFactory.getPlus());
          10        ExpAbstract e2 = new Two_e_Exp(new IntNumber(3), new IntNumber(4),
          11                OpFactory.getMultiply());
          12        ExpAbstract e = new Two_e_Exp(e1, e2, OpFactory.getPlus());
          13        System.out.println(e.getRes());
          14    }

          15}

          16
          有點亂   回復  更多評論   

          # re: 今天OO考試的一個題[未登錄] 2007-07-16 21:07

          謝謝樓上的2位指教!  回復  更多評論   

          主站蜘蛛池模板: 大城县| 平湖市| 高台县| 孟村| 芜湖县| 宜川县| 凉城县| 南涧| 麻江县| 灵川县| 政和县| 新昌县| 团风县| 兴城市| 楚雄市| 泸州市| 枝江市| 和林格尔县| 汝城县| 裕民县| 黄浦区| 姚安县| 恭城| 乾安县| 汝南县| 全椒县| 塔河县| 南充市| 北川| 乾安县| 海宁市| 旌德县| 苏尼特右旗| 大安市| 克东县| 鸡西市| 石楼县| 尉犁县| 博白县| 蓬溪县| 长乐市|