public class FlyweightTest1 { public static void main(String[] args) { WebSite fx = new WebSite("产品展示"); fx.Use(); WebSite fy = new WebSite("产品展示"); fy.Use(); WebSite fz = new WebSite("产品展示"); fz.Use(); WebSite fl = new WebSite("博客"); fl.Use(); WebSite fm = new WebSite("博客"); fm.Use(); WebSite fn = new WebSite("博客"); fn.Use(); System.out.println(); } } class WebSite { private String name = ""; public WebSite(String name) { this.name = name; } public void Use() { System.out.println("网站分类:"+name); } } import java.util.*; public class FlyweightTest2 { public static void main(String[] args) { int extrinsicstate = 22; FlyweightFactory f = new FlyweightFactory(); Flyweight fx = f.GetFlyweight("X"); fx.Operation(--extrinsicstate); Flyweight fy = f.GetFlyweight("Y"); fx.Operation(--extrinsicstate); Flyweight fz = f.GetFlyweight("Z"); fx.Operation(--extrinsicstate); UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight(); uf.Operation(--extrinsicstate); System.out.println(); } } abstract class Flyweight { abstract public void Operation(int extrinsicstate); } class ConcreteFlyweight extends Flyweight { public void Operation(int extrinsicstate) { System.out.println("具体 Flyweight:" + extrinsicstate); } } class UnsharedConcreteFlyweight extends Flyweight { public void Operation(int extrinsicstate) { System.out.println("不共享的具体 Flyweight:" + extrinsicstate); } } class FlyweightFactory { private Hashtable flyweights = new Hashtable(); public FlyweightFactory() { flyweights.put("X",new ConcreteFlyweight()); flyweights.put("Y",new ConcreteFlyweight()); flyweights.put("Z",new ConcreteFlyweight()); } public Flyweight GetFlyweight(String key) { return (Flyweight)(flyweights.get(key)); } } import java.util.*; public class FlyweightTest3 { public static void main(String[] args) { WebSiteFactory f = new WebSiteFactory(); WebSite fx = f.GetWebSiteCategory("产品展示"); fx.Use(); WebSite fy = f.GetWebSiteCategory("产品展示"); fy.Use(); WebSite fz = f.GetWebSiteCategory("产品展示"); fz.Use(); WebSite fl = f.GetWebSiteCategory("博客"); fl.Use(); WebSite fm = f.GetWebSiteCategory("博客"); fm.Use(); WebSite fn = f.GetWebSiteCategory("博客"); fn.Use(); System.out.println("网站分类总数为:"+f.GetWibSiteCount()); System.out.println(); } } abstract class WebSite { abstract public void Use(); } class ConcreteWebSite extends WebSite { private String name = ""; public ConcreteWebSite(String name) { this.name = name; } public void Use() { System.out.println("网站分类:" + name); } } class WebSiteFactory { private Hashtable flyweights = new Hashtable(); public WebSite GetWebSiteCategory(String key) { if(!flyweights.containsKey(key)) flyweights.put(key,new ConcreteWebSite(key)); return (WebSite)(flyweights.get(key)); } public int GetWibSiteCount() { return flyweights.size(); } } import java.util.*; public class FlyweightTest4 { public static void main(String[] args) { WebSiteFactory f = new WebSiteFactory(); WebSite fx = f.GetWebSiteCategory("产品展示"); fx.Use(new User("小蔡")); WebSite fy = f.GetWebSiteCategory("产品展示"); fy.Use(new User("大鸟")); WebSite fz = f.GetWebSiteCategory("产品展示"); fz.Use(new User("娇娇")); WebSite fl = f.GetWebSiteCategory("博客"); fl.Use(new User("老顽童")); WebSite fm = f.GetWebSiteCategory("博客"); fm.Use(new User("桃谷六仙")); WebSite fn = f.GetWebSiteCategory("博客"); fn.Use(new User("南海恶神")); System.out.println("网站分类总数为:"+f.GetWibSiteCount()); System.out.println(); } } class User { private String name; public User(String name) { this.name = name; } public String getname() { return name; } } abstract class WebSite { abstract public void Use(User user); } class ConcreteWebSite extends WebSite { private String name = ""; public ConcreteWebSite(String name) { this.name = name; } public void Use(User user) { System.out.println("网站分类:" + name + "用户:" + user.getname()); } } class WebSiteFactory { private Hashtable flyweights = new Hashtable(); public WebSite GetWebSiteCategory(String key) { if(!flyweights.containsKey(key)) flyweights.put(key,new ConcreteWebSite(key)); return (WebSite)(flyweights.get(key)); } public int GetWibSiteCount() { return flyweights.size(); } }