design_model(11)flyweight
1.享元模式
池技术的重要实现方式,它可以减少应用程序创建的对象,降低程序内存的占用,提高程序的性能。
2.实例
public class Color { private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } } public class FlyWeight { private Map<String, Color> colors = new HashMap<>(); public Color getColor(String str) { if (colors.containsKey(str)) { return colors.get(str); } else { Color color = new Color(); colors.put(str, color); return color; } } } public class Client { public static void main(String[] args) { FlyWeight flyWeight = new FlyWeight(); Color color = flyWeight.getColor("红色"); Color color2 = flyWeight.getColor("红色"); System.out.println(color == color2);// true } }