14. Structural Design Model -- flyweight pattern (lightweight model)

Source: Internet
Author: User
    • Definition

The sharing technology is used to effectively support a large number of fine-grained objects. The system only uses a small number of objects. These objects are similar, the state changes are small, and the usage of objects increases.

The UML class diagram is as follows:

The relationship between classes and objects:

1. flyweight (Abstract lightweight class): declares an interface that can accept external parameters (states) and Act on new States ).

2. concreteflyweight (Specific lightweight class): implements the flyweight interface and adds storage space for internal states. The concreteflyweight object must be shareable and all its storage states must be internal, it exists independently in its own environment.

3. unsharedconcreteflyweight (do not share specific lightweight classes): Not all flyweight subclasses need to be shared by the package, but flyweight sharing is not mandatory. In some flyweight structural layers, the unsharedconcreteflyweight Object usually uses the concreteflyweight object as a subnode.

4. flyweightfactory: Creates and manages flyweight objects and ensures that flyweight is used. When a user requests a flyweight, flyweightfactory provides an existing instance or creates an instance.

5. Client (customer applicationProgram): Maintain a reference to flyweight; compute or store one or more external States of Flyweight.

Sequence diagram of typical applications

When a customer obtains flyweight from a lightweight factory for the first time, the lightweight factory creates a new specific flyweight object and saves it. The next time the customer obtains the object, it does not need to create a new one, return directly in the storage pool. The customer is responsible for handling the flyweight status.

    • Instance 1 -- document Editor

Therefore, sharing these character objects will save a lot of storage space. The UML class diagram of the document editor is as follows:

Code

    //  Flyweight Factory  
Class Characterfactotry
{
Private Hashtable characters = New Hashtable ();
Public Character getcharacter ( Char Key)
{
// Extract characters from the initialized hashtable
Character character = (Character) characters [Key];
// If the character is null, initialize it.
If (Character = Null )
{
Switch (Key)
{
Case ' A ' :
Character = New Characloud ();
Break ;
Case ' B ' :
Character = New Characterb ();
Break ;
Case ' Z ' :
Character = New Characterz ();
Break ;
}
Characters. Add (Key, character );
}
Return Character;
}
}
// Flyweight
Abstract Class Character
{
Protected Char Symbol;
Protected Int Width;
Protected Int Height;
Protected Int Ascent;
Protected Int Descent;
Protected Int Pointsize;
Public Abstract Void Draw ( Int Pointsize );
}
Class Characloud: character
{
Public Characloud ()
{
This . Symbol = ' A ' ;
This . Height = 100 ;
This . Width = 120 ;
This . Ascent = 70 ;
This . Descent = 0 ;
}
Public Override Void Draw ( Int Pointsize)
{
This . Pointsize = Pointsize;
Console. writeline ( This . Symbol );
}
}
Class Characterb: character
{
Public Characterb ()
{
This . Symbol = ' B ' ;
This . Height = 100 ;
This . Width = 140 ;
This . Ascent = 72 ;
This . Descent = 0 ;
}
Public Override Void Draw ( Int Pointsize)
{
This . Pointsize = Pointsize;
Console. writeline ( This . Symbol );
}
}
Class Characterz: character
{
Public Characterz ()
{
This . Symbol = ' Z ' ;
This . Height = 100 ;
This . Width = 100 ;
This . Ascent = 68 ;
This . Descent = 0 ;
}
Public Override Void Draw ( Int Pointsize)
{
This . Pointsize = Pointsize;
Console. writeline ( This . Symbol );
}
}


// Customer application testing
Class Client
{
[Stathread]
Static Void Main ( String [] ARGs)
{
// Create document with character array
Char [] Document = { ' A ' , ' B ' , ' Z ' , ' Z ' , ' A ' , ' A ' };
Characterfactotry F = New Characterfactotry ();
// External status
Int Pointsize = 12 ;
// Use a flyweight object for each character
Foreach ( Char C In Document)
{
Character character = F. getcharacter (C );
Character. Draw (pointsize );
}
Console. Read ();
}
}

 

 

    • Strengths and weaknesses

In the flyweight mode, you need to carefully consider how to refine the object to reduce the number of objects to be processed, so as to reduce the usage of the remaining objects in the memory or other storage devices. However, this mode requires maintaining the external State of a large number of objects. If the data volume of an external State is large, transfer, search, and computation of these evil data will become very complicated. When the external and internal states are difficult to distinguish, the flyweight mode is not suitable.

    • Application scenarios

The following scenario is suitable for the lightweight Application Mode:

1. The system requires a large number of objects to share some essential and unchanged information.

2. objects can be used in multiple environments at the same time.

3. In each instance, flyweight can be used as an independent object.

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.