http://www.verejava.com/?id=16999120847970
Package Com.flyweight.theory;public class testchess{public static void Main (string[] args) {Chess black1=ch Essfactory.getchess ("Black Chess"); Chess black2=chessfactory.getchess ("Black Chess"); Chess white1=chessfactory.getchess ("White Chess"); System.out.println (Chessfactory.getsize ()); }}package com.flyweight.theory;import Java.util.hashmap;import Java.util.map;public class ChessFactory{private Static map<string,chess> map=new hashmap<string,chess> (); public static int GetSize () {return map.size (); } public static Chess getchess (String key) {Chess chess=null; if (Map.get (key) ==null) {chess=new chess (key); Map.put (key, chess); } else {chess=map.get (key); } return chess; }}package Com.flyweight.theory;public class chess{private int x, y; Private String type; Public Chess (String type) { Super (); This.type = type; } 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 String GetType () {return type; } }
http://www.verejava.com/?id=16999120847970
Pattern of Eagle in Java design mode