public function getFromFabric(FlyweightFactory $factory) { $flyweghts = array(); $flyweghts[1] = $factory->getFlyweight(1); $flyweghts[2] = $factory->getFlyweight(1); $flyweghts[3] = $factory->getFlyweight(2); return $flyweghts; }
/** * Get theme model by theme path and area code * * @param string $themePath * @param string $areaCode * @return \Magento\Framework\View\Design\ThemeInterface */ public function getThemeModel($themePath, $areaCode) { if ($this->appState->isInstalled()) { $themeModel = $this->flyweightFactory->create($themePath, $areaCode); } else { $themeModel = $this->themeList->getThemeByFullPath($areaCode . '/' . $themePath); } return $themeModel; }
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'); }
public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; }
private function queryUser($user) { $col = array(); foreach (self::$user_data as $data) { $str = explode(",", $data); if ($str[0] == $user) { $fm = FlyweightFactory::getInstance()->getFlyweight($str[1] . ',' . $str[2]); $col[] = $fm; } } return $col; }
private $books = array(); function addBook($book) { $this->books[] = $book; } function showBooks() { $return_string = NULL; foreach ($this->books as $book) { $return_string .= 'title: ' . $book->getAuthor() . ' author: ' . $book->getTitle(); } return $return_string; } } writeln('BEGIN TESTING FLYWEIGHT PATTERN'); $flyweightFactory = new FlyweightFactory(); $flyweightBookShelf1 = new FlyweightBookShelf(); $flyweightBook1 = $flyweightFactory->getBook(1); $flyweightBookShelf1->addBook($flyweightBook1); $flyweightBook2 = $flyweightFactory->getBook(1); $flyweightBookShelf1->addBook($flyweightBook2); writeln('test 1 - show the two books are the same book'); if ($flyweightBook1 === $flyweightBook2) { writeln('1 and 2 are the same'); } else { writeln('1 and 2 are not the same'); } writeln(''); writeln('test 2 - with one book on one self twice'); writeln($flyweightBookShelf1->showBooks()); writeln('');
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Incorrect theme identification key */ public function testNegativeCreate() { $this->factory->create(null); }
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'); }