/** @param args the command line arguments */ public static void main(String[] args) { FlyweightFactory ff = new FlyweightFactory(); String patentes[] = {"ARG123", "CHL123", "PER123"}; String colores[] = {"Negro", "Azul", "Rojo"}; String duenios[] = {"Carlos", "Pedro", "Julio"}; ff.listarAutos(colores, patentes, duenios); }
/** Creates a new instance of FlyweightPattern */ public FlyweightPattern() { fly1 = factory.getFlyWeight("Google"); fly2 = factory.getFlyWeight("Qutr"); fly3 = factory.getFlyWeight("Google"); fly4 = factory.getFlyWeight("Google"); fly5 = factory.getFlyWeight("Google"); fly6 = factory.getFlyWeight("Google"); }
/** @param args */ public static void main(String[] args) { int i = 20; FlyweightFactory factory = new FlyweightFactory(); Flyweight flyweight1 = factory.getFlyweight("X"); flyweight1.operate(--i); Flyweight flyweight2 = factory.getFlyweight("Y"); flyweight2.operate(--i); Flyweight flyweight3 = factory.getFlyweight("Z"); flyweight3.operate(--i); }
public void Operation(String key, boolean shared) { Flyweight flyweight; if (shared) { flyweight = FlyweightFactory.getFlyweight(key); } else { flyweight = new UnsharedConcreteFlyweight(key); } flyweight.Operation("changed extrinsicly"); }
public void showFlyweight() { fly1.operation(); fly2.operation(); fly3.operation(); fly4.operation(); fly5.operation(); fly6.operation(); int objSize = factory.getFlyweightSize(); System.out.println("objSize = " + objSize); }
public static void main(String[] args) { // 单纯的享元模式 FlyweightFactory factory = new FlyweightFactory(); InterFlyweight fly = factory.factory(new Character('a')); fly.operation("First Call"); fly = factory.factory(new Character('b')); fly.operation("Second Call"); fly = factory.factory(new Character('a')); fly.operation("Third Call"); Utils.showStr("-----------------------------------------------------"); // 复合的享元模式 List<Character> characters = new ArrayList<Character>(); characters.add('a'); characters.add('b'); characters.add('c'); characters.add('a'); characters.add('b'); FlyweightFactory factory1 = new FlyweightFactory(); InterFlyweight interFlyweight = factory1.factory(characters); InterFlyweight interFlyweight1 = factory1.factory(characters); // 一个复合享元对象的所有单纯享元对象元素的外蕴状态都是与复合享元对象的外蕴状态相等的。即外运状态都等于Composite Call。 // 一个复合享元对象所含有的单纯享元对象的内蕴状态一般是不相等的。即内蕴状态分别为b、c、a。 interFlyweight.operation("Composite Call"); // 复合享元对象是不能共享的。即使用相同的对象compositeState通过工厂分别两次创建出的对象不是同一个对象。 Utils.showStr("复合享元模式是否可以共享对象: " + (interFlyweight == interFlyweight1)); Character state = 'a'; InterFlyweight fly1 = factory1.factory(state); InterFlyweight fly2 = factory1.factory(state); // 单纯享元对象是可以共享的。即使用相同的对象state通过工厂分别两次创建出的对象是同一个对象。 Utils.showStr("单纯享元模式是否可以共享对象: " + (fly1 == fly2)); }