-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathBusinessService.java
More file actions
142 lines (115 loc) · 3.93 KB
/
BusinessService.java
File metadata and controls
142 lines (115 loc) · 3.93 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cn.aofeng.demo.aspectj;
/**
* 模拟业务方法,将被Aspectj织入代码,增加功能。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class BusinessService {
public long add(int a, int b) {
return a+b;
}
public long add(int a, int b, int... other) {
long result = a + b;
for (int i : other) {
result += i;
}
return result;
}
public String join(String first, String... appends) {
if (null == first) {
throw new IllegalArgumentException("first is null");
}
StringBuilder buffer = new StringBuilder();
buffer.append(first);
for (String str : appends) {
buffer.append(str);
}
return buffer.toString();
}
public String addPrefix(String src) {
if (null == src) {
throw new IllegalArgumentException("src is null");
}
return "-->"+src;
}
public static void printLine(char style) {
if ('=' == style) {
System.out.println("========================================================================================");
} else if ('-' == style) {
System.out.println("----------------------------------------------------------------------------------------");
} else {
System.out.println(" ");
}
}
public static void main(String[] args) {
final BusinessService bs = new BusinessService();
System.out.println("1、执行方法add(int, int)");
RunMethod rm = new RunMethod() {
@Override
public void run() {
long result = bs.add(1, 2);
System.out.println(">>> 结果:" + result);
}
};
rm.execute();
System.out.println("2、执行方法add(int, int, int...)");
rm = new RunMethod() {
@Override
public void run() {
long result = bs.add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println(">>> 结果:" + result);
}
};
rm.execute();
System.out.println("3、执行方法join(String, String...)");
rm = new RunMethod() {
@Override
public void run() {
String str = bs.join("first", "-second", "-third");
System.out.println(">>> 结果:" + str);
}
};
rm.execute();
System.out.println("4、执行方法join(String, String...)");
rm = new RunMethod() {
@Override
public void run() {
String str = bs.join(null, "-second", "-third");
System.out.println(">>> 结果:" + str);
}
};
rm.execute();
System.out.println("5、执行方法addPrefix(String)");
rm = new RunMethod() {
@Override
public void run() {
String str = bs.addPrefix("原字符串");
System.out.println(">>> 结果:" + str);
}
};
rm.execute();
System.out.println("6、执行方法addPrefix(String)");
rm = new RunMethod() {
@Override
public void run() {
String str = bs.addPrefix(null);
System.out.println(">>> 结果:" + str);
}
};
rm.execute();
}
public static abstract class RunMethod {
private char _style = '=';
public void execute() {
printLine(_style);
try {
run();
} catch (Exception e) {
e.printStackTrace(System.err);
}
printLine(_style);
printLine(' ');
}
public abstract void run();
}
}