Skip to content

Commit f819639

Browse files
committed
#commit 2021_02_15_16_03
1 parent 58d4f1d commit f819639

13 files changed

+225
-0
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<version>4.12</version>
2626
<scope>test</scope>
2727
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-context</artifactId>
32+
<version>5.2.12.RELEASE</version>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>

res/doc/pattern/cp/prototype/prototype.md

+128
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,132 @@ public class Client {
158158
159159
###### [代码](../../../../../src/main/java/org/fade/pattern/cp/prototype/improve)
160160

161+
#### 在Spring中的源码分析
162+
163+
###### [代码](../../../../../src/main/java/org/fade/pattern/cp/prototype/spring)
164+
165+
```xml
166+
<?xml version="1.0" encoding="UTF-8" ?>
167+
<beans xmlns="http://www.springframework.org/schema/beans"
168+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
169+
xsi:schemaLocation="http://www.springframework.org/schema/beans
170+
http://www.springframework.org/schema/beans/spring-beans.xsd">
171+
172+
<bean id="id01" class="org.fade.pattern.cp.prototype.spring.Monster" scope="prototype" />
173+
174+
</beans>
175+
```
176+
177+
```java
178+
public class Monster {
179+
180+
private Integer id = 10;
181+
182+
private String nickName = "牛魔王";
183+
184+
private String skill = "芭蕉扇";
185+
186+
public Monster(){
187+
188+
}
189+
190+
public Monster(Integer id, String nickName, String skill) {
191+
this.id = id;
192+
this.nickName = nickName;
193+
this.skill = skill;
194+
}
195+
196+
public Integer getId() {
197+
return id;
198+
}
199+
200+
public void setId(Integer id) {
201+
this.id = id;
202+
}
203+
204+
public String getNickName() {
205+
return nickName;
206+
}
207+
208+
public void setNickName(String nickName) {
209+
this.nickName = nickName;
210+
}
211+
212+
public String getSkill() {
213+
return skill;
214+
}
215+
216+
public void setSkill(String skill) {
217+
this.skill = skill;
218+
}
219+
220+
@Override
221+
public String toString() {
222+
return "Monster{" +
223+
"id=" + id +
224+
", nickName='" + nickName + '\'' +
225+
", skill='" + skill + '\'' +
226+
'}';
227+
}
228+
229+
}
230+
231+
public class Prototype {
232+
233+
public static void main(String[] args) {
234+
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(System.getProperty("user.dir")
235+
+"/src/main/java/org/fade/pattern/cp/prototype/spring/ApplicationContext.xml");
236+
Object bean1 = applicationContext.getBean("id01");
237+
System.out.println("bean1:"+bean1);
238+
Object bean2 = applicationContext.getBean("id01");
239+
System.out.println("bean2:"+bean2);
240+
System.out.println(bean1==bean2);
241+
}
242+
243+
}
244+
```
245+
246+
###### 运行结果
247+
248+
```
249+
bean1:Monster{id=10, nickName='牛魔王', skill='芭蕉扇'}
250+
bean2:Monster{id=10, nickName='牛魔王', skill='芭蕉扇'}
251+
false
252+
```
253+
254+
###### DEBUG调试
255+
256+
1. ###### 单击图示处打上断点
257+
258+
![#1](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-39-02.png)
259+
260+
2. ###### 右键点击图示1处,选择图示2处选项“Debug 'Prototype.main()'”
261+
262+
![#2](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-41-59.png)
263+
264+
3. ###### 等调试系统上线后,点击图示处选项“Step Into”
265+
266+
![#3](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-45-52.png)
267+
268+
4. ###### 再点击图示处选项“Step Over”
269+
270+
![#4](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-47-16.png)
271+
272+
5. ###### 当代码运行至下图所示时,再次点击选项“Step Into”
273+
274+
![#5](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-49-03.png)
275+
276+
6. ###### 当出现如下图所示内容时,点击“getBean”
277+
278+
![#6](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-51-26.png)
279+
280+
7. ###### 当出现如下图所示内容时,点击选项“Step Into”
281+
282+
![#7](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-54-33.png)
283+
284+
285+
8. ###### 滚动页面,当代码为下图所示的内容时,可以发现此处Spring中确实采用了原型模式
286+
287+
![#8](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-56-21.png)
288+
161289

Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
7+
<bean id="id01" class="org.fade.pattern.cp.prototype.spring.Monster" scope="prototype" />
8+
9+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.fade.pattern.cp.prototype.spring;
2+
3+
/**
4+
* 原型模式
5+
* 在spring框架中的源码分析
6+
* @author fade
7+
* */
8+
public class Monster {
9+
10+
private Integer id = 10;
11+
12+
private String nickName = "牛魔王";
13+
14+
private String skill = "芭蕉扇";
15+
16+
public Monster(){
17+
18+
}
19+
20+
public Monster(Integer id, String nickName, String skill) {
21+
this.id = id;
22+
this.nickName = nickName;
23+
this.skill = skill;
24+
}
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public String getNickName() {
35+
return nickName;
36+
}
37+
38+
public void setNickName(String nickName) {
39+
this.nickName = nickName;
40+
}
41+
42+
public String getSkill() {
43+
return skill;
44+
}
45+
46+
public void setSkill(String skill) {
47+
this.skill = skill;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Monster{" +
53+
"id=" + id +
54+
", nickName='" + nickName + '\'' +
55+
", skill='" + skill + '\'' +
56+
'}';
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.fade.pattern.cp.prototype.spring;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.FileSystemXmlApplicationContext;
5+
6+
/**
7+
* 原型模式
8+
* 在spring框架中的源码分析
9+
* @author fade
10+
* */
11+
public class Prototype {
12+
13+
public static void main(String[] args) {
14+
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(System.getProperty("user.dir")
15+
+"/src/main/java/org/fade/pattern/cp/prototype/spring/ApplicationContext.xml");
16+
Object bean1 = applicationContext.getBean("id01");
17+
System.out.println("bean1:"+bean1);
18+
Object bean2 = applicationContext.getBean("id01");
19+
System.out.println("bean2:"+bean2);
20+
System.out.println(bean1==bean2);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)