This article mainly introduces the metadata sharing mode in the php design mode. if you are interested, refer to it.
I. intention
Use sharing technology to effectively support a large number of fine-grained objects
The metadata mode changes the storage overhead of objects.
II. structure of the metadata mode
III. primary roles in the metadata mode
Abstract Flyweight role:This role is a superclass of all the specific metadata classes, and defines the public interfaces to be implemented for these classes. Operations that require an external state can be passed in the form of parameters by calling business.
The specific ConcreteFlyweight role:Implement the Flyweight interface and pull back the bucket for the internal status (if any. The ConcreteFlyweight object must be shareable. It must be stored in an internal state.
UnsharedConcreteFlyweight:Not all Flyweight subclasses need to be shared. Flyweigth makes sharing possible, but it does not force sharing.
FlyweightFactory role:Creates and manages metadata roles. This role must ensure that the object to be shared by the system.
Client role:This role must maintain a reference to all the metadata objects. This role needs to store the external status of all the object
IV. Advantages and disadvantages of the metadata sharing model
Metadata-basedAdvantages:The Flyweight mode can greatly reduce the number of objects in the memory.
Metadata-basedDisadvantages:
1. the Flyweight mode makes the system more complex.
2. in Flyweigth mode, the state of the object to be shared is externalized, and reading the external state leads to a slightly longer running time.
5. applicable scenarios
The Flyweight mode is used when all of the following conditions are met:
1. an application uses a large number of objects.
2. a large storage overhead is caused by the use of a large number of objects.
3. most states of objects can be changed to external states.
4. if you delete the external state of an object, you can replace multiple groups of objects with fewer shared objects.
5. applications do not rely on object identifiers.
VI. metadata and other modes
Singleton: the client creates or obtains a metadata object to be referenced by a factory object. each time the client references a metadata object, you can use the same factory object to reference the expected object. Therefore, the metadata factory can be designed as a singleton mode, which ensures that the client only references one factory instance. Because all the metadata objects are managed by one factory object, there is no need to reference multiple factory objects on the client. Whether in the simple metadata mode or the compound metadata mode, the metadata factory role can be designed as a singleton mode without any impact on the results.
Composite mode: the Composite mode is actually a combination of simple and merging modes. Simple metadata objects can be shared as leaf objects, while Composite metadata objects can be used as branch objects. Therefore, clustering management methods can be added to composite metadata roles.
VII. PHP example of the metadata mode
<? Php/*** abstract meta-role */abstract class Flyweight {/*** schematic method * @ param string $ state external state */abstract public function operation ($ state );} /*** specific meta-role */class ConcreteFlyweight extends Flyweight {private $ _ intrinsicState = null; /*** constructor * @ param string $ state Internal state */public function _ construct ($ state) {$ this-> _ intrinsicState = $ state ;} public function operation ($ state) {echo 'concreteflyweight operation, Intrinsic State = '. $ this-> _ intrinsicState. 'extrinsic State = '. $ state.'
';}}/*** Does not share the specific metadata. the client directly calls */class UnsharedConcreteFlyweight extends Flyweight {private $ _ intrinsicState = null; /*** constructor * @ 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.'
';}}/*** Metadata factory role */class FlyweightFactory {private $ _ flyweights; public function _ construct () {$ this-> _ flyweights = array ();} public function getFlyweigth ($ state) {if (isset ($ this-> _ flyweights [$ state]) {return $ this-> _ flyweights [$ state];} else {return $ this-> _ flyweights [$ state] = new ConcreteFlyweight ($ state );}}} /*** Client */class Client {/*** Main program. */public static functio N main () {$ flyweightFactory = new FlyweightFactory (); $ flyweight = $ flyweightFactory-> getFlyweigth ('State '); $ flyweight-> operation ('other state a'); $ flyweight = $ flyweightFactory-> getFlyweigth ('State B '); $ flyweight-> operation ('other state B ');/* non-shared object, called separately */$ uflyweight = new UnsharedConcreteFlyweight ('State '); $ uflyweight-> operation ('other state a') ;}} Client: main ();?>
8. compound metadata sharing mode
The composite object is composed of some simple elements that use the composite mode.
Objects represented by a composite meta-object cannot be shared. However, a composite meta-object can be divided into multiple combinations of meta-objects.
IX. PHP example of compound metadata-sharing mode
<? Php/*** abstract meta-role */abstract class Flyweight {/*** schematic method * @ param string $ state external state */abstract public function operation ($ state );} /*** specific meta-role */class ConcreteFlyweight extends Flyweight {private $ _ intrinsicState = null; /*** constructor * @ param string $ state Internal state */public function _ construct ($ state) {$ this-> _ intrinsicState = $ state ;} public function operation ($ state) {echo 'concreteflyweight operation, Intrinsic State = '. $ this-> _ intrinsicState. 'extrinsic State = '. $ state.'
';}}/*** Does not share the specific metadata. the client directly calls */class UnsharedConcreteFlyweight extends Flyweight {private $ _ flyweights; /*** constructor * @ param string $ state Internal state */public function _ construct () {$ this-> _ flyweights = array ();} public function operation ($ state) {foreach ($ this-> _ flyweights as $ flyweight) {$ flyweight-> operation ($ state) ;}} public function add ($ state, flyweight $ flyweight) {$ this-> _ flyweights [$ s Tate] = $ flyweight;}/*** */class FlyweightFactory {private $ _ flyweights; public function _ construct () {$ this-> _ flyweights = array ();} public function getFlyweigth ($ state) {if (is_array ($ state )) {// composite mode $ uFlyweight = new UnsharedConcreteFlyweight (); foreach ($ state as $ row) {$ uFlyweight-> add ($ row, $ this-> getFlyweigth ($ row);} return $ uFlyweight;} else if (is_string ($ state )){ If (isset ($ this-> _ flyweights [$ state]) {return $ this-> _ flyweights [$ state];} else {return $ this-> _ flyweights [$ state] = new ConcreteFlyweight ($ state) ;}} else {return null ;}}} /*** Client */class Client {/*** Main program. */public static function main () {$ flyweightFactory = new FlyweightFactory (); $ flyweight = $ flyweightFactory-> getFlyweigth ('State '); $ flyweight-> operation ('other state A '); $ flyweight = $ flyweightFactory-> getFlyweigth ('State B'); $ flyweight-> operation ('other state B '); /* composite object */$ uflyweight = $ flyweightFactory-> getFlyweigth (array ('State A', 'state B ')); $ uflyweight-> operation ('other state a') ;}} Client: main ();?>
10. The role of PHP in the metadata model
Compared with other modes, the Flyweight mode does not make much sense in the existing PHP versions, because the lifecycle of PHP is page-level, that is, the required resources will be loaded from the execution of a PHP file, after the execution is complete, all these resources will be released, and generally we will not let a page be executed for too long.
The above is the code that uses php to implement the metadata-sharing mode. There are also some concepts about the metadata-sharing mode, which will be helpful for your learning.