PHP设计模式享元模式
PHP设计模式——享元模式
声明:本系列博客参考资料《大话设计模式》,作者程杰。
享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件;它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件。通常物件中的部分状态是可以分享。常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。
UML类图:
角色分析:
享元工厂角色(FWFactory):创建并管理BlogModel对象。
所有具体享元父接口角色(BolgModel):接受并作用与外部状态。
具体享元角色(JobsBlog):具体变化点,为内部对象增加储存空间。
代码实现:
<!--?php /** * Created by PhpStorm. * User: LYL * Date: 2015/5/16 * Time: 12:00 */ /**所有享元父接口角色 * Interface IBlogModel */ interface IBlogModel { function showTime(); function showColor(); } /**乔布斯的博客模板 * Class JobsBlog */ class JobsBlog implements IBlogModel { function showTime() { echo 纽约时间:5点整<br/-->; } function showColor() { echo
Jobs
; } } /**雷军博客模板 * Class LeiJunBlog */ class LeiJunBlog implements IBlogModel { function showTime() { echo 北京时间:17点整; } function showColor() { echo
雷军
; } } /**博客模板工厂 * Class BlogFactory */ class BlogFactory { private $model=array(); function getBlogModel($name) { if(isset($this->model[$name])) { echo 我是缓存里的; return $this->model[$name]; } else { try { echo 我是新创建的
; $class=new ReflectionClass($name); $this->model[$name]=$class->newInstance(); return $this->model[$name]; } catch(ReflectionException $e) { echo 你要求的对象我不能创建哦。
; return null; } } } }
客户端调用代码:
header(Content-Type:text/html;charset=utf-8); //------------------------门面模式测试代码------------------ require_once ./Flyweight/Flyweight.php; $factory=new BlogFactory(); $jobs=$factory->getBlogModel(JobsBlog); if($jobs) { $jobs->showTime(); $jobs->showColor(); } $jobs1=$factory->getBlogModel(JobsBlog); if($jobs1) { $jobs1->showTime(); $jobs1->showColor(); } $leijun=$factory->getBlogModel(LeiJunBlog); if($leijun) { $leijun->showTime(); $leijun->showColor(); } $leijun1=$factory->getBlogModel(LeiJunBlog); if($leijun1) { $leijun1->showTime(); $leijun1->showColor(); } $aFanda=$factory->getBlogModel(aFanda); if($aFanda) { $aFanda->showTime(); $aFanda->showColor(); }
优点:
1.减少运行时对象实例的个数,节省内存
2.将许多“虚拟”对象的状态集中管理
缺点:
一旦被实现,单个的逻辑实现将无法拥有独立而不同的行为
适用场景:
当一个类有许多的实例,而这些实例能被同一方法控制的时候,我们就可以使用享元模式。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.
