Enjoy meta-mode is a structured model
Overview: Use shared technology to efficiently support large amounts of fine-grained objects
Enjoy meta mode:
Use shared technology to effectively support a large number of fine-grained objects
The change of the sharing mode is the storage cost of the object.
Main roles in the enjoy meta-mode:
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 need to be Sinotrans can be passed in as arguments by calling 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 a share
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
Benefits of the Enjoy meta-mode:
Flyweight mode can significantly reduce the number of objects in memory
Disadvantages of the enjoy meta model:
Flyweight mode makes the system more complex
The flyweight mode takes the state of a metadata object out of the way, while reading the external state makes the run time slightly longer
Enjoy meta mode for scenarios:
Use the flyweight mode when the situation is true:
11 applications use a large number of objects
2 full storage overhead due to the large number of objects used
3 most states of an object can be changed to an external state
4 If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects
5 application does not depend on object identity
Enjoy meta mode and other modes:
Singleton mode (Singleton): The client is to reference the object of enjoyment, which is created or obtained from a factory object, and each time a client refers to a single object, it is possible to refer to the desired privilege object through the same factory object. Therefore, you can design the enjoy meta factory as a singleton mode, which guarantees that clients only reference one factory instance. Because all of the object objects are managed uniformly by a single factory object, it is not necessary for the client to refer to multiple factory objects. Whether it's a simple-to-enjoy meta-mode or a share-factory role in the compound-sharing model, it can be designed as a singleton pattern, with no effect on the results.
Composite mode: The compound-to-share mode is actually a combination of the simple-to-enjoy meta-pattern and the compositing pattern. A simple-to-use meta-object can be shared as a leaf object, and a compound-enjoy meta-object can be used as a tree object, so you can add a clustered management method in a composite-privilege role
/**
* Abstract enjoy meta role
*/
Abstract class flyweight{
Abstract public Function operation ($state);
}
/**
* Specific enjoy meta role
*/
Class Concreteflyweight extends flyweight{
Private $_intrinsicstate = null;
/**
* @param $state Internal status
*/
Public function __construct ($state) {
$this->_intrinsicstate = $state;
}
Public function operation ($state) {
Echo ' concreteflyweight operation, intrinsic state = '. $this->_intrinsicstate
. ' Extrinsic state = '. $state. '
';
}
}
/**
* Do not share the specific privileges, the client calls directly
*/
Class Unsharedconcreteflyweight extends Flyweight {
Private $_intrinsicstate = null;
/**
* Construction Method
* @param string $state internal state
*/
Public function __construct ($state) {
$this->_intrinsicstate = $state;
}
Public function operation ($state) {
Echo ' unsharedconcreteflyweight operation, intrinsic state = '. $this->_intrinsicstate
. ' Extrinsic state = '. $state. '
';
}
}
/**
* Enjoy the meta-factory role
*/
Class flyweightfactory{
Private $_flyweights;
Public Function __construct () {
$this->_flyweights = Array ();
}
Public Function Getflyweight ($state) {
if (Isset ($this->_flyweights[$state])) {
return $this->_flyweights[$state];
}
return $this->_flyweights[$state] = new Concreteflyweight ($state);
}
}
/**
* Enjoy meta mode
*/
Public Function Actionflyweight () {
Yii::import (' ext.flyweight.* ');
$flyweightFactory = new Flyweightfactory ();
$flyweightFactory->getflyweight (' state A ');
$flyweightFactory->operation (' other State A ');
$flyweightFactory = new Flyweightfactory ();
$flyweightFactory->getflyweight (' state B ');
$flyweightFactory->operation (' other State B ');
$flyweightFactory = new Flyweightfactory ();
$flyweightFactory->getflyweight (' state C ');
$flyweightFactory->operation (' other State C ');
/* objects that are not shared, are called separately */
$uflyweight = new Unsharedconcreteflyweight (' state A ');
$uflyweight->operation (' other State A ');
}