-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nivance
committed
Jan 5, 2015
1 parent
e374532
commit 2c6fd53
Showing
26 changed files
with
761 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.company.flyweight.section1; | ||
|
||
public class Client { | ||
|
||
public static void main(String[] args) { | ||
//从工厂中获得一个对象 | ||
SignInfo signInfo = SignInfoFactory.getSignInfo(); | ||
//进行其他业务处理 | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.company.flyweight.section1; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class SignInfo { | ||
|
||
//报名人员的ID | ||
private String id; | ||
//考试地点 | ||
private String location; | ||
//考试科目 | ||
private String subject; | ||
//邮寄地址 | ||
private String postAddress; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
public String getLocation() { | ||
return location; | ||
} | ||
public void setLocation(String location) { | ||
this.location = location; | ||
} | ||
public String getSubject() { | ||
return subject; | ||
} | ||
public void setSubject(String subject) { | ||
this.subject = subject; | ||
} | ||
public String getPostAddress() { | ||
return postAddress; | ||
} | ||
public void setPostAddress(String postAddress) { | ||
this.postAddress = postAddress; | ||
} | ||
|
||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/dp/com/company/flyweight/section1/SignInfoFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.company.flyweight.section1; | ||
|
||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class SignInfoFactory { | ||
//报名信息的对象擦很难生气 | ||
public static SignInfo getSignInfo(){ | ||
return new SignInfo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.company.flyweight.section2; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class Client { | ||
|
||
public static void main(String[] args) { | ||
//初始化对象池 | ||
for(int i=0;i<4;i++){ | ||
String subject = "科目" + i; | ||
//初始化地址 | ||
for(int j=0;j<30;j++){ | ||
String key = subject + "考试地点"+j; | ||
SignInfoFactory.getSignInfo(key); | ||
} | ||
} | ||
SignInfo signInfo = SignInfoFactory.getSignInfo("科目1考试地点1"); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.company.flyweight.section2; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class SignInfo { | ||
|
||
//报名人员的ID | ||
private String id; | ||
//考试地点 | ||
private String location; | ||
//考试科目 | ||
private String subject; | ||
//邮寄地址 | ||
private String postAddress; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
public String getLocation() { | ||
return location; | ||
} | ||
public void setLocation(String location) { | ||
this.location = location; | ||
} | ||
public String getSubject() { | ||
return subject; | ||
} | ||
public void setSubject(String subject) { | ||
this.subject = subject; | ||
} | ||
public String getPostAddress() { | ||
return postAddress; | ||
} | ||
public void setPostAddress(String postAddress) { | ||
this.postAddress = postAddress; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.company.flyweight.section2; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class SignInfo4Pool extends SignInfo { | ||
|
||
//定义一个对象池提取的KEY值 | ||
private String key; | ||
|
||
//构造函数获得相同标志 | ||
public SignInfo4Pool(String _key){ | ||
this.key = _key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/dp/com/company/flyweight/section2/SignInfoFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.company.flyweight.section2; | ||
|
||
import java.util.HashMap; | ||
|
||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class SignInfoFactory { | ||
//池容器 | ||
private static HashMap<String,SignInfo> pool = new HashMap<String,SignInfo>(); | ||
|
||
//报名信息的对象工厂 | ||
@Deprecated | ||
public static SignInfo getSignInfo(){ | ||
return new SignInfo(); | ||
} | ||
|
||
//从池中获得对象 | ||
public static SignInfo getSignInfo(String key){ | ||
//设置返回对象 | ||
SignInfo result = null; | ||
//池中没有该对象,则建立,并放入池中 | ||
if(!pool.containsKey(key)){ | ||
System.out.println(key + "----建立对象,并放置到池中"); | ||
result = new SignInfo4Pool(key); | ||
pool.put(key, result); | ||
}else{ | ||
result = pool.get(key); | ||
System.out.println(key +"---直接从直池中取得"); | ||
} | ||
return result; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/dp/com/company/flyweight/section3/ConcreteFlyweight1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.company.flyweight.section3; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class ConcreteFlyweight1 extends Flyweight{ | ||
|
||
//接受外部状态 | ||
public ConcreteFlyweight1(String _Extrinsic){ | ||
super(_Extrinsic); | ||
} | ||
|
||
//根据外部状态进行逻辑处理 | ||
public void operate(){ | ||
//业务逻辑 | ||
} | ||
|
||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/dp/com/company/flyweight/section3/ConcreteFlyweight2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.company.flyweight.section3; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class ConcreteFlyweight2 extends Flyweight{ | ||
|
||
//接受外部状态 | ||
public ConcreteFlyweight2(String _Extrinsic){ | ||
super(_Extrinsic); | ||
} | ||
|
||
//根据外部状态进行逻辑处理 | ||
public void operate(){ | ||
//业务逻辑 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.company.flyweight.section3; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public abstract class Flyweight { | ||
//内部状态 | ||
private String intrinsic; | ||
//外部状态 | ||
protected final String Extrinsic; | ||
//要求享元角色必须接受外部状态 | ||
public Flyweight(String _Extrinsic){ | ||
this.Extrinsic = _Extrinsic; | ||
} | ||
|
||
//定义业务操作 | ||
public abstract void operate(); | ||
|
||
//内部状态的getter/setter | ||
public String getIntrinsic() { | ||
return intrinsic; | ||
} | ||
public void setIntrinsic(String intrinsic) { | ||
this.intrinsic = intrinsic; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/dp/com/company/flyweight/section3/FlyweightFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.company.flyweight.section3; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class FlyweightFactory { | ||
//定义一个池容器 | ||
private static HashMap<String,Flyweight> pool= new HashMap<String,Flyweight>(); | ||
|
||
//享元工厂 | ||
public static Flyweight getFlyweight(String Extrinsic){ | ||
//需要返回的对象 | ||
Flyweight flyweight = null; | ||
//在池中没有改对象 | ||
if(pool.containsKey(Extrinsic)){ | ||
flyweight = pool.get(Extrinsic); | ||
}else{ | ||
//根据外部状态创建享元对象 | ||
flyweight = new ConcreteFlyweight1(Extrinsic); | ||
//放置到池中 | ||
pool.put(Extrinsic, flyweight); | ||
} | ||
|
||
return flyweight; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<class-diagram version="1.1.6" icons="true" always-add-relationships="false" generalizations="true" realizations="true" | ||
associations="true" dependencies="false" nesting-relationships="true"> | ||
<class id="1" language="java" name="com.company.flyweight.section3.Flyweight" project="DPModel" | ||
file="/DPModel/src/dp/com/company/flyweight/section3/Flyweight.java" binary="false" corner="BOTTOM_RIGHT"> | ||
<position height="-1" width="-1" x="220" y="160"/> | ||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true" | ||
visibility="true"> | ||
<attributes public="true" package="true" protected="true" private="true" static="true"/> | ||
<operations public="true" package="true" protected="true" private="true" static="true"/> | ||
</display> | ||
</class> | ||
<class id="2" language="java" name="com.company.flyweight.section3.FlyweightFactory" project="DPModel" | ||
file="/DPModel/src/dp/com/company/flyweight/section3/FlyweightFactory.java" binary="false" corner="BOTTOM_RIGHT"> | ||
<position height="-1" width="-1" x="555" y="159"/> | ||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true" | ||
visibility="true"> | ||
<attributes public="true" package="true" protected="true" private="true" static="true"/> | ||
<operations public="true" package="true" protected="true" private="true" static="true"/> | ||
</display> | ||
</class> | ||
<class id="3" language="java" name="com.company.flyweight.section3.ConcreteFlyweight1" project="DPModel" | ||
file="/DPModel/src/dp/com/company/flyweight/section3/ConcreteFlyweight1.java" binary="false" corner="BOTTOM_RIGHT"> | ||
<position height="-1" width="-1" x="109" y="356"/> | ||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true" | ||
visibility="true"> | ||
<attributes public="true" package="true" protected="true" private="true" static="true"/> | ||
<operations public="true" package="true" protected="true" private="true" static="true"/> | ||
</display> | ||
</class> | ||
<class id="4" language="java" name="com.company.flyweight.section3.ConcreteFlyweight2" project="DPModel" | ||
file="/DPModel/src/dp/com/company/flyweight/section3/ConcreteFlyweight2.java" binary="false" corner="BOTTOM_RIGHT"> | ||
<position height="-1" width="-1" x="351" y="357"/> | ||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" accessors="true" | ||
visibility="true"> | ||
<attributes public="true" package="true" protected="true" private="true" static="true"/> | ||
<operations public="true" package="true" protected="true" private="true" static="true"/> | ||
</display> | ||
</class> | ||
<generalization id="5"> | ||
<end type="SOURCE" refId="3"/> | ||
<end type="TARGET" refId="1"/> | ||
</generalization> | ||
<generalization id="6"> | ||
<end type="SOURCE" refId="4"/> | ||
<end type="TARGET" refId="1"/> | ||
</generalization> | ||
<association id="7"> | ||
<end type="SOURCE" refId="2" navigable="false"> | ||
<attribute id="8" name="pool"/> | ||
<multiplicity id="9" minimum="0" maximum="2147483647"/> | ||
</end> | ||
<end type="TARGET" refId="1" navigable="true"/> | ||
<display labels="true" multiplicity="true"/> | ||
</association> | ||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true" | ||
accessors="true" visibility="true"> | ||
<attributes public="true" package="true" protected="true" private="true" static="true"/> | ||
<operations public="true" package="true" protected="true" private="true" static="true"/> | ||
</classifier-display> | ||
<association-display labels="true" multiplicity="true"/> | ||
</class-diagram> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.company.flyweight.section4; | ||
|
||
/** | ||
* @author cbf4Life [email protected] | ||
* I'm glad to share my knowledge with you all. | ||
*/ | ||
public class Client { | ||
|
||
public static void main(String[] args) { | ||
//在对象池中初始化四个对象 | ||
SignInfoFactory.getSignInfo("科目1"); | ||
SignInfoFactory.getSignInfo("科目2"); | ||
SignInfoFactory.getSignInfo("科目3"); | ||
SignInfoFactory.getSignInfo("科目4"); | ||
|
||
//取得对象 | ||
SignInfo signInfo = SignInfoFactory.getSignInfo("科目2"); | ||
while(true){ | ||
signInfo.setId("ZhangSan"); | ||
signInfo.setLocation("ZhangSan"); | ||
(new MultiThread(signInfo)).start(); | ||
|
||
signInfo.setId("LiSi"); | ||
signInfo.setLocation("LiSi"); | ||
(new MultiThread(signInfo)).start(); | ||
|
||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.