概念:
a,内在信息:对象的内在信息是独立与对象所处环境的
b,外部信息:一个对象的外部信息是与对象所处华宁有关的,并且随着环境的变化而有所不同。
享元应用场合:
该应用需要创建大量的对象,而这些对象只有部分参数是一致的。创建和维护这么多对象的代价是非常昂贵的,需要消耗大量的内存。
享元模式建议将所有的内在共同数据分离出来,建立一个单独的对象,也就是享元。这样就可以节省可观的内存以及时间开销。
实例:
它用于打印一家大型组织中所有雇员的名片数据,该组织拥有四个主要的分区办事处。
名片格式:
(外部数据)
《雇员姓名》
《头衔》
(内部数据)
《公司名称》
《分区办事处地址》
《城市》《州》《压缩码》
public interface FlyweightIntr {
public String getCompany();
public String getAddress();
public String getCity();
public String getState();
public String getZip();
}
import java.util.*;
// singleton Flyweight Factory
public class FlyweightFactory {
private HashMap lstFlyweight;
private static FlyweightFactory factory =
new FlyweightFactory();
private FlyweightFactory() {
lstFlyweight = new HashMap();
}
public synchronized FlyweightIntr getFlyweight(
String divisionName) {
if (lstFlyweight.get(divisionName) == null) {
FlyweightIntr fw = new Flyweight(divisionName);
lstFlyweight.put(divisionName, fw);
return fw;
} else {
return (FlyweightIntr) lstFlyweight.get(divisionName);
}
}
public static FlyweightFactory getInstance() {
return factory;
}
//Inner flyweight class
private class Flyweight implements FlyweightIntr {
private String company;
private String address;
private String city;
private String state;
private String zip;
private void setValues(String cmp, String addr,
String cty, String st, String zp) {
company = cmp;
address = addr;
city = cty;
state = st;
zip = zp;
}
private Flyweight(String division) {
// values are hard coded
//for simplicity
if (division.equals("North")) {
setValues("CMP","addr1", "cty1","st1","10000");
}
if (division.equals("South")) {
setValues("CMP","addr2", "cty2","st2","20000");
}
if (division.equals("East")) {
setValues("CMP","addr3", "cty3","st3","30000");
}
if (division.equals("West")) {
setValues("CMP","addr4", "cty4","st4","40000");
}
}
public String getCompany() {
return company;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
}// end of Flyweight
}// end of FlyweightFactory
public class VCard {
String name;
String title;
FlyweightIntr objFW;
public VCard(String n, String t, FlyweightIntr fw) {
name = n;
title = t;
objFW = fw;
}
public void print() {
System.out.println(name);
System.out.println(title);
System.out.println(objFW.getAddress() + "-" +
objFW.getCity() + "-" +
objFW.getState() + "-" +
objFW.getZip());
System.out.println("----------------");
}
}
import java.io.*;
import java.util.*;
public class FileUtil {
DataOutputStream dos;
public String OutputToFile(String FileName, String DataLine,
boolean AppendMode, boolean NewLine) {
if ((FileExists(FileName)) && (AppendMode == false)) {
try {
String tempFile =
FileName + new Date().getTime();
File OutFile = new File(tempFile);
dos = new DataOutputStream(
new FileOutputStream(OutFile));
dos.writeBytes(DataLine);
dos.close();
DeleteFile(FileName);
OutFile.renameTo(new File(FileName));
} catch (FileNotFoundException ex) {
//
System.out.println(
" File Utility OutputToFile ERROR=" +
ex.getMessage());
System.exit(1);
}
catch (IOException ex) {
//
System.out.println(
" File Utility OutputToFile ERROR=" +
ex.getMessage());
System.exit(1);
}
} else {
if (NewLine) {
DataLine = "\n" + DataLine;
}
try {
File OutFile = new File(FileName);
if (AppendMode) {
dos = new DataOutputStream(
new FileOutputStream(FileName, true));
} else {
dos = new DataOutputStream(
new FileOutputStream(OutFile));
}
dos.writeBytes(DataLine);
dos.close();
} catch (FileNotFoundException ex) {
//
System.out.println(
" File Utility OutputToFile ERROR=" +
ex.getMessage());
System.exit(1);
}
catch (IOException ex) {
//
System.out.println(
" File Utility OutputToFile ERROR=" +
ex.getMessage());
System.exit(1);
}
} // If-Else
return ("Success");
}
public String ReadFromFile(String FileName) {
String DataLine = "";
try {
File InFile = new File(FileName);
BufferedReader dos = new BufferedReader(
new InputStreamReader(
new FileInputStream(InFile)));
DataLine = dos.readLine();
dos.close();
} catch (FileNotFoundException ex) {
//
System.out.println(
" File Utility ReadFromFile ERROR=" +
ex.getMessage());
System.exit(1);
}
catch (IOException ex) {
//
System.out.println(
" File Utility ReadFromFile ERROR=" +
ex.getMessage());
System.exit(1);
}
return (DataLine);
}
public boolean FileExists(String FileName) {
File InFile = new File(FileName);
return InFile.exists();
}
public boolean DeleteFile(String FileName) {
File InFile = new File(FileName);
return InFile.delete();
}
public Vector FileToVector(String FileName) {
Vector v = new Vector();
String inputLine;
try {
File InFile = new File(FileName);
BufferedReader din = new BufferedReader(
new InputStreamReader(
new FileInputStream(InFile)));
while ((inputLine = din.readLine()) != null) {
v.addElement(inputLine);
}
din.close();
} // Try
catch (FileNotFoundException ex) {
//
System.out.println(
" File Utility FiletoVector ERROR=" +
ex.getMessage());
}
catch (IOException ex) {
//
System.out.println(
" File Utility FiletoVector ERROR=" +
ex.getMessage());
}
return (v);
}
public void VectorToFile(Vector v, String FileName) {
for (int i = 0; i < v.size(); i++) {
OutputToFile(FileName, (String) v.elementAt(i), true,
true);
}
}
}// end of class
import java.io.*;
import java.util.*;
public class FlyweightTest {
public static void main(String[] args) throws Exception {
Vector empList = initialize();
FlyweightFactory factory =
FlyweightFactory.getInstance();
for (int i = 0; i < empList.size(); i++) {
String s = (String) empList.elementAt(i);
StringTokenizer st = new StringTokenizer(s, ",");
String name = st.nextToken();
String title = st.nextToken();
String division = st.nextToken();
FlyweightIntr flyweight =
factory.getFlyweight(division);
//associate the flyweight
//with the extrinsic data object.
VCard card = new VCard(name, title, flyweight);
card.print();
}
}
private static Vector initialize() {
//for simplicity values are being hardcoded.
Vector v = new Vector();
v.add("name1,title1,North");
v.add("name2,title2,South");
v.add("name3,title1,North");
v.add("name4,title3,East");
v.add("name5,title4,East");
v.add("name6,title2,East");
v.add("name7,title1,West");
v.add("name8,title3,West");
v.add("name9,title1,West");
v.add("name10,title6,South");
v.add("name11,title5,North");
v.add("name12,title1,North");
return v;
}
}
文档与源代码下载地址:http://download.csdn.net/detail/hnzhangshilong/3685894