标签:private color hashmap 红色 getc mode div ash this
1.享元模式//假如已经有了这么对象就是用这个对象,没有就创建这个对象,例如String等。
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 } }
标签:private color hashmap 红色 getc mode div ash this
原文地址:https://www.cnblogs.com/gg128/p/9570277.html