Design pattern----Flyweight (enjoy Element) mode

Source: Internet
Author: User
Design Patterns ----Flyweight ( Henry Yuan ) Mode GOF :The use of sharing technology to effectively support a large number of fine-grained objects. explain the concept: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. For example (refer to the example in Gof book) a text system, each letter to set an object, then the large lowercase letter is 52, then you need to define 52 objects. If there is a 1M text, then the letter is so much, if each letter defines an object then memory burst.   So if you share an object with each letter, you save a lot of resources. 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. Flyweight mode is a model that improves program efficiency and performance, and can greatly speed up the running of programs. There are many applications, for example: first, define an abstract flyweight class:

Package Flyweight;

Public abstract class Flyweight
... {
public abstract void operation ();

}//End abstract class Flyweight

In implementing a specific class:
Package Flyweight;

public class Concreteflyweight extends Flyweight
... {
private string string;

Public concreteflyweight (String str)
... {
string = str;
}//end concreteflyweight (...)

public void operation ()
... {
SYSTEM.OUT.PRINTLN ("Concrete---Flyweight:" + string);
}//end operation ()

}//End Class Concreteflyweight

Implement a factory method class:
Package Flyweight;

Import java.util.Hashtable;

public class Flyweightfactory
... {
Private Hashtable flyweights = new Hashtable ()//----------------------------1

Public Flyweightfactory () ... {
}

Public Flyweight getflyweight (Object obj)
... {
Flyweight Flyweight = (Flyweight) flyweights.get (obj);//----------------2

if (flyweight = null) ... {//---------------------------------------------------3
Create a new Concreteflyweight
Flyweight = new Concreteflyweight ((String) obj);
Flyweights.put (obj, flyweight);//--------------------------------------5
}
Return flyweight;//---------------------------------------------------------6
}//end getflyweight (...)

public int getflyweightsize ()
... {
return Flyweights.size ();
}

}//End Class Flyweightfactory
This factory method class is very critical, and here's a detailed explanation:
A hashtable is defined at 1 to store each object, and 2 to select the object to instantiate, return the object at 6, and if there is no object to select in Hashtable, the variable flyweight null. Produces a new flyweight stored in Hashtable and returns the object.

Finally look at the flyweight call:
Package Flyweight;

Import java.util.Hashtable;

public class Flyweightpattern ... {

Flyweightfactory factory = new Flyweightfactory ();
Flyweight fly1;

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.