The metadata mode of PHP design mode

Source: Internet
Author: User
PHP design mode-the metadata mode is a structural mode

Overview: using the sharing technology to effectively support a large number of fine-grained objects

Metadata mode:

Use sharing technology to effectively support a large number of fine-grained objects

The metadata mode changes the storage overhead of objects.

Main 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 outbound operation status can be passed in the form of parameters by calling business.

The specific ConcreteFlyweight role: implements the Flyweight interface and pulls 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 the metadata. This role must ensure that the object to be shared by the system.

Client role: This role must maintain a reference to all metadata objects. This role needs to store the external status of all the object

Advantages of the meta-mode:

The Flyweight mode can greatly reduce the number of objects in the memory.

Disadvantages of the metadata sharing mode:

The Flyweight mode makes the system more complex.

In Flyweight mode, the state of the object to be shared is externalized, while reading the external state makes the running time slightly longer.

Applicable scenarios of the Enjoy mode:

When this happens, the Flyweight mode is used:

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

Metadata mode 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.

/**

* Abstract meta-role

*/

Abstract class Flyweight {

Abstract public function operation ($ state );

}

/**

* Specific meta-roles

*/

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 .'
';

}

}

/**

* The client directly calls the specific metadata that is not shared.

*/

Class UnsharedConcreteFlyweight extends Flyweight {

Private $ _ intrinsicState = null;

/**

* Constructor

* @ Param string $ state internal status

*/

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 getFlyweight ($ state ){

If (isset ($ this-> _ flyweights [$ state]) {

Return $ this-> _ flyweights [$ state];

}

Return $ this-> _ flyweights [$ state] = new ConcreteFlyweight ($ state );

}

}

/**

* Metadata mode

*/

Public function actionFlyweight (){

Yii: import ('ext. flyweight .*');

$ FlyweightFactory = new FlyweightFactory ();

$ FlyweightFactory-> getFlyweight ('State ');

$ FlyweightFactory-> operation ('other state ');

$ FlyweightFactory = new FlyweightFactory ();

$ FlyweightFactory-> getFlyweight ('State B ');

$ FlyweightFactory-> operation ('other state B ');

$ FlyweightFactory = new FlyweightFactory ();

$ FlyweightFactory-> getFlyweight ('State c ');

$ FlyweightFactory-> operation ('other state c ');

/* Non-shared objects, called separately */

$ Uflyweight = new UnsharedConcreteFlyweight ('State ');

$ Uflyweight-> operation ('other state ');

}

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.