标签:java oop struts1 享元模式 flyweight
享元模式是指"运用共享技术有效地支持大量细粒度的对象"package model.flyweight1; /* * 抽象享元角色 */ public interface Share { public String work(String fettle); } package model.flyweight1; /* * 具体享元角色 * 属于conreteFlyweight角色 */ public class ConcerteShare implements Share { //内藴状态 private Character internalFettle = null; @Override public String work(String fettle) { String a = "internalFettle="+internalFettle+",exterior Fettle="+fettle; return a; } //将外藴状态传进去 public ConcerteShare(Character fettle) { this.internalFettle = fettle; } } package model.flyweight1; import java.util.Hashtable; /* * 享元工厂角色类 * 属于FlyweightFactory */ public class ShareFactory { private Hashtable matter = new Hashtable(); private Share share; public ShareFactory(){ } public Share factory(Character fettle){ if(matter.containsKey(fettle)){ return (Share) matter.get(fettle); }else{ share = new ConcerteShare(fettle); matter.put(fettle, share); return share; } } } package model.flyweight1; public class Client { public static void main(String[] args) { ShareFactory factory = new ShareFactory(); Share share =factory.factory(new Character('中')); System.out.println("字体:"+share.work("黑体")); System.out.println("字体:"+share.work("宋体")); share =factory.factory(new Character('外')); System.out.println("字体:"+share.work("仿体")); } }
package model.flyweight2; /* * 抽象享元角色 */ public interface Share { public String work(String fettle); } package model.flyweight2; /* * 具体享元角色 * 属于conreteFlyweight角色 */ public class ConcerteShare implements Share { //内藴状态 private Character internalFettle = null; @Override public String work(String fettle) { String a = "internalFettle="+internalFettle+",exterior Fettle="+fettle; return a; } //将外藴状态传进去 public ConcerteShare(Character fettle) { this.internalFettle = fettle; } } package model.flyweight2; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; /* *复合享元角色 *即UnsharableFlyweight角色 */ public class ConcreteCompositeShare implements Share { private Hashtable matter = new Hashtable(10); private Share share; //传入外藴状态 @Override public String work(String fettle) { Share share = null; for (Iterator it=matter.entrySet().iterator(); it.hasNext();) { Map.Entry e = (Map.Entry)it.next(); share = (Share) e.getValue(); share.work(fettle); } return fettle; } public ConcreteCompositeShare(){ } //添加一个新的单纯享元对象到聚合中 public void add(Character key,Share share){ matter.put(key, share); } } package model.flyweight2; import java.util.Hashtable; /* * 享元工厂角色类 * 属于FlyweightFactory */ public class ShareFactory { private Hashtable matter = new Hashtable(); private Share share; public ShareFactory(){ } //单享元工厂方法,所需状态以参数形式传进去 public Share factory(Character fettle){ if(matter.containsKey(fettle)){ return (Share) matter.get(fettle); }else{ share = new ConcerteShare(fettle); matter.put(fettle, share); return share; } } //享元工厂方法,运用String参数传入状态 public Share factory(String compositeState){ ConcreteCompositeShare compositeShare = new ConcreteCompositeShare(); int length = compositeState.length(); Character fettle = null; for (int i = 0; i < length; i++) { fettle = new Character(compositeState.charAt(i)); System.out.println("factory("+fettle+")"); compositeShare.add(fettle, this.factory(fettle)); } return compositeShare; } } package model.flyweight2; public class Client { public static void main(String[] args) { ShareFactory factory = new ShareFactory(); Share share = factory.factory("东南西"); System.out.println("内藴东南西,外藴均为字体:"+share.work("仿体")); } }
标签:java oop struts1 享元模式 flyweight
原文地址:http://blog.csdn.net/qqmaye/article/details/44041177