php享元模式(flyweight pattern)
2024-09-01 13:07:44
周日一大早,今天要送老婆孩子去火车站,
所以练代码要早点哈。
<?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(); ?>
最新文章
- vue2.0构建淘票票webapp
- 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
- myeclipse 8.5离线安装python插件
- Python复杂多重排序
- Swift 了解(1)
- php&;amp;&;amp;页面静态化
- Java设计模式(六)Adapter适配器模式
- 关于使用Mybatis的使用说明(一)【未完善待更新】
- textCNN原理
- django channle的使用
- Python笔记 【无序】 【二】
- Java基础——Servlet(七)过滤器&;监听器 相关
- python基础学习第三天
- Spring Framework: @RestController vs @Controller
- Github访问慢解决办法
- FastDFS与springBoot集成
- MT【177】三个乘积和
- Java中使用Oracle的客户端 load data和sqlldr命令执行数据导入到数据库中
- eclipse使用git提交项目
- windebug分析高cpu问题
热门文章
- Swagger2.9.2进入API界面报NumberFormatException异常
- 【计算机视觉】ImageNet介绍
- java ImmutableMap使用
- python实现栈结构
- [转帖]spring、springMvc、springBoot和springCloud的联系与区别
- 第十节:Asp.Net Core 配置详解和选项模式
- C++ 01 基础知识点
- 什么是 Shell?
- HDU校赛 | 2019 Multi-University Training Contest 6
- HDU校赛 | 2019 Multi-University Training Contest 2