1. 程式人生 > >flyweight - 物件結構型模式

flyweight - 物件結構型模式

    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.測試結果