1.意图
运用共享技术有效的支持大量细粒度的对象
2.参与者
Flyweight - 描述一个接口flyweight可以接受并作用于外部状态
ConcreteFlyweight - 实现flyweight接口,并为内部状态增加存储空间。
FlyweightFactory - 创建对象确保合理的共享对象
3.结构
4.代码:
public interface Flyweight {
Character getValue();
int getTimes();
void setTimes(int times);
}
public class ConcreteFlyweight implements Flyweight {
private Character c ;
private int times;
ConcreteFlyweight(Character c){
this.c=c;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
@Override
public Character getValue() {
return c;
}
}
public class CharacterObject {
int rowNUm;
int cloumnNum;
ConcreteFlyweight concreteFlyweight;
CharacterObject(Character c,int rowNUm,int cloumnNum){
this.rowNUm = rowNUm;
this.cloumnNum = cloumnNum;
concreteFlyweight = (ConcreteFlyweight) FlyweightFactory.instance().getFlyweight(c);
concreteFlyweight.setTimes(concreteFlyweight.getTimes()+1);
}
public ConcreteFlyweight getValue() {
return concreteFlyweight;
}
public int getRowNum() {
return rowNUm;
}
public int getCloumnNum() {
return cloumnNum;
}
}
public class FlyweightFactory {
private final static FlyweightFactory flyweightFactory = new FlyweightFactory();
private Map<Character,Flyweight> flyweightMap = new HashMap<Character,Flyweight>();
public static FlyweightFactory instance() {
return flyweightFactory;
}
private FlyweightFactory() {
}
public Flyweight getFlyweight(Character c){
Flyweight o = flyweightMap.get(c);
if(o == null ){
o = new ConcreteFlyweight(c);
flyweightMap.put(c, o);
}
return o;
}
public void __Time(Flyweight c ){
c.setTimes(c.getTimes()-1);
}
public Iterator<Flyweight> createIterator(){
return flyweightMap.values().iterator();
}
}
public class Article {
List<List<CharacterObject>> article;
Article() {
article = new LinkedList<List<CharacterObject>>();
}
@SuppressWarnings("unused")
public void add(char c, int row) {
List<CharacterObject> list;
int rows = article.size();
if (rows <= row) {
list = new LinkedList<CharacterObject>();
} else {
list = article.get(row);
}
int clounmn = list.size();
CharacterObject characterObject = new CharacterObject(c, row, clounmn);
list.add(characterObject);
if (rows <= row) {
article.add(row, list);
} else {
article.set(row, list);
}
}
public void remove(int row, int clounmn) {
if (article.size() < row) {
throw new RuntimeException("不存在当前行,请确定后再删除!");
}
List<CharacterObject> list = article.get(row);
if (list.size() < clounmn) {
throw new RuntimeException("不存在当前列,请确定后再删除!");
}
ConcreteFlyweight f = list.get(clounmn).getValue();
FlyweightFactory.instance().__Time(f);
list.remove(clounmn);
}
public Iterator<List<CharacterObject>> Iterator(){
return article.iterator();
}
}
public class ArticleDirector {
public static Article getArticle(String articleStr){
char[] artiClechar = articleStr.toCharArray();
Article article = new Article();
int row = 0;
String flag = "/n";
char pre = 0;
for(char c:artiClechar){
if(c == '/'){
pre = c;
continue;
}else if('/'==pre && c == 'n'){
row ++ ;
pre = 0;
continue;
}else if(pre == '/'){
article.add(pre, row);
article.add(c, row);
pre = 0;
}else{
article.add(c, row);
pre = 0;
}
}
return article;
}
public static void print(Article a) {
Iterator<List<CharacterObject>> it = a.Iterator();
while(it.hasNext()){
Iterator<CharacterObject> list = it.next().iterator();
while(list.hasNext()){
CharacterObject f = list.next();
System.out.print(f.getValue().getValue());
}
System.out.println();
}
}
}
public class Client {
public static void main(String[] args) {
Article a = ArticleDirector.getArticle("adsdfadfs adfas adewfa /n dfadfaasdfwfqt /nreq erq eqreq/n");
ArticleDirector.print(a);
Iterator<Flyweight> it = FlyweightFactory.instance().createIterator();
while(it.hasNext()){
Flyweight c = it.next();
System.out.println(c.getValue()+":"+c.getTimes());
}
a.remove(0, 0);
ArticleDirector.print(a);
it = FlyweightFactory.instance().createIterator();
while(it.hasNext()){
Flyweight c = it.next();
System.out.println(c.getValue()+":"+c.getTimes());
}
}
}
5.测试结果