Ejemplo n.º 1
0
 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');
 }
Ejemplo n.º 2
0
 public function getFlyweight($state)
 {
     if (is_array($state)) {
         //复合模式
         $uFlyweight = new UnsharedConcreteFlyweight();
         foreach ($state as $row) {
             $uFlyweight->add($row, $this->getFlyweight($row));
         }
         return $uFlyweight;
     } else {
         if (is_string($state)) {
             if (isset($this->_flyweights[$state])) {
                 return $this->_flyweights[$state];
             } else {
                 return $this->_flyweights[$state] = new ConcreteFlyweight($state);
             }
         } else {
             return null;
         }
     }
 }