1.使用场景:
内存资源比较稀缺,不要随便浪费,如果有很多相同或者类似的对象,通过使用享元模式的方法,节省内存,例如线程池以及String类等。
2.UML表示
在享元模式中通常用以下的几类对象:
2.1FlyweightFactory(享元工厂,创建并且管理享元对象)
2.2FlyWeight享元抽象类
2.3ConcreteFlyWeight具体享元类
2.4UnsharedConcreteFlyWeight非共享享元类
具体的UML示意图如下:
3.代码实现
首先,定义一个接口:
public interface ChessFlyWeight {
void setColor();
String getColor();
void display(Coordinate coordinate);
}
定义棋子类:
public class ConcreteChess implements ChessFlyWeight {
private String color;
public ConcreteChess(String color) {
this.color = color;
}
@Override
public void setColor() {
this.color=color;
}
@Override
public String getColor() {
return color;
}
@Override
public void display(Coordinate coordinate) {
System.out.println("棋子颜色:"+color);
System.out.println("棋子位置:"+coordinate.getX()+"-----"+coordinate.getY());
}
}
再定义坐标类:
public class Coordinate {
private int x,y;
public Coordinate(int x, int y) {
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;
}
}
然后是我们的享元工厂:
public class ChessWeightFactory {
private static Map<String,ChessFlyWeight> map=new HashMap<>();
public static ChessFlyWeight getChess(String color){
if (map.get(color)!=null){
return map.get(color);
}
else{
ChessFlyWeight chessFlyWeight=new ConcreteChess(color);
map.put(color,chessFlyWeight);
return chessFlyWeight;
}
}
}
最后,给出一个简单的测试程序:
public class Client {
public static void main(String[] args) {
ChessFlyWeight chessFlyWeight1= ChessWeightFactory.getChess("黑色");
ChessFlyWeight chessFlyWeight2= ChessWeightFactory.getChess("黑色");
System.out.println(chessFlyWeight1.equals(chessFlyWeight2));
System.out.println("增加外部状态的处理");
chessFlyWeight1.display(new Coordinate(0,0));
chessFlyWeight2.display(new Coordinate(10,0));
}
}
4.总结
享元模式以共享的方式高效的支持大量细粒度对象的重用。共享的关键是使用了内部状态以及外部状态,其中:
内部状态指的是可以共享,不会随着环的变化而变化的属性;而外部状态指的是不可以共享,会随着环境变化而变化的属性。对于外部状态,使用外部类来进行处理,而对于内部状态,我们单独通过享元类来进行处理。
下面给出项目的链接:
https://github.com/memoryexplosion/design_pattern_review/tree/master/src/java/facade