Skip to content

Commit 2d6b72d

Browse files
committed
Merge branch 'spring' of https://github.com/paigeman/DesignPattern into spring
� Conflicts: � pom.xml � res/doc/pattern/cp/prototype/prototype.md
2 parents 1c7bbc6 + 1e83657 commit 2d6b72d

14 files changed

+230
-0
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<scope>test</scope>
2727
</dependency>
2828

29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-context</artifactId>
32+
<version>5.2.12.RELEASE</version>
33+
</dependency>
34+
2935
<!--CGLib-->
3036
<dependency>
3137
<groupId>cglib</groupId>

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

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

161+
#### [在Spring中的源码分析](spring.md)
162+
161163
#### [原型模式下的浅拷贝问题](shallow.md)
162164

163165
#### [对于浅拷贝问题的解决(深拷贝)](deep.md)
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# 在Spring中的源码分析
2+
3+
#### 例子
4+
5+
```xml
6+
<?xml version="1.0" encoding="UTF-8" ?>
7+
<beans xmlns="http://www.springframework.org/schema/beans"
8+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:schemaLocation="http://www.springframework.org/schema/beans
10+
http://www.springframework.org/schema/beans/spring-beans.xsd">
11+
12+
<bean id="id01" class="org.fade.pattern.cp.prototype.spring.Monster" scope="prototype" />
13+
14+
</beans>
15+
```
16+
17+
```java
18+
public class Monster {
19+
20+
private Integer id = 10;
21+
22+
private String nickName = "牛魔王";
23+
24+
private String skill = "芭蕉扇";
25+
26+
public Monster(){
27+
28+
}
29+
30+
public Monster(Integer id, String nickName, String skill) {
31+
this.id = id;
32+
this.nickName = nickName;
33+
this.skill = skill;
34+
}
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public String getNickName() {
45+
return nickName;
46+
}
47+
48+
public void setNickName(String nickName) {
49+
this.nickName = nickName;
50+
}
51+
52+
public String getSkill() {
53+
return skill;
54+
}
55+
56+
public void setSkill(String skill) {
57+
this.skill = skill;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "Monster{" +
63+
"id=" + id +
64+
", nickName='" + nickName + '\'' +
65+
", skill='" + skill + '\'' +
66+
'}';
67+
}
68+
69+
}
70+
71+
public class Prototype {
72+
73+
public static void main(String[] args) {
74+
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(System.getProperty("user.dir")
75+
+"/src/main/java/org/fade/pattern/cp/prototype/spring/ApplicationContext.xml");
76+
Object bean1 = applicationContext.getBean("id01");
77+
System.out.println("bean1:"+bean1);
78+
Object bean2 = applicationContext.getBean("id01");
79+
System.out.println("bean2:"+bean2);
80+
System.out.println(bean1==bean2);
81+
}
82+
83+
}
84+
```
85+
86+
#### 运行结果
87+
88+
```
89+
bean1:Monster{id=10, nickName='牛魔王', skill='芭蕉扇'}
90+
bean2:Monster{id=10, nickName='牛魔王', skill='芭蕉扇'}
91+
false
92+
```
93+
94+
#### [代码](../../../../../src/main/java/org/fade/pattern/cp/prototype/spring)
95+
96+
#### DEBUG调试
97+
98+
1. ###### 单击图示处打上断点
99+
100+
![#1](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-39-02.png)
101+
102+
2. ###### 右键点击图示1处,选择图示2处选项“Debug 'Prototype.main()'”
103+
104+
![#2](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-41-59.png)
105+
106+
3. ###### 等调试系统上线后,点击图示处选项“Step Into”
107+
108+
![#3](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-45-52.png)
109+
110+
4. ###### 再点击图示处选项“Step Over”
111+
112+
![#4](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-47-16.png)
113+
114+
5. ###### 当代码运行至下图所示时,再次点击选项“Step Into”
115+
116+
![#5](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-49-03.png)
117+
118+
6. ###### 当出现如下图所示内容时,点击“getBean”
119+
120+
![#6](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-51-26.png)
121+
122+
7. ###### 当出现如下图所示内容时,点击选项“Step Into”
123+
124+
![#7](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-54-33.png)
125+
126+
127+
8. ###### 滚动页面,当代码为下图所示的内容时,可以发现此处Spring中确实采用了原型模式
128+
129+
![#8](../../../../img/pattern/cp/prototype/Snipaste_2021-02-15_15-56-21.png)
130+
131+
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)