周日一大早,今天要送老婆孩子去火车站,
所以练代码要早点哈。
<?php /* The flyweight pattern is about performance and resource reduction, sharing as much data as possible between similar objects. What this means is that instances of a class which are identical are shared in an implementation. This works best when a large number of same class instances are expected to be created. */ interface Shape{ public function draw(); } class Circle implements Shape { private $color; private $radius; public function __construct($color) { $this->color = $color; } public function draw() { echo sprintf('Color %s, radius %s <br/>', $this->color, $this->radius); } public function setRadius($radius) { $this->radius = $radius; } } class ShapeFactory { private $circleMap; public function getCircle($color) { if (!isset($this->cicrleMap[$color])) { $circle = new Circle($color); $this->circleMap[$color] = $circle; } return $this->circleMap[$color]; } } $shapeFactory = new ShapeFactory(); $circle = $shapeFactory->getCircle('yellow'); $circle->setRadius(10); $circle->draw(); $shapeFactory = new ShapeFactory(); $circle = $shapeFactory->getCircle('orange'); $circle->setRadius(15); $circle->draw(); $shapeFactory = new ShapeFactory(); $circle = $shapeFactory->getCircle('yellow'); $circle->setRadius(20); $circle->draw(); ?>