Enjoy meta mode and PHP implementation

Source: Internet
Author: User

Enjoy meta mode:
Flyweight Pattern: Use sharing technology to effectively support the reuse of a large number of fine-grained objects. The system uses only a small number of objects, and these objects are very similar, the state changes very small, you can achieve multiple reuse of objects. Because the enjoy meta mode requires that the object that can be shared must be a fine-grained object, it is also known as lightweight mode, which is an object-structured pattern.

Mode motive:
Object-oriented technology solves some of the flexibility or scalability problems, but in many cases you need to increase the number of classes and objects in the system. When the number of objects is too large, it will lead to high operating costs, resulting in performance degradation and so on.
The mode of enjoyment was born to solve this kind of problem. The enjoy meta-mode enables the reuse of identical or similar objects through shared technology.
The same content that can be shared in the enjoy meta-mode is called the internal state (intrinsicstate), while those that require an external environment to be set up that cannot be shared are called external states (extrinsic state), because of the distinction between internal and external states, So you can set different external states so that the same objects can have some different characteristics, and the same internal state can be shared.
The Factory mode is typically present in the enjoy meta-mode, and it is necessary to create a Flyweight to maintain a pool of privileges to store the shared meta objects with the same internal state.
The internal state of the object is shared in the enjoy meta-mode, and the external state needs to be set through the environment. In practice, the internal state of being able to share is limited, so the enjoyment meta-object is generally designed to be a smaller object, which contains fewer internal states, which are also known as fine-grained objects. The purpose of the enjoy meta-mode is to use shared technologies to enable the reuse of a large number of fine-grained objects.

Main roles:
Abstract enjoy meta (Flyweight) Role: This role is a superclass of all the specific classes of the classes that specify the public interfaces that need to be implemented. Operations that require an external state can be passed in as arguments by invoking business
Specific Concreteflyweight role: implements the flyweight interface and pulls back the storage space for the internal state (if any). The Concreteflyweight object must be shareable. The state it is stored in must be internal
Specific unsharedconcreteflyweight roles that are not shared: Not all flyweight subclasses need to be shared. Flyweigth makes sharing possible, but it does not force sharing.
The enjoy meta-factory (flyweightfactory) role: Responsible for creating and managing the rewards role. This role must ensure that the enjoyment meta object may be properly shared by the system
Client role: This role needs to maintain a reference to all of the enjoyment meta-objects. This role needs to store the external state of all the objects on its own

UML diagram:

  

Pattern Analysis:
The enjoy meta-mode is a design mode that takes system performance into account, and it can save memory space and improve the performance of the system by using the enjoy meta-mode.
The core of the enjoy meta-factory class, the benefit of the meta-factory class is to provide a pool for the enjoyment of the meta-object, the user needs the object, first obtained from the pool of enjoyment, if the pool does not exist, a new object is created to return to the user, and in the pool to save the new objects.
Enjoy meta-mode efficiently supports a large number of fine-grained objects in a shared way, and the key to sharing the meta-object is to differentiate between internal states (Internal State) and external states (External State).
The internal state is a state that is stored inside the object and is not changed with the environment, so the internal state can be shared.
An external state is a state that changes with the environment and cannot be shared. The external state of the enjoy meta object must be saved by the client and then passed in to the inside of the object when it needs to be used after the object is created. An external state is independent from the other external state.

Advantages
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.
The external state of the enjoy meta-mode is relatively independent and does not affect its internal state, allowing the enjoyment meta-object to be shared in different environments.

Disadvantages
The element-sharing model makes the system more complex and requires the separation of internal and external states, which complicates the logic of the program.
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.

You can use the enjoy meta mode in the following situations:
A system with a large number of identical or similar objects, due to the large use of such objects, resulting in a large amount of memory consumption.
Most of the state of an object can be externally passed in to an object.
Using the enjoy meta-mode requires maintaining a pool of privileges that stores the metadata of the objects, which consumes resources, so it should be worthwhile to use the enjoy meta-mode when reusing the object multiple times.

Simple-to-enjoy meta-mode and complex-enjoy meta-mode
Simple to enjoy meta mode: In the simple-to-enjoy meta-mode, all the objects of the object can be shared, that is, all the subclasses of the abstract-sharing class can be shared, there is no non-shared specific class
Compound-Enjoy meta-mode: combine some simple-to-use combination patterns to form compound-sharing objects, which cannot be shared by themselves, but can be decomposed into simple-to-enjoy meta-objects, while the latter can be shared.

Enjoy meta-mode with other modes
A static factory method is typically provided in the enjoy meta-factory class of the enjoy meta-mode to return the enjoyment meta object, using the simple Factory mode to generate the Privilege object.
In a system, there is usually only one single-use factory, so the enjoy meta-factory class can be designed using singleton mode.
The enjoy meta-mode can be combined to form a compound to enjoy meta-mode, unified to the enjoyment of the object set external state.

Code implementation:

<?PHP//abstract enjoy meta roleInterfaceflyweight{functionshow ();}//shared-specific privileges roleclassConcreteflyweightImplementsflyweight{Private $state; function__construct ($state){        $this->state =$state; }    functionShow () {return $this-State ; }}//does not share the specific privileges of the meta role, the client calls directlyclassUnsharedconcreteflyweightImplementsflyweight{Private $state; function__construct ($state){        $this->state =$state; }    functionShow () {return $this-State ; }}//enjoy meta-Factory modeclassflyweightfactory{Private $flyweights=Array(); functionGetflyweight ($state){        if(!isset($this->flyweights[$state])){            $this->flyweights[$state]=NewConcreteflyweight ($state); }        return $this->flyweights[$state]; }}//Test$flyweightFactory=Newflyweightfactory ();$flyweightOne=$flyweightFactory->getflyweight ("State A");$flyweightOne-Show ();$flyweightTwo=$UnsharedConcreteFlyweight("State B");$flyweightTwo-Show ();?>

Enjoy meta mode and PHP implementation

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.