Design mode--12. Enjoy meta mode

Source: Internet
Author: User

1. Mode motivation

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 .

2. Pattern definition

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.

3. Pattern structure

The enjoy meta mode contains the following roles:

Flyweight: Abstract enjoy meta class

Concreteflyweight: Specific class of classes to enjoy

Unsharedconcreteflyweight: Non-shared specific class of privileges

Flyweightfactory: Enjoy the Yuan factory class

4. Timing Diagram

5. Code Analysis
#include <iostream> #include "ConcreteFlyweight.h" #include "FlyweightFactory.h" #include "Flyweight.h" using namespace Std;int Main (int argc, char *argv[]) {Flyweightfactory factory; Flyweight * fw = Factory.getflyweight ("one"); Fw->operation (); Flyweight * fw2 = Factory.getflyweight ("both"); Fw2->operation ();//aready exist in poolflyweight * fw3 = Factory.getflyw Eight ("one"); Fw3->operation (); return 0;}
#include "FlyweightFactory.h" #include "ConcreteFlyweight.h" #include <iostream>using namespace std; Flyweightfactory::flyweightfactory () {}flyweightfactory::~flyweightfactory () {}flyweight* FlyweightFactory:: Getflyweight (String str) {Map<string,flyweight*>::iterator ITR = m_mpflyweight.find (str); if (ITR = = M_ Mpflyweight.end ()) {Flyweight * fw = new Concreteflyweight (str); M_mpflyweight.insert (Make_pair (STR,FW)); return FW;} Else{cout << "Aready in the Pool,use the exist one:" << Endl;return Itr->second;}
#include "Flyweight.h" #include <string>using namespace Std;class concreteflyweight:public flyweight{public: Concreteflyweight (string str); virtual ~concreteflyweight (); virtual void operation ();p rivate:string intrinsicstate;};
#include "ConcreteFlyweight.h" #include <iostream>using namespace std; Concreteflyweight::concreteflyweight (String str) {intrinsicstate = str;} Concreteflyweight::~concreteflyweight () {}void concreteflyweight::operation () {cout << ' flyweight[' << Intrinsicstate << "do operation." << Endl; }

Operation Result:

6. 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.

7. Advantages

Benefits of the Enjoy meta model

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.

8. Disadvantages

Disadvantages of the enjoy meta model

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.

9. Applicable environment

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.

10. Mode application

Enjoy meta mode is used extensively in the editor software, such as the appearance of the same picture multiple times in a document, you only need to create a picture object, by setting the location in the application where the picture appears, you can make the picture repeated multiple times in different places.

11. Mode expansion

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, and there is no non-shared element 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.

12. Summary

The shared meta-mode effectively supports the reuse of a large number of fine-grained objects using sharing technology. The system uses only a small number of objects, and these objects are very similar, the state changes very small, can achieve multiple reuse of objects, it is an object-structured pattern.

The enjoy meta-mode contains four roles: the abstract-class declares an interface through which it can accept and act on the external state, and the concrete-to-be-shared meta-class implements an abstraction of the meta-interface, an instance of which is called the privilege object, and the unshared element is a subclass of an abstract-class that cannot be shared; the enjoy meta- It is programmed for the abstraction of the class, storing the various types of specific objects in a single pool of privileges.

Enjoy meta-mode to effectively support a large number of fine-grained objects in a shared way, the key to sharing the meta-object is to differentiate between internal and external states. The internal state is a state stored inside the object that is not changed as the environment changes, so the internal state can be shared; The external state is a state that changes with the environment and cannot be shared.

The main advantage of the enjoy meta-mode is that it can greatly reduce the number of objects in memory, so that the same object or similar objects in memory only one copy, its disadvantage is to make the system more complex, and the need to be the state of the object to be external, read the external state to make the runtime longer.

The application of the meta-mode includes: A system with a large number of identical or similar objects, due to the large amount of use of such objects, resulting in a large amount of memory consumption, the majority of the state of the object can be external, you can pass these external states into the object, multiple reuse of the enjoyment object.

Design mode--12. Enjoy meta-mode

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.