Design Mode C ++ description ---- 12. Flyweight Mode

Source: Internet
Author: User

I. Overview


In the design and implementation of an object-oriented system, object creation is the most common operation.

There is a problem here: if an application uses too many objects, it will cause a large storage overhead. Especially for a large number of lightweight (fine-grained) objects, for example, in the design process of the document Editor, if we do not create an object for letters, the system may waste storage overhead due to a large number of objects.

For example, a letter "a" appears 100000 times in the document. In fact, we can share these 10 thousand letters "a" with an object, of course, because the letter "a" may have different display effects at different locations (for example, different font and size settings ), in this case, we can divide the object state into "external state" and "internal state", and store the State that can be shared (not changed) as the internal state in the object, for external objects (such as the font and size mentioned above), we can pass the most parameters of the external object to the object as appropriate (for example, when displaying, the font, size, and other information are passed to the object ).

Ii. metadata sharing mode

Definition: Use the sharing technology to effectively support a large number of fine-grained objects.


The structure is as follows:

 

Flyweight: the parent class or interface of all the specific Metadata classes.

ConcreteFlyweight: implements specific operations for specific Metadata classes

Unshared concreteflyweight: subclass that does not need to be shared

FlyweightFactory: reasonably create and manage the metadata class


The Code is as follows:


[Cpp] view plaincopyprint? // Enjoy the metadata class
Class Flyweight
{
Public:
Virtual ~ Flyweight (){}
 
Virtual void Operation (const string & extrinsicState ){}

String GetIntrinsicState ()
{
Return this-> _ intrinsicState;
}
 
Protected:
Flyweight (string intrinsicState)
{
This-> _ intrinsicState = intrinsicState;
}
 
Private:
String _ intrinsicState;
};
 
// Specific metadata
Class ConcreteFlyweight: public Flyweight
{
Public:
ConcreteFlyweight (string intrinsicState): Flyweight (intrinsicState)
{
Cout <"ConcreteFlyweight Build..." <intrinsicState <endl;
}
 
~ ConcreteFlyweight (){}
 
// Implementation Interface
Void Operation (const string & extrinsicState)
{
Cout <"Internal [" <this-> GetIntrinsicState () <"] external [" <extrinsicState <"]" <endl;
}
};
 
// Enjoy the Yuan Factory
Class FlyweightFactory
{
Public:
FlyweightFactory (){}
 
~ FlyweightFactory (){}
 
// Ensure that Flyweight is properly shared
Flyweight * GetFlyweight (const string & key)
{
Vector <Flyweight *>: iterator it = _ fly. begin ();
 
For (; it! = _ Fly. end (); it ++)
{
If (* it)-> GetIntrinsicState () = key)
{
Cout <"already created by users..." <endl;
Return * it;
}
}
 
Flyweight * fn = new ConcreteFlyweight (key );
_ Fly. push_back (fn );
Return fn;
}
 
Private:
Vector <Flyweight *> _ fly;
};
 
 
// Test
Int main (int argc, char * argv [])
{
FlyweightFactory * fc = new FlyweightFactory ();

// For different objects, the metadata factory will create a new metadata class.
Flyweight * fw1 = fc-> GetFlyweight ("Object ");
Flyweight * fw2 = fc-> GetFlyweight ("Object B ");

// For the same object, the metadata factory will use a created metadata class.
Flyweight * fw3 = fc-> GetFlyweight ("Object ");
 
Return 0;
}
// Enjoy the metadata class
Class Flyweight
{
Public:
Virtual ~ Flyweight (){}

Virtual void Operation (const string & extrinsicState ){}

String GetIntrinsicState ()
{
Return this-> _ intrinsicState;
}

Protected:
Flyweight (string intrinsicState)
{
This-> _ intrinsicState = intrinsicState;
}

Private:
String _ intrinsicState;
};

// Specific metadata
Class ConcreteFlyweight: public Flyweight
{
Public:
ConcreteFlyweight (string intrinsicState): Flyweight (intrinsicState)
{
Cout <"ConcreteFlyweight Build..." <intrinsicState <endl;
}

~ ConcreteFlyweight (){}

// Implementation Interface
Void Operation (const string & extrinsicState)
{
Cout <"Internal [" <this-> GetIntrinsicState () <"] external [" <extrinsicState <"]" <endl;
}
};

// Enjoy the Yuan Factory
Class FlyweightFactory
{
Public:
FlyweightFactory (){}

~ FlyweightFactory (){}

// Ensure that Flyweight is properly shared
Flyweight * GetFlyweight (const string & key)
{
Vector <Flyweight *>: iterator it = _ fly. begin ();

For (; it! = _ Fly. end (); it ++)
{
If (* it)-> GetIntrinsicState () = key)
{
Cout <"already created by users..." <endl;
Return * it;
}
}

Flyweight * fn = new ConcreteFlyweight (key );
_ Fly. push_back (fn );
Return fn;
}

Private:
Vector <Flyweight *> _ fly;
};


// Test
Int main (int argc, char * argv [])
{
FlyweightFactory * fc = new FlyweightFactory ();

// For different objects, the metadata factory will create a new metadata class.
Flyweight * fw1 = fc-> GetFlyweight ("Object ");
Flyweight * fw2 = fc-> GetFlyweight ("Object B ");

// For the same object, the metadata factory will use a created metadata class.
Flyweight * fw3 = fc-> GetFlyweight ("Object ");

Return 0;
}
Iii. Description

1. the metadata factory class is the focus, because it creates and manages the metadata object, creates an existing object for it, and provides a created instance for the existing object.

2. It can be imagined that there is an object pool which contains some Metadata classes. The role of the metadata factory is to retrieve objects from the object pool.

3. It aims to greatly reduce the number of classes to be instantiated.

 


From lwbeyond

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.