📄 flyweight1.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -