PHP design mode FlyWeight (_php mode) skills

Source: Internet
Author: User
In the mode of "Flyweight pattern", I would like to thank the strongman who translated Flyweight pattern into the meta mode, because the word clearly shows the way in which the pattern is used; If the translation becomes a feather-grade model or a fly-level model, etc., Although implicit in the use of this pattern to achieve the purpose, but still do not grasp the key to this model.

The privilege mode is defined as: a share is used to avoid the overhead of having a large number of identical content objects. The most common and intuitive of this overhead is the loss of memory. The privilege mode supports a large number of fine-grained objects efficiently in a shared way.

Sharing this core concept is embodied in both the name and the definition, so how do you realize sharing? To know that each thing is different, but there is a certain commonality, if only the same things can be shared, then the mode of identity can be said to be not feasible, so we should try to share things in common, but also retain its personality. In order to do this, the element pattern distinguishes the intrinsic state from the state of the outer Yun. The intrinsic state is the commonness, and the outer state is individuality.

Note: The shared object must be immutable, otherwise the change will be total (except for this requirement).

The intrinsic state is stored in the inner element and will not vary with the environment, and it can be shared; the outer state is not shared and changes with the environment, so the external state is maintained by the client (because the changes in the environment are caused by the client). In each specific environment, the client passes the outside state to the element to create a different object.

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

Copy Code code as follows:

<?php
/**
* Enjoy meta mode
*
* Use the 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 setartist ($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;
return $objArtist;
}
}
}
$objArtistFactory = new Artistfactory ();
$objCD 1 = new CD ();
$objCD 1->settitle ("Title1");
$objCD 1->setartist ($objArtistFactory->getartist (' Artist1 '));
$objCD 2 = new CD ();
$objCD 2->settitle ("Title2");
$objCD 2->setartist ($objArtistFactory->getartist (' Artist2 '));
$objCD 3 = new CD ();
$objCD 3->settitle ("Title3");
$objCD 3->setartist ($objArtistFactory->getartist (' Artist1 '));

There are three points to the essence of the privilege pattern:

    1. Fine-grained objects that are heavily used by the system, granularity to have how much, the amount of how much to see the JDK used in the use of the mode to know that the JDK, integer,character,string and so on are used in the mode, they are the most basic data types, is not a fine, They often participate in the operation, is not a large number.
    2. The intrinsic attribute/state and the external attribute/state of the object are divided; the so-called intrinsic State, is the existence of the object of the internal, not with the environment changes in the state, there is a netizen said very well, that is, no difference in the state, that is, after taking out the attributes of the same class of objects have no difference in the intrinsic state is the object of the yuan God As long as the yuan God is no difference, then there is no difference between the objects, but also only these no region other God can be shared, I think this is flyweight is translated into the reason for the yuan. Outside the state is specified by the client, will change with the state of the environment; For an integer, his intrinsic attribute is actually his value (of course it has no external attribute);
    3. Use a factory to control the creation of the element; Because the object can not be created by the client, otherwise there is no meaning. The factory usually provides a caching mechanism for saving already created dollars.

Although object-oriented solves the problem of abstraction, but for a real running software system, we also need to consider the cost of object-oriented, which is the problem of object-oriented cost. The object sharing method is used to reduce the number of objects in the system, thus reducing the memory pressure brought by fine-grained objects to the system.

The

Usage pattern is not commonly used in general project development, but is often applied to the development of the underlying system to solve the system's performance problems. The type of string in Java and. NET is the use of the privilege mode. If you have created a string object S1 in Java or. NET, the next time you create the same string s2, the system simply points the S2 reference to the specific object referenced by S1, which enables the same string to be shared in memory. If you create a new string object each time you perform a s1= "ABC" operation, the overhead of the memory can be significant.

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.