flyweight1.java
来自「郭克华j2ee高级框架实战教学视频源代码 这个是比较值得一看的 由郭克华博士」· Java 代码 · 共 34 行
JAVA
34 行
import java.util.*;
class Word{//编写享元类
public String content;
public String key;
public Word(String content,String key){
System.out.println("构造函数");
this.content = content;
this.key = key;
}
}
//管理享元,用池,可以将享元用HashMap存储
class WordPool{
private static HashMap pool = new HashMap();
public static Word getWord(String key,String content){
Word word = (Word)pool.get(key);
if(word==null){
word = new Word(content,key);
pool.put(key,word);
}
return word;
}
}
public class Flyweight1{
public static void main(String args[]){
Word w1 = WordPool.getWord("刘德华","001");
Word w2 = WordPool.getWord("张学友","002");
Word w3 = WordPool.getWord("刘德华","001");
Word w4 = WordPool.getWord("刘德华","001");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?