コード例 #1
0
ファイル: Client.php プロジェクト: henryf/designPatterns
 public function getFromFabric(FlyweightFactory $factory)
 {
     $flyweghts = array();
     $flyweghts[1] = $factory->getFlyweight(1);
     $flyweghts[2] = $factory->getFlyweight(1);
     $flyweghts[3] = $factory->getFlyweight(2);
     return $flyweghts;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
ファイル: cflyweight.php プロジェクト: luisOO/design-patterns
 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");
 }
コード例 #4
0
 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);
 }
コード例 #5
0
ファイル: flyweight.php プロジェクト: luisOO/design-patterns
 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');
 }
コード例 #6
0
 public static function getInstance()
 {
     if (self::$instance === null) {
         self::$instance = new self();
     }
     return self::$instance;
 }
コード例 #7
0
 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;
 }
コード例 #8
0
ファイル: Flyweight.php プロジェクト: manachyn/trainings
    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('');
コード例 #9
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Incorrect theme identification key
  */
 public function testNegativeCreate()
 {
     $this->factory->create(null);
 }
コード例 #10
0
ファイル: flyweight.php プロジェクト: EdenChan/Instances
 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');
 }