public function getFromFabric(FlyweightFactory $factory) { $flyweghts = array(); $flyweghts[1] = $factory->getFlyweight(1); $flyweghts[2] = $factory->getFlyweight(1); $flyweghts[3] = $factory->getFlyweight(2); return $flyweghts; }
public static function main() { $flyweightFactory = new FlyweightFactory(); $flyweight = $flyweightFactory->getFlyweight("state A"); $flyweight->operation("state x"); $flyweight = $flyweightFactory->getFlyweight("state B"); $flyweight->operation("state y"); //复合对象 $uFlyweight = $flyweightFactory->getFlyweight(array("state A", "state B")); $uFlyweight->operation("state z"); }
public function testFlyweight() { $expect = "My name: Jeck"; $factory = new FlyweightFactory(); $flyweight_1 = $factory->getFlyweight(1); $flyweight_2 = $factory->getFlyweight(2); $flyweight_3 = $factory->getFlyweight(3); $flyweight_1->setName("Jim"); $flyweight_2->setName("Jeck"); $flyweight_3->setName("Jill"); $flyweight_n = $factory->getFlyweight(2); $result = $flyweight_n->getName(); $this->assertEquals($result, $expect); }
public static function main() { $flyweightFactory = new FlyweightFactory(); $flyweight = $flyweightFactory->getFlyweight("state A"); $flyweight->operation("state X"); $flyweight = $flyweightFactory->getFlyweight("state B"); $flyweight->operation("state B"); $flyweight1 = $flyweightFactory->getFlyweight("state B"); var_dump($flyweight == $flyweight1); /** * unshared objs */ $uflyweight = new UnsharedConcreteFlyweight('state A'); $uflyweight->operation('state Y'); }
static function main() { $flyweightFactory = new FlyweightFactory(); $flyweight = $flyweightFactory->getFlyweight('state A'); $flyweight->operation('other state A'); $flyweight = $flyweightFactory->getFlyweight('state B'); $flyweight->operation('other state B'); /* 复合对象*/ $uflyweight = $flyweightFactory->getFlyweight(array('state A', 'state B')); $uflyweight->operation('other state A'); }