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.
Sharing is a core concept in both name and definition, so how do you share it? 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 is stored in the inside of the element, and will not change with the environment, it can be shared, the outer state is not shared, it changes with the environment change, so the external state is maintained by the client (because the environment change is 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 procedure, probably understand the next to enjoy meta-mode.
Copy the Code code as follows:
/**
* Enjoy meta mode
*
* Effective support for a large number of fine-grained objects with the use of the sharing technology
*/
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. "
";
$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 '));
The essentials of the enjoy meta-mode are three points:
- 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.
- 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);
- 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.
The above describes the PHP design mode FlyWeight to enjoy the meta-mode, including the content, I hope that the PHP tutorial interested in a friend helpful.