Summary of design patterns-flyweight Method)

Source: Internet
Author: User

Problem:
In object-oriented design and development, it is often encountered that the same type of objects (new from the same class) appear multiple times in different scenarios, however, because of different application scenarios, their statuses (attribute values) are different, but most of their attributes are the same, we need to create a large volume of objects with different States in different application scenarios. However, a large number of objects may cause high system storage overhead and performance overhead during object creation.
Definition:

The sharing mode (also called the lightweight mode) is a structural mode that uses the sharing technology to effectively support a large number of fine-grained objects.

Intent:
Extracts the common states of multiple identical objects in different application scenarios (intrinsic, relatively unchanged in different application scenarios ), it is encapsulated in a concrete flyweight that can be shared and provides an access interface for modifying the status to be changed. It is used in different scenarios, extrinsic. flyweight factory has a "repository" (or an object pool) for managing and storing objects ), when a client request is received, the objects in the object pool will be traversed. If an existing object exists, the client will be returned directly. Otherwise, a new object will be created, added to the pool, and then returned to the client, this reduces the number of object creation tasks.

Participants:
• Abstract element (flyweight) role:

This role is the base class of all the specific Metadata classes and specifies the public interfaces to be implemented for these classes. Operations that require external States (extrinsic) can be passed in as parameters by calling the enjoy method.
• The specific concrete flyweight role:

Implement the interface specified by the abstract metadata role. If an internal state (intrinsic) exists, you must provide storage space for the internal state (intrinsic. The internal state (intrinsic) of the object must be independent of the surrounding environment of the object, so that the object can be shared within the system.
• Flyweight factory role:

This role is responsible for creating and managing the role. This role must ensure that the object can be shared by the system. When a client object calls a metadata object, the metadata factory role checks whether a metadata object meets the requirements in the system. If you already have a metadata factory role, you should provide the existing metadata object. If the system does not have an appropriate metadata object, the metadata factory role should create a suitable metadata object.
• Client role:

This role must maintain a reference to all the metadata objects. This role needs to store the external status (extrinsic) of all metadata objects on its own ).

UML:

Instance description:

Nokia mobile phone Factory
There are factories in the headquarters for production, and then transferred to each branch for listing

The UML diagram is as follows:

 

Code:

///   <Summary>
/// Abstract flyweight role
///   </Summary>
Public Abstract Class Phonemodel
{

// Shared Element
Public String Name { Get ; Set ;}
Public String Net { Get ; Set ;}
Public String Size { Get ; Set ;}
// Elements in the non-shared status
Protected String Productionplace { Get ; Set ;}
// Provides a method to modify the non-shared status.
Public Abstract Void Change ( String Productionplace );
}
///   <Summary>
/// Concrete flyweight role
///   </Summary>
Public Class N8object: phonemodel
{
Public N8object ()
{
This . Name = " N8 " ;
This . Net = " CDMA " ;
This . Size = " 98*20*10 " ;
}
Public Override Void Change ( String Productionplace)
{
This . Productionplace = productionplace;
}
}
///   <Summary>
/// Concrete flyweight role
///   </Summary>
Public Class N9object: phonemodel
{
Public N9object ()
{
This . Name = " Bytes " ;
This . Net = " WCDMA " ;
This . Size = " 100*20*10 " ;
}
Public Override Void Change ( String Productionplace)
{
This . Productionplace = productionplace;
System. Console. writeline (" Region region changed " + Productionplace );
}
}
///   <Summary>
/// Flyweight factory role
///   </Summary>
Public Class Flyweightfactory
{
Dictionary < String , Phonemodel> phonelist = New Dictionary < String , Phonemodel> ();

Public Phonemodel getphoneobject ( String Name)
{
If (! Phonelist. containskey (name ))
{
Phonemodel phone = Null ;
Switch (Name)
{
Case " N8 " :
Phone = New N8object (); Break ;
Case " Bytes " :
Phone = New N9object (); Break ;
}
Phonelist. Add (name, phone );
}
Return Phonelist [name];
}
}
///   <Summary>
/// Client Test
///   </Summary>
Public Void Decoratortest ()
{
Flyweightfactory factory =New Flyweightfactory ();
VaR N8 = factory. getphoneobject ( " N8 " );
N8.change ( " New Zealand " );
}

advantages:
• reduces the number of processed objects, which reduces the Object Storage overhead and the performance overhead during object creation.
disadvantages:
• the external state of the object needs to be maintained by the client. If the data volume of the external State is large, transferring, searching, and computing will become very complicated.
Applicability:
• the system requires a large number of objects, and objects in different scenarios share a large number of States.
PS:
the listener_instance list is maintained in the metadata factory, and resources are also required, therefore, if you do not have a large number of metadata instances (one object has fewer application scenarios), you cannot use the metadata mode.
If the object has many independent states, more than the shared status, it is not suitable for the use of the metadata mode. Because maintaining a large number of external States not only makes the logic complex, but also does not save resources.

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.