JAVA design mode (in)- <结构型> Flyweight mode (__java)

Source: Internet
Author: User
1 Definition:

Flyweight mode (in)

Use sharing to support large numbers of fine-grained objects. (using shared objects can effectively support a large number of fine-grained objects.) )

The privilege mode requires that objects that can be shared must be fine-grained objects.

The key to sharing is the distinction between internal states (intrinsic State) and external states (extrinsic state). The following is a brief introduction to the internal state and external State of the element:

(1) Internal state is stored in the inside of the object and will not change with the environment changes in the state, the internal state can be shared . such as the content of the character, will not change with the external environment changes, no matter in any environment, the character "a" is always "a", will not become "B".

(2) The external state is changed with the environment change, can not be shared state. The external state of the privilege object is usually saved by the client and is then passed into the inside of the object when it needs to be used, after the object is created. An external state and another external state are independent of each other. such as the color of the characters, you can have different colors in different places, for example, some "a" is red, some "a" is green, the size of the character is the same, some "a" is fifth word, some "a" is fourth words. and the color and size of the characters are two independent external states, they can change independently, with no effect on each other, the client can be used to inject the external state into the object.

Because of the distinction between the internal state and the external state, we can store objects with the same internal state in the Chi Yuan pool, the object in the pool can be shared, the need for the object from the pool to take out, to achieve the object of reuse.       By injecting different external states into the extracted object, you can get a series of similar objects that actually store only one copy of the object in memory. The mode of the privilege is defined as follows: Flyweight pattern: The use of shared technology to effectively support the reuse of a large number of fine-grained objects. The system uses only a small number of objects, and these objects are very similar, the state changes very small, can be real object multiple Reuse . Because the pattern requires that the object that can be shared must be a fine-grained object, it is also known as lightweight mode, which is an object-structured pattern.
1.1 General class diagram:


The following roles are included in the meta-schema structure diagram:

Abstract (Flyweight): It is usually an interface or abstract class that declares the common method of a specific class in an abstract class < defines an interface or implementation of an object's external state and internal state, which provides the outside world with the internal data (internal state) of an object. You can also use these methods to set external data (external state).

(concreteflyweight): It implements the abstract class, the instance of which is called the Privilege object, and provides the storage space for the internal state in the specific privilege class. In general, we can design a specific meta class with a single example pattern. Provide a unique privilege for each specific class of Meta objects < The role should be noted that internal state processing should be independent of the environment, and that an operation should not occur that changes the internal state, while modifying the external state, which is not allowed >.

unshared (unsharedconcreteflyweight): Not all subclasses of abstract classes need to be shared, and subclasses that cannot be shared can be designed to be unshared; When you need an object that does not share a specific class, you create it directly by instantiating it. < This object generally does not appear in the exclusive factory >.

Henry Factory Class (Flyweightfactory): The Henry Factory class is used to create and manage the privilege object. It is designed for the abstract class of classes, the various types of specific objects are stored in a pool of interest, and the chi-yuan pool is generally intended to be a collection of "key-value pairs" (or other types of collections), Can be designed in conjunction with the factory model; When a user requests a specific object, the privilege factory provides an instance that is already created in the MSR pool or creates a new instance (if it does not exist), returns the newly created instance and stores it in the MSR pool.

In the enjoy meta mode, we introduce the element factory class, and the function of the Meta factory class is to provide a pool of benefits for storing the object of the privilege. When the user needs an object, it is first obtained from the pool of privileges, if it does not exist, create a new one to return to the user and save the newly created object in the pool of privileges.

1.2 General code:

Error Example:

[Java]  View plain copy package _22_flyweight;   public abstract class flyweight  {       private String intrinsic;        protected string extrinsic;          public flyweight ( String intrinsic)  {           this.intrinsic =  intrinsic;       }          public  string getintrinsic ()  {           return  intrinsic;       }          public  abstract void operate ();          public string  getextrinsic ()  {           return extrinsic;        }          public void setextrinisic ( String extrinsic)  {           this.extrinsic =  extrinsic;       }  }      public class  ConcreteFlyweight1 extends Flyweight {       public  ConcreteFlyweight1 (string intrinsic)  {            Super (intrinsic);       }          public  void operate ()  {       }  }      public class concreteflyweight2 extends flyweight {        public concreteflyweight2 (string intrinsic)  {             super (intrinsic);       }     

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.