PHP design mode flyweight (enjoy meta mode)

Source: Internet
Author: User
The use of the technology to effectively support a large number of fine-grained objects, the need for friends can refer to.

Enjoy meta-mode English is called "Flyweight pattern", I am very grateful to the Flyweight pattern translated into the model of the strong man, because the word will use this pattern to express the way out, if the translation into a feather scale mode or fly scale mode, etc., Although it is possible to implicitly demonstrate the use of this pattern to achieve the purpose, but still do not grasp the key to this pattern.

The definition of the enjoy meta-mode is to use a share to avoid the overhead of having a large number of objects with the same content. The most common and intuitive of this overhead is the loss of memory. The enjoy meta-mode efficiently supports a large number of fine-grained objects in a shared manner.

Benefits of the Enjoy meta-mode:

1) The advantage of the meta-mode is that it can greatly reduce the number of objects in memory, allowing the same object or similar objects to save only one copy in memory.

2) The external state of the enjoy meta-mode is relatively independent, and does not affect its internal state, so that the enjoyment meta-object can be shared in different environments.

Disadvantages of the enjoy meta model:

1) Enjoy meta-mode makes the system more complex, need to separate the internal state and external state, which makes the logic of the program complicated.

2) in order for objects to be shared, the enjoy meta-mode requires the state of the object to be externally instantiated, while the read external state makes the run time longer.


has a core concept of sharing in both name and definition, so how do you implement sharing? To know that each thing is different, but there is a certain commonality, if only the exact same thing can be shared, then the enjoyment of the meta-model can be said to be not feasible, so we should try to share the commonality of things, but also retain its personality. In order to do this, the intrinsic state and the outer state are distinguished in the meta-mode. The intrinsic state is the generality, the outer state is the individuality.

Note: The shared object must be immutable, or the change will change completely (with the exception of this requirement). The intrinsic state of

is stored in the inside of the element, and does not vary depending on the environment, is shareable, the outer state is not shared, it changes with the environment, so the external state is maintained by the client (because changes in the environment are caused by the client). In each specific environment, the client passes the external state to the privilege, creating different objects.

Take a look at the following program, and probably understand the following meta-mode.

<?php/** * Enjoy meta-mode * * Use the enjoy meta-technology to effectively support a large number of fine-grained objects */class CD {private $_title = null; private $_artist = null; Public function Settitle ($title) {$this->_title = $title;} public Function GetTitle () {return $this->_title;} public function S Etartist ($artist) {$this->_artist = $artist;} public Function Getartist ($artist) {return $this->_artist;}} Class Artist {private $_name; public function construct ($name) {echo "construct". $name. " <br/> "; $this->_name = $name; } public Function GetName () {return $this->_name;}} Class Artistfactory {Private $_artists = array (), Public function getartist ($name) {if (Isset ($this->_artists[$name]) {return $this->_artists[$name];} else {$objArtist = new Artist ($name); $this->_artists[$name] = $objArtist; ret Urn $objArtist; }}} $objArtistFactory = new Artistfactory (); $objCD 1 = new CD (); $objCD 1->settitle ("Title1"); $objCD 1->setartist ($objArtistFactory->getartist (' Artist1 ')); $objCD 2 = new CD (); $objCD2->settitle ("Title2"); $objCD 2->setartist ($objArtistFactory->getartist (' Artist2 ')); $objCD 3 = new CD (); $objCD 3->settitle ("Title3"); $objCD 3->setartist ($objArtistFactory->getartist (' Artist1 '));

The essentials of the enjoy meta-mode are three points:

    1. By the system used in a large number of fine-grained objects, granularity to be how much, how much, to see the JDK used in the use of the meta-mode to know, JDK, integer,character,string, etc. are used to enjoy the meta-model, they are the most basic data type, not very thin, Their frequent involvement in computing is not a big deal.

    2. Dividing the intrinsic properties/state of the object and the outer-yun attribute/state; the so-called intrinsic State, is the existence of the object of the internal, not with the state of the environment changes, there is a netizen said very well, is the state of no difference, that is, take off the external properties of the same class of objects without distinguishing the intrinsic state of the As long as the yuan God is no different, then the object will be no difference, but also only these no region of the meta-God can be shared, I think this is flyweight was translated into the reason for the yuan. The external state is specified by the client and will change with the environment. For an integer, his intrinsic attribute is actually his value (and of course it has no external properties);

    3. Use a factory to control the creation of the element of enjoyment, because the object cannot be created by the client at will, otherwise it is meaningless. The factory usually provides a caching mechanism to save the already created benefits.

Object-oriented is a good solution to the problem of abstraction, but for an actual running software system, we also need to consider the object-oriented cost problem, the use of the meta-model to solve the object-oriented cost problem. The use of object sharing reduces the number of objects in the system, thus reducing the memory pressure on the system from fine-grained objects.

The mode of sharing is not commonly used in general project development, but is often applied to the development of the bottom of the system in order to solve the system performance problem. The string type in Java and. NET is the use of the enjoy meta pattern. If a string object S1 has already been created in Java or. NET, then the next time you create the same string s2, the system simply points the S2 reference to the specific object that the S1 refers to, which implements the sharing of the same string in memory. If you create a new string object each time you perform a s1= "ABC" operation, the memory overhead will be significant.

Related Article

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.