PHP design mode--Enjoy meta mode

Source: Internet
Author: User

Statement: This series of blog reference "Big Talk design mode", author Geoscience.

enjoy meta mode Use Shared Objects , used to reduce memory usage as much as possible and share information to as many similar objects as possible; it is suitable for a large number of objects that are simply duplicated to use an unacceptable amount of memory. Usually part of the state in an object can be shared. It is common practice to put them in an external data structure and pass them on to the element when it is needed.


UML Class Diagrams:


Role Analysis:

enjoy meta factory role (fwfactory): Create and manage blogmodel objects.

all the specific metadata parent interface roles ( Bolgmodel ): Accept and act with external states.

The specific enjoy meta role ( JobsBlog ): specific change points, to increase the storage space for internal objects.


Code implementation:

<?php/** * Created by Phpstorm. * user:lyl * DATE:2015/5/16 * time:12:00 *//** all enjoy meta parent interface role * Interface Iblogmodel */interface iblogmodel{function Show    Time (); function Showcolor ();} /** Jobs ' Blog template * Class jobsblog */class JobsBlog implements iblogmodel{function ShowTime () {echo "New York Time: 5 O'Clock Full <    Br/> "; } function Showcolor () {echo "<div style= ' color: #006600; Height:30;width:30;background-color: #898989; '    >Jobs</div> "; }}/** Lei June Blog Template * Class leijunblog */class Leijunblog implements iblogmodel{function ShowTime () {echo "Beijing time: 17 points    Whole <br/> "; } function Showcolor () {echo "<div style= ' color: #c7254e; Height:30;width:100;background-color: #898989; '    > Lei June </div> ";    }}/** Blog Template Factory * Class blogfactory */class blogfactory{private $model =array ();            function Getblogmodel ($name) {if (Isset ($this->model[$name])) {echo "I am <br/> in cache"; return $this->model[$naMe];                } else {try {echo "I am the newly created <br/>";                $class =new Reflectionclass ($name);                $this->model[$name]= $class->newinstance ();            return $this->model[$name]; } catch (Reflectionexception $e) {echo "<span style= ' color: #ff0000; ' > You asked for the object I can't create Oh.                </span><br/> ";            return null; }        }    }}

Client calling Code:

Header ("Content-type:text/html;charset=utf-8");//------------------------Façade mode test code------------------require_ Once "./flyweight/flyweight.php"; $factory =new blogfactory (), $jobs = $factory->getblogmodel ("JobsBlog"); if ($jobs ) {    $jobs->showtime ();    $jobs->showcolor ();} $jobs 1= $factory->getblogmodel ("JobsBlog"), if ($jobs 1) {    $jobs 1->showtime ();    $jobs 1->showcolor ();} $leijun = $factory->getblogmodel ("Leijunblog"), if ($leijun) {    $leijun->showtime ();    $leijun->showcolor ();} $leijun 1= $factory->getblogmodel ("Leijunblog"), if ($leijun 1) {    $leijun 1->showtime ();    $leijun 1->showcolor ();} $aFanda = $factory->getblogmodel ("Afanda"), if ($aFanda) {    $aFanda->showtime ();    $aFanda->showcolor ();}

Advantages:

1 . save memory by reducing the number of object instances at run time

2. Centrally manage the state of many "virtual" objects

Disadvantages:

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

Applicable scenarios:

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


PHP Object-oriented design pattern


PHP design mode--Enjoy meta 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: info-contact@alibabacloud.com 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.