/**
*簡單工廠模式其實不是23種設(shè)計模式之一,但是它是創(chuàng)建型模式的最基礎(chǔ)的內(nèi)容,
*工廠方法和抽象工廠都是在它的基礎(chǔ)上的改良版。
*關(guān)于簡單工廠模式的優(yōu)缺點,請看設(shè)計模式(1)------工廠類設(shè)計模式。
*
*
*它是對對象實例化的過程和需要實例化的對象進行細(xì)節(jié)上的封裝。
*
* 優(yōu)點:工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實例化相關(guān)類。對于客戶端來說,去除了與具體產(chǎn)品的依賴。
package simpleFactroy;
2
3
public class Operation {
4
private double dNumberA;
5
private double dNumberB;
6
7
public double getResults(){
8
return 0l ;
9
}
10
11
public double getdNumberA() {
12
return dNumberA;
13
}
14
15
public void setdNumberA(double dNumberA) {
16
this.dNumberA = dNumberA;
17
}
18
19
public double getdNumberB() {
20
return dNumberB;
21
}
22
23
public void setdNumberB(double dNumberB) {
24
this.dNumberB = dNumberB;
25
}
26
27
public Operation(double dNumberA, double dNumberB) {
28
this.dNumberA = dNumberA;
29
this.dNumberB = dNumberB;
30
}
31
32
public Operation(double dNumberA) {
33
this.dNumberA = dNumberA;
34
}
35
36
public Operation() {
37
38
}
39
40
41
}
42
43
44
package simpleFactroy;
45
46
public class OperationAdd extends Operation {
47
public double getResults() {
48
return getdNumberA() + getdNumberB() ;
49
}
50
}
51
52
53
package simpleFactroy;
54
55
public class OperationSub extends Operation {
56
57
public double getResults() {
58
return getdNumberA() - getdNumberB() ;
59
}
60
61
}
62
63
64
65
package simpleFactroy;
66
67
public class OperationSqrt extends Operation {
68
69
@Override
70
public double getResults() {
71
return Math.sqrt(getdNumberA());
72
}
73
74
75
}
76
77
78
79
package simpleFactroy;
80
81
public class OperationFactroy {
82
public static Operation getOperation(String opr){
83
Operation operation = null ;
84
if(opr.equals("+")){
85
operation = new OperationAdd();
86
}else if(opr.equals("-")){
87
operation = new OperationSub();
88
}else{
89
operation = new OperationSqrt();
90
}
91
return operation ;
92
}
93
94
}
95
96
97
98
package simpleFactroy;
99
100
import java.util.Scanner;
101
102
103
public class TestCalculate {
104
public static void main(String[] args)throws Exception{
105
while (true) {
106
Scanner src = new Scanner(System.in);
107
double a = src.nextDouble();
108
String operation = src.next();
109
double b = src.nextDouble();
110
System.out.println(calculate(a, operation, b));
111
System.out.println(calculate(a));
112
}
113
}
114
115
public static double calculate(double a,String operation, double b ){
116
Operation opr = OperationFactroy.getOperation(operation);
117
opr.setdNumberA(a);
118
opr.setdNumberB(b);
119
return opr.getResults();
120
}
121
public static double calculate(double a){
122
Operation opr = OperationFactroy.getOperation("");
123
opr.setdNumberA(a);
124
return opr.getResults();
125
}
126
127
}
128
*簡單工廠模式其實不是23種設(shè)計模式之一,但是它是創(chuàng)建型模式的最基礎(chǔ)的內(nèi)容,
*工廠方法和抽象工廠都是在它的基礎(chǔ)上的改良版。
*關(guān)于簡單工廠模式的優(yōu)缺點,請看設(shè)計模式(1)------工廠類設(shè)計模式。
*
*
*它是對對象實例化的過程和需要實例化的對象進行細(xì)節(jié)上的封裝。
*
* 優(yōu)點:工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實例化相關(guān)類。對于客戶端來說,去除了與具體產(chǎn)品的依賴。
*缺點:當(dāng)需要增加產(chǎn)生一個新對象實例的方法時,需要修改工廠類,這樣,工廠類就違背了“開-閉原則”。
*
*/
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128
