场景:
内存属于稀缺资源,不要随便浪费,如果有很多完全相同或相似的对象,我们可以通过享元模式节约内存
核心:
享元模式以共享的方式高效的支持大量细粒度对象的重用。
享元模式可以做到共享的关键是区分了内部状态和外部状态。
内部状态:可以共享,不会随环境变化而改变
外部状态:不可以共享,会随环境变化而改变
使用时经常和工厂模式搭配使用
应用场景:
线程池、数据库连接池、String等
优点:
极大减少内存中对象的数量
相同 或相似对象内存中只存一份,极大的节约资源,提高系统性能
外部状态相对独立,不影响内部状态
缺点:
模式较为复杂,使程序逻辑复杂化
为了节省内存,共享了内部状态,分理出外部状态,而读取外部状态使运行时间变长,用时间换取了空间
示例
package com.bjsxt.structure.flyweight;
/**
* 围棋棋子模拟享元类
* 棋子:颜色,位置
* 颜色不变,可共享,设计为享元类
* 位置变化,不可共享,非共享享元类
* @author WL20180732
*
*/
public interface ChessFlyWeight {
void setColor(String c);
String getColor();
void display(Coordinate coordinate); // 显示棋子
}
class ConcreteChess implements ChessFlyWeight {
private String color;
@Override
public void display(Coordinate coordinate) {
System.out.println("棋子颜色:" + color +"棋子位置:(" + coordinate.getX() + ", " + coordinate.getY() +")");
}
@Override
public void setColor(String c) {
this.color = c;
}
@Override
public String getColor() {
return color;
}
public ConcreteChess(String color) {
this.color = color;
}
}
package com.bjsxt.structure.flyweight;
/**
* 棋子坐标
* 模拟外部状态
* @author WL20180732
*
*/
public class Coordinate {
private int x, y;
public Coordinate(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package com.bjsxt.structure.flyweight;
import java.util.HashMap;
import java.util.Map;
/**
* 享元工厂类
* @author WL20180732
*
*/
public class ChessFlyWeightFactory {
// 享元池
private static Map<String, ChessFlyWeight> map = new HashMap<String, ChessFlyWeight>();
// 获得棋子
public static ChessFlyWeight getChess(String color) {
if (map.containsKey(color)) {
return map.get(color);
} else {
ChessFlyWeight chessFlyWeight = new ConcreteChess(color);
map.put(color, chessFlyWeight);
return chessFlyWeight;
}
}
}
package com.bjsxt.structure.flyweight;
public class Client {
public static void main(String[] args) {
ChessFlyWeight chess1 = ChessFlyWeightFactory.getChess("黑色");
ChessFlyWeight chess2 = ChessFlyWeightFactory.getChess("黑色");
System.out.println(chess1);
System.out.println(chess2);
System.out.println("增加外部状态的处理=============");
chess1.display(new Coordinate(10, 10));
chess2.display(new Coordinate(20, 20));
}
}