Skip to content

Commit

Permalink
add 享元模式
Browse files Browse the repository at this point in the history
  • Loading branch information
nivance committed Jan 5, 2015
1 parent e374532 commit 2c6fd53
Show file tree
Hide file tree
Showing 26 changed files with 761 additions and 0 deletions.
Binary file added src/dp/com/company/flyweight/flyweight.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/dp/com/company/flyweight/section1/Client.java
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();
//进行其他业务处理
}


}
44 changes: 44 additions & 0 deletions src/dp/com/company/flyweight/section1/SignInfo.java
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 src/dp/com/company/flyweight/section1/SignInfoFactory.java
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();
}
}
23 changes: 23 additions & 0 deletions src/dp/com/company/flyweight/section2/Client.java
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");
}


}
44 changes: 44 additions & 0 deletions src/dp/com/company/flyweight/section2/SignInfo.java
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;
}


}
25 changes: 25 additions & 0 deletions src/dp/com/company/flyweight/section2/SignInfo4Pool.java
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 src/dp/com/company/flyweight/section2/SignInfoFactory.java
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 src/dp/com/company/flyweight/section3/ConcreteFlyweight1.java
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 src/dp/com/company/flyweight/section3/ConcreteFlyweight2.java
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(){
//业务逻辑
}
}
28 changes: 28 additions & 0 deletions src/dp/com/company/flyweight/section3/Flyweight.java
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 src/dp/com/company/flyweight/section3/FlyweightFactory.java
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;
}

}
62 changes: 62 additions & 0 deletions src/dp/com/company/flyweight/section3/flyweight.ucls
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>
31 changes: 31 additions & 0 deletions src/dp/com/company/flyweight/section4/Client.java
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();

}
}


}
Loading

0 comments on commit 2c6fd53

Please sign in to comment.