周日一大早,今天要送老婆孩子去火车站,

所以练代码要早点哈。

<?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();
?>

最新文章

  1. vue2.0构建淘票票webapp
  2. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
  3. myeclipse 8.5离线安装python插件
  4. Python复杂多重排序
  5. Swift 了解(1)
  6. php&amp;amp;&amp;amp;页面静态化
  7. Java设计模式(六)Adapter适配器模式
  8. 关于使用Mybatis的使用说明(一)【未完善待更新】
  9. textCNN原理
  10. django channle的使用
  11. Python笔记 【无序】 【二】
  12. Java基础——Servlet(七)过滤器&amp;监听器 相关
  13. python基础学习第三天
  14. Spring Framework: @RestController vs @Controller
  15. Github访问慢解决办法
  16. FastDFS与springBoot集成
  17. MT【177】三个乘积和
  18. Java中使用Oracle的客户端 load data和sqlldr命令执行数据导入到数据库中
  19. eclipse使用git提交项目
  20. windebug分析高cpu问题

热门文章

  1. Swagger2.9.2进入API界面报NumberFormatException异常
  2. 【计算机视觉】ImageNet介绍
  3. java ImmutableMap使用
  4. python实现栈结构
  5. [转帖]spring、springMvc、springBoot和springCloud的联系与区别
  6. 第十节:Asp.Net Core 配置详解和选项模式
  7. C++ 01 基础知识点
  8. 什么是 Shell?
  9. HDU校赛 | 2019 Multi-University Training Contest 6
  10. HDU校赛 | 2019 Multi-University Training Contest 2