PHP design pattern-enjoy the metadata Pattern

Source: Internet
Author: User

PHP design pattern-enjoy the metadata Pattern

Disclaimer: Cheng Jie, author of this series of blog references "big talk design mode.

The shared object mode is used to minimize memory usage and share information with as many similar objects as possible. It is suitable for the use of a large number of objects that are unacceptable due to duplication. Some statuses of objects are usually shared. A common practice is to put them in an external data structure and pass them to the user when necessary.


UML class diagram:


Role analysis:

FWFactory: Creates and manages BlogModel objects.

All the specific shared element parent interface roles (BolgModel): Accept the role and external status.

JobsBlog: a change that adds storage space for internal objects.


Code implementation:


 ;} Function showColor () {echo
Jobs;}/** leijun blog template * Class LeiJunBlog */class LeiJunBlog implements IBlogModel {function showTime () {echo Beijing Time: 17 o'clock
;} Function showColor () {echo leijun;}/** blog template factory * Class BlogFactory */class BlogFactory {private $ model = array (); function getBlogModel ($ name) {if (isset ($ this-> model [$ name]) {echo I am in the cache
; Return $ this-> model [$ name];} else {try {echo I created
; $ Class = new ReflectionClass ($ name); $ this-> model [$ name] = $ class-> newInstance (); return $ this-> model [$ name];} catch (ReflectionException $ e) {echo I cannot create the object you requested.
; Return null ;}}}}
Client call code:



Header (Content-Type: text/html; charset = UTF-8); // ------------------------ test code in facade mode ------------------ require_once. /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. Reduce the number of running object instances and save memory

2. centrally manage the statuses of many "virtual" Objects


Once implemented, a single logical implementation cannot have independent and different behaviors.


Applicable scenarios:

When a class has many instances and these instances can be controlled by the same method, we can use the metadata mode.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.