Enjoy meta-mode refers to "using shared technology to effectively support a large number of fine-grained objects"
All "use shared technology to effectively support a large number of fine-grained objects" we can understand that when the number of fine-grained objects is too high, the cost of using shared technology can greatly reduce the cost of running
1. Structure
1.1 Simple-to-enjoy meta-mode structure
This mode, all the shared meta objects are shareable, such as
(1) Abstract enjoy meta-role (Flyweight): For the specific enjoyment meta-role defined must be implemented method, the interreligious state is in the form of parameters passed through this method, in the Java Chinese Academy of Sciences by the abstract class, interface class to assume.
(2) Specific enjoy meta-role (Concreteflyweight): The method that implements the abstract role stipulation. If there is an internal interreligious state, hold the storage space for the internal interreligious state
(3) Enjoy the meta-factory role (Flyweightfactory): Responsible for creating and managing the privilege role, to achieve the purpose of sharing, the role of the implementation of the key! (Files available HashMap or hashtable)
(4) Client role: maintains a reference to all the interreligious, and also stores the corresponding external state.
1.2 composite Enjoy meta-mode structure
This mode is reused using the simple-to-enjoy-element, interface-composite-synthesis mode, as shown in
(1) Abstract enjoy meta-role (Flyweight): It is the superclass of all the specific classes of the class
(2) specific privileges (concreteflyweight): implementation of the interface defined by the abstract privileges meta role
(3) Compound Unsharableflyweight: The object it represents is not shareable, but a composite of the object can be willing to decompose into a plurality of itself is a simple combination of meta-objects. As in the Concretecompositeflyweight,
(4) Enjoy the meta-factory role (Flyweightfactory): Responsible for creating and managing the rewards role
(5) Client role: This role needs to store the external interreligious state of all the objects in its own
2. Example
2.1 Single-Enjoy meta-mode instances
There is a series of ice cream in the beverage store for takeaway, ice cream has a interreligious state, that is, ice cream flavor, ice cream has no environmental factors, that is, no external interreligious state. If you develop a management system for each ice cream manager A separate object, then produce a large number of objects. The reasonable way to do this is by the type of ice cream That is, taste), each flavor ice cream only creates an object and implements sharing, so-called sharing, mainly refers to the taste of ice cream sharing, manufacturing methods of sharing and so on. Therefore, in the beverage shop, you do not need to be prepared separately for each ice cream. When necessary, The owner can make a one-time ice cream that is enough for the day. Based on this, the use of the enjoy meta-mode is designed as follows
The owner is the client role, the ice cream to enjoy the meta-plant to enjoy the meta-factory role, ice cream to enjoy the meta-interface is the abstract enjoy meta-role, ice cream to enjoy the meta-implementation class and specific privileges
2.2 Multi-element mode instances
The ice cream in the beverage store is only used for takeaway, and the customer has a suggestion that they would like to have a table in store to consume ice cream, when the ice cream table is out of interreligious state. At this point, the single-enjoy meta-mode needs to be extended to the multi-element mode, based on the use of the multi-element mode design such as
The owner is the client role, the ice cream to enjoy the meta-factory to enjoy the meta-factory role, ice cream to enjoy the meta-interface is an abstraction of the meta-role, ice cream to enjoy the meta-realization class is the specific enjoy meta role, compound enjoy meta
Application Scenarios
The advantage of the enjoy meta-mode is that it saves memory space by reducing the number of memory objects. Based on this, I think the following scenarios can be used in the use of the software and implementation of the module.
1. When there are too many software system objects.
2. Objects in the software system spend too much memory
3. The state of the object in the software system, most of which can be externally
4. When classifying according to the state of the object, when the external state is removed, each classification army can replace it with one object
5. Software systems need not rely on the logo of the object
3. Example Source code:
3.1 Single-Enjoy meta-mode
The source code is as follows
Package model.flyweight1;/* * Abstract enjoy meta role */public interface Share {public string work (String fettle);} Package model.flyweight1;/* * Specific enjoy meta role * belongs to Conreteflyweight role */public class Concerteshare implements Share {//Inside interreligious status private C Haracter internalfettle = null; @Overridepublic string work (string fettle) {String a = "internalfettle=" +internalfettle+ " , exterior fettle= "+fettle;return A;} Transfer the outer interreligious State into public concerteshare (Character fettle) {this.internalfettle = fettle;}} Package Model.flyweight1;import java.util.hashtable;/* * Enjoy meta factory role class * belongs to Flyweightfactory */public class Sharefactory { Private Hashtable matter = new Hashtable ();p rivate Share share;public sharefactory () {}public Share factory (Character Fett Le) {if (Matter.containskey (fettle)) {return (Share) matter.get (fettle);} Else{share = new Concerteshare (fettle); Matter.put (fettle, share); return share;}}} Package Model.flyweight1;public class Client {public static void main (string[] args) {Sharefactory factory = new Sharefact Ory (); Share Share =factory.factorY (New Character (' Medium ')); System.out.println ("Font:" +share.work ("blackbody")); System.out.println ("Font:" +share.work ("Arial")); share =factory.factory (New Character (' outside ')); System.out.println ("Font:" +share.work ("faux"));}}
3.2 Multi-element mode
The source code is as follows
Package model.flyweight2;/* * Abstract enjoy meta role */public interface Share {public string work (String fettle);} Package model.flyweight2;/* * Specific enjoy meta role * belongs to Conreteflyweight role */public class Concerteshare implements Share {//Inside interreligious status private C Haracter internalfettle = null; @Overridepublic string work (string fettle) {String a = "internalfettle=" +internalfettle+ " , exterior fettle= "+fettle;return A;} Transfer the outer interreligious State into public concerteshare (Character fettle) {this.internalfettle = fettle;}} Package Model.flyweight2;import Java.util.hashmap;import Java.util.hashtable;import java.util.iterator;import java.util.map;/* * Composite enjoy meta-role * I.E. Unsharableflyweight role */public class Concretecompositeshare implements Share {private Hashtable matter = new Hashtable (Ten);p rivate Share share;//Incoming interreligious status @overridepublic string work (string fettle) {Share Share = Null;for (Iterator it=matter.entryset (). Iterator (); It.hasnext ();) {Map.entry E = (map.entry) it.next (); share = (share) e.getvalue (); Share.work (fettle);} return fettle;} Public Concretecompositeshare ({}//adds a new pure meta object to the aggregation in public void Add (Character key,share Share) {matter.put (key, Share);}} Package Model.flyweight2;import java.util.hashtable;/* * Enjoy meta factory role class * belongs to Flyweightfactory */public class Sharefactory { Private Hashtable matter = new Hashtable ();p rivate Share share;public sharefactory () {}//single-access meta-factory method, required state passed in as parameter public Share Factory (Character fettle) {if (Matter.containskey (fettle)) {return (Share) matter.get (fettle);} Else{share = new Concerteshare (fettle); Matter.put (fettle, share); return share;}} Enjoy the Meta factory method, use the string parameter to pass in the state public Share Factory (string compositestate) {Concretecompositeshare compositeshare = new Concretecompositeshare (); int length = Compositestate.length (); Character fettle = null;for (int i = 0; i < length; i++) {fettle = new Character (Compositestate.charat (i)); System.out.println ("Factory (" +fettle+ ")") Compositeshare.add (fettle, This.factory (fettle));} return compositeshare;}} Package Model.flyweight2;public class Client {public static void main (string[] args) {sharefactory factOry = new Sharefactory (); Share Share = Factory.factory ("Southeast West"); System.out.println ("Inner interreligious Southeast, outer interreligious are fonts:" +share.work ("Imitation"));}}
Struts1 and enjoy meta mode
The action class in Struts1 embodies the shared mode, for example, we declare a field in action and set the relevant parameter, if the initial value is 88, when the client has access to set the value to 77, then the second consumer calls this field, the result will be 88, Instead of the modified 77. The reason is that the Action Declaration field property is shared for the manifest request, of course, the suitability of the shared mode is applied here, which should be combined with the project requirements, if the project needs a value that never changes, then this mode is more reasonable here, if the project has a numerical modification of the field , we also have a way to solve.
For example, we can remove the following part of the struts existing Requestprocessor class processactioncreate () by creating a subclass of Rquestprocessor Testrequestprocessor
Instance = (Action) action.get (className);
if (instance! = null) {
if (log.ustraceenbled ()) {
Log.trace ("retuning existing Action instance");
}
return (instance);
}
All the rest of the code is placed in the Testrequestprocessor processactioncrteate ()
Enjoy meta mode (flyweight)