Php design pattern FlyWeight (metadata pattern) _ PHP Tutorial

Source: Internet
Author: User
Php design mode: FlyWeight ). The "FlyweightPattern" is an English example of the "enjoy meta-pattern" model. I am very grateful to the strong man who translates the "FlyweightPattern" into the "enjoy meta-pattern" model, because the term uses this Pattern to clearly express the "Flyweight Pattern" in English, I am very grateful to the strong man who translates the "Flyweight Pattern" into the "enjoy yuan" Pattern, this word clearly expresses the method used in this mode. if it is translated into a feather or fly mode, it can implicitly show the purpose of using this mode, however, the key to this mode is still not grasped.

The metadata mode is defined as follows: use a shared object to avoid overhead of a large number of objects with the same content. The most common and intuitive overhead is the memory loss. The metadata mode efficiently supports a large number of fine-grained objects in a shared manner.

Sharing is a core concept in both the name and definition. how can we achieve sharing? You must know that everything is different, but there are some commonalities. if only identical things can be shared, then the metadata-sharing mode can be said to be unfeasible; therefore, we should try our best to share the commonalities of things while retaining its personality. In order to achieve this, the metadata mode distinguishes between the internal and external states. The inherent state is the commonality, and the external state is the personality.

Note: the shared object must be unchangeable. Otherwise, the shared object will be changed in a single change (unless otherwise required ).

The internal state is stored in the shared object and can be shared without changing the environment. the external state cannot be shared and changed as the environment changes, therefore, the external state is maintained by the client (because the environment changes are caused by the client ). In each specific environment, the client transmits the external status to the object to create different objects.

Let's take a look at the following program to get a rough idea of the metadata sharing mode.

The code is as follows:


/**
* Metadata mode
*
* Use the metadata 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 ."
";
$ 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 ();
$ ObjCD1 = new CD ();
$ ObjCD1-> setTitle ("title1 ");
$ ObjCD1-> setArtist ($ objArtistFactory-> getArtist ('artist1 '));
$ ObjCD2 = new CD ();
$ ObjCD2-> setTitle ("title2 ");
$ ObjCD2-> setArtist ($ objArtistFactory-> getArtist ('artist2 '));
$ ObjCD3 = new CD ();
$ ObjCD3-> setTitle ("title3 ");
$ ObjCD3-> setArtist ($ objArtistFactory-> getArtist ('artist1 '));


The benefits of the meta-mode are as follows:

  1. A large number of fine-grained objects are used by the system. The granularity should be fine and the quantity should be large. you can see the metadata mode used in jdk. in jdk, Integer, Character, string and so on all use the meta-mode. they are all the most basic data types, but they are not very detailed. they are frequently involved in computation, not very large.
  2. Divide the inherent attributes/states and external attributes/States of an object. The so-called internal state refers to the existence of an object and does not change with the environment. one netizen said that it is good, there is no difference in the state, that is, after the external property is removed, there is no difference between the same class of objects, the inherent state of the object is the God of God, as long as there is no difference between the God of God, then the object is no difference, at the same time, there is only one possibility to share these unique gods. I think this is why Flyweight was translated into yuan. The external state is the state that is specified by the client and will change with the environment; for Integer, its internal attribute is actually its value (of course it does not have an external property );
  3. Use a factory to control the creation of the yuan, because the yuan object cannot be created by the client at will, otherwise it will be meaningless. The factory usually provides a Cache mechanism to save the created items.

Although object-oriented solves the abstract problem, we also need to consider the cost of object-oriented for a software system running in practice, the metadata mode solves the problem of object-oriented cost. The object sharing mode reduces the number of objects in the system, thus reducing the memory pressure on the system caused by fine-grained objects.

The metadata mode is not commonly used in general project development. it is often used in the development of the underlying system to solve system performance problems. In Java and. Net, the String type uses the metadata mode. In Java or. NET has already created a string object s1, so the next time you create the same string s2, the system just points the s2 reference to the specific object referenced by s1, this achieves the sharing of the same string in the memory. If a new string object is created every time s1 = "abc" is executed, the memory overhead will be high.

Thank you very much for translating Flyweight Pattern into the strong person in the meta-mode, because the word clearly expresses how this mode is used...

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.