无题

拿个学位是骗自己的。学问是一辈子的。

统计

留言簿(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 阅读(1397) 评论(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位指教!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: