Design Pattern Learning Notes (22)-flyweight mode

Source: Internet
Author: User
Tags abstract constructor

First, the flyweight mode definition:

The use of sharing technology to effectively support a large number of fine-grained objects.

Second, the model explanation

That is, if you have more than one object in a system, it's OK to share only one copy, without having to instantiate each object. In flyweight mode, factory patterns often occur in flyweight mode because of the variety of objects to be produced. The internal state of the Flyweight is shared, and Flyweight factory is responsible for maintaining an object storage pool (Flyweight pool) to store the internal state. The flyweight mode is a model that improves program efficiency and performance, and can greatly speed up the program's running speed.

Third, the structure chart

The roles involved in the meta pattern include the abstract meta role, the specific (simple) privileges, the composite element role, the employee factory role, and the client role.

Abstract Flyweight role: This role is a superclass of all the specific superclass classes that specify the public interfaces or abstract classes that need to be implemented for these classes. Operations that require the presence of the External state can be passed through the parameters of the method. The interface of the abstract element makes the element possible, but does not force the subclass to be shared, so not all of the privilege objects are shared.

The specific (concreteflyweight) role: Implements the interface specified by the abstract privilege role. If you have an intrinsic state, you must be responsible for providing storage space for the intrinsic state. The intrinsic state of the privilege object must be independent of the environment around which the object is in, so that the object of the privilege can be shared within the system. Sometimes the specific role of the element is also called the simple specific role of the element, because the composite role is composed of a simple and specific role of the element.

Composite (unsharableflyweight) Role: The object represented by the composite element role cannot be shared, but a composite element object can be decomposed into multiple combinations that are itself a purely exclusive object. The composite element role is also called an unshared object. This role is generally rarely used.

The Flyweightfactoiy role: This role is responsible for creating and managing the privilege role. This role must ensure that the privilege object can be properly shared by the system. When a client object requests a privilege object, the Meta factory role needs to check if there is already a qualifying object in the system, and if so, the Meta factory role should provide this existing object of entitlement, and if there is not a proper object in the system, The Meta factory role should create a new appropriate object for the privilege.

Client role: This role also needs to store the external state of all the objects.

四、一个 Example

Import java.util.Hashtable;

/**
*
* @author <a href= "mailto:flustar2008@163.com" >flustar</a>
* @version 1.0
* Creation Date:jan, 2008 1:33:49 PM
*/

"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 Display (int pointsize);
}

"Concreteflyweight"

Class Charactera extends Character
{
Constructor
Public Charactera ()
{
This.symbol = ' A ';
This.height = 100;
This.width = 120;
This.ascent = 70;
this.descent = 0;
}

public void Display (int pointsize)
{
This.pointsize = pointsize;
System.out.println (This.symbol +
"(pointsize" + this.pointsize + ")");
}
}

"Concreteflyweight"

Class Characterb extends Character
{
Constructor
Public Characterb ()
{
This.symbol = ' B ';
This.height = 100;
This.width = 140;
This.ascent = 72;
this.descent = 0;
}

public void Display (int pointsize)
{
This.pointsize = pointsize;
System.out.println (This.symbol +
"(pointsize" + this.pointsize + ")");
}

}

C, D, E, etc.

"Concreteflyweight"

Class Characterz extends Character
{
Constructor
Public Characterz ()
{
This.symbol = ' Z ';
This.height = 100;
This.width = 100;
This.ascent = 68;
this.descent = 0;
}

public void Display (int pointsize)
{
This.pointsize = pointsize;
System.out.println (This.symbol +
"(pointsize" + this.pointsize + ")");
}
}
"Flyweightfactory"

Class Characterfactory
{
Private Hashtable characters = new Hashtable ();

Public Character Getcharacter (char key)
{
Uses "Lazy Initialization"
Character Character = (Character) characters.get (key);
if (character = null)
{
Switch (key)
{
Case ' A ': character = new Charactera (); Break
Case ' B ': character = new Characterb (); Break
//
Case ' Z ': character = new Characterz (); Break
}
Characters.put (key, character);
}
return character;
}
}
Test application
public class Test
{
public static void Main (String []args)
{
Build a document with text
String document = "AAZZBBZB";
char[] chars = Document.tochararray ();

Characterfactory f = new characterfactory ();

Extrinsic state
int pointsize = 10;

For each character use a flyweight object
for (Char c:chars)
{
pointsize++;
Character Character = F.getcharacter (c);
Character. Display (pointsize);
}

}
}

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.