享元模式以共享的方式高效地支持大量的细粒度对象.
1).FlyweightDemo.java
package com.flyingh.demo;
public class FlyweightDemo {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "a" + "bc";
String str3 = "abc";
String str4 = str3 + "";
final String str5 = "ab";
System.out.println(str1 == str2);
System.out.println(str2 == str3);
System.out.println(str3 == str4);
str4 = str5 + "c";
System.out.println(str3 == str4);
System.out.println("*********");
Integer i1 = 3;
Integer i2 = 3;
Integer i3 = 1 + 2;
System.out.println(i1 == i2);
System.out.println(i2 == i3);
System.out.println("*********");
Long l1 = 127l;
Long l2 = 127l;
System.out.println(l1 == l2);
}
}
程序运行结果如下:
true
true
false
true
*********
true
true
*********
true
2).假设有许许多多的Athlete进行比赛,他们按Key(包含age,sex,weight)分组进行比赛,即使分组后每组也有许许多多的Athlete:
Key.java
package com.flyingh.vo;
public class Key {
private int age;
private Sex sex;
private float weight;
public Key(int age, Sex sex, float weight) {
super();
this.age = age;
this.sex = sex;
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Sex getSex() {
return sex;
}
public void setSex(Sex sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
result = prime * result + Float.floatToIntBits(weight);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key other = (Key) obj;
if (age != other.age)
return false;
if (sex != other.sex)
return false;
if (Float.floatToIntBits(weight) != Float.floatToIntBits(other.weight))
return false;
return true;
}
@Override
public String toString() {
return "[age=" + age + ", sex=" + sex + ", weight=" + weight + "]";
}
}
Athlete.java
package com.flyingh.vo;
public class Athlete {
private Key key;
public Athlete(Key key) {
super();
this.key = key;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public void setName(String name) {
System.out.println("名字为:" + name);
}
}
Sex.java
package com.flyingh.vo;
public enum Sex {
man, woman;
}
AthleteFactory.java
package com.flyingh.pool;
import java.util.HashMap;
import java.util.Map;
import com.flyingh.vo.Key;
import com.flyingh.vo.Athlete;
public class AthleteFactory {
private static Map<Key, Athlete> map = new HashMap<Key, Athlete>();
public static Athlete getAthlete(Key key) {
if (map.containsKey(key)) {
System.out.println(key + "已经存在!");
return map.get(key);
} else {
System.out.println(key + "不存在,创建后放入Pool!");
Athlete person = new Athlete(key);
map.put(key, person);
return person;
}
}
}
Client.java
package com.flyingh.client;
import com.flyingh.pool.AthleteFactory;
import com.flyingh.vo.Key;
import com.flyingh.vo.Athlete;
import com.flyingh.vo.Sex;
public class Client {
public static void main(String[] args) {
Key key1 = new Key(18, Sex.man, 60);
Key key2 = new Key(20, Sex.woman, 55);
Athlete zhangsan = AthleteFactory.getAthlete(key1);
zhangsan.setName("张三");
Athlete lisi = AthleteFactory.getAthlete(key1);
lisi.setName("李四");
Athlete wangwu = AthleteFactory.getAthlete(key2);
wangwu.setName("王五");
Athlete zhaoliu = AthleteFactory.getAthlete(new Key(18, Sex.man, 60));
zhaoliu.setName("赵六");
}
}
程序运行结果如下:
[age=18, sex=man, weight=60.0]不存在,创建后放入Pool!
名字为:张三
[age=18, sex=man, weight=60.0]已经存在!
名字为:李四
[age=20, sex=woman, weight=55.0]不存在,创建后放入Pool!
名字为:王五
[age=18, sex=man, weight=60.0]已经存在!
名字为:赵六