PS One sentence: Eventually choose Csdn to organize the publication of the knowledge points of these years, the article parallel migration to CSDN. Because CSDN also support markdown grammar, Ah!
"Craftsman Joshui Http://blog.csdn.net/yanbober" read the previous "design pattern (structural type) of the appearance pattern (facade pattern)" http://blog.csdn.net/yanbober/article/ details/45476527
Overview
When a software system produces too many objects at run time, it will cause problems such as high running costs and degraded system performance. So a share is needed to avoid the overhead of having a large number of objects with the same content. In Java, the string type is the use of the enjoy meta mode. The string object is the final type, and the object cannot be changed once it is created. In Java, where string constants are present in a constant pool, Java ensures that a string constant has only one copy in the constant pool.
Core
Concept: the use of sharing 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, you can achieve multiple reuse of objects. Because the enjoy meta mode 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.
About the basics of sharing:
The key to sharing a shared meta object is to differentiate between internal states (intrinsic State) and external states (extrinsic state).
Internal state
The state that is stored inside the element of the object and does not change with the environment, and the internal state can be shared.
External state
The external state of the enjoy meta object is typically saved by the client, and is then passed into the object's interior once it is created. A state that cannot be shared as the environment changes. An external state is independent from the other external state.
Because of the distinction between the internal state and the external state, we can store objects with the same internal state in the pool, and the objects in the pool can be shared, and when needed, the objects are removed from the pool to achieve the reuse of objects. By injecting different external states into the object being fetched, you can get a series of similar objects that actually store only one copy in memory.
Enjoy meta-mode classification:
- Simply enjoy meta mode
- Compound enjoy meta mode
Simple to enjoy meta-mode structure important core modules:
Abstract enjoy meta role
Specifies the method that must be implemented for the specific privilege role, and the external state is passed in as a parameter through this method. In Java, it can be assumed by an abstract class, an interface.
Specific enjoy meta roles
The method of implementing an abstract role stipulation. If there is an internal state, it is responsible for providing storage space for the internal state.
Enjoy the meta-factory role
Responsible for creating and managing the privileges role. To achieve the purpose of sharing, the role of the implementation is the key!
Client role
Maintains a reference to all of the object's objects, and it also needs to store the corresponding external state.
The simple-to-enjoy meta-mode is very similar to the built-in simplicity of the factory model, but its focus or intent is quite different from the factory model. The factory model is used primarily to make the system independent of the implementation details, while the main purpose of the enjoy meta-mode is to avoid the overhead of having the same content object in large numbers.
Complex to enjoy meta-mode structure important core modules:
Abstract enjoy meta role
Specifies the method that must be implemented for the specific privilege role, and the external state is passed in as a parameter through this method. In Java, it can be assumed by an abstract class, an interface.
Specific enjoy meta roles
The method of implementing an abstract role stipulation. If there is an internal state, it is responsible for providing storage space for the internal state.
Compound enjoy meta role
The object it represents is not shareable and can be decomposed into a combination of multiple simple-to-enjoy meta-objects.
Enjoy the meta-factory role
Responsible for creating and managing the privileges role. To achieve the purpose of sharing, the role of the implementation is the key!
Client role
Maintains a reference to all of the object's objects, and it also needs to store the corresponding external state.
Usage Scenarios
A system has a large number of identical or similar objects, resulting in a large amount of memory consumption.
Most of the state of an object can be externally passed in to an object.
When you use the enjoy meta-mode, you need to maintain a pool of privileges that stores the metadata of the objects, which requires a certain amount of system resources, so it should be worthwhile to use the enjoy meta-mode when you need to reuse the object multiple times.
Program Ape Instance
simple to enjoy meta-mode example: The example is the core point of the text translation code, do not do too much explanation.
PackageYanbober.github.io;ImportJava.util.HashMap;ImportJava.util.Map;//Abstract enjoy Meta role classInterface Icustomerstring {The //external state is passed through this method in the form of a parameter voidOpt (String state);}///Specific enjoy Meta role classClass Customerstringimpl implements Icustomerstring {//responsible for providing storage space for internal state PrivateCharacter minnerstate =NULL; Public Customerstringimpl(Character minnerstate) { This. minnerstate = minnerstate; }@Override Public void opt(String State) {System.out.println ("Inner state ="+ This. minnerstate); System.out.println ("Out state ="+state); }}//Enjoy meta factory role class//In general, the Meta factory object has only one in the entire system, so you can also use singleton modeClass Customerstringfactory {PrivateMap<character, icustomerstring> map =NewHashmap<> (); PublicIcustomerstringFactory(Character State) {icustomerstring cachetemp = Map.get (state);if(Cachetemp = =NULL) {cachetemp =NewCustomerstringimpl (state); Map.put (state, cachetemp); }returnCachetemp; }}//Client Public class Main { Public Static void Main(string[] args) {Customerstringfactory factory =NewCustomerstringfactory (); Icustomerstring customerstring = Factory.factory (NewCharacter (' Y ')); Customerstring.opt ("Yanbo"); customerstring = Factory.factory (NewCharacter (' B ')); Customerstring.opt ("Bob"); customerstring = Factory.factory (NewCharacter (' Y ')); Customerstring.opt ("Jesse"); }}
Operation Result:
Inner state = Y
Out state = Yanbo
Inner state = B
Out state = Bob
Inner state = Y
Out state = Jesse
The above example results can be seen at a glance to simple to enjoy the characteristics of meta-mode.
Composite enjoy meta-mode instances:
The following example is a composite element mode, adding a composite object, as follows:
PackageYanbober.github.io;Importjava.util.*;//Abstract enjoy Meta role classInterface Icustomerstring {The //external state is passed through this method in the form of a parameter voidOpt (String state);}///Specific enjoy Meta role classClass Customerstringimpl implements Icustomerstring {//responsible for providing storage space for internal state PrivateCharacter minnerstate =NULL; Public Customerstringimpl(Character minnerstate) { This. minnerstate = minnerstate; }@Override Public void opt(String State) {System.out.println ("Inner state ="+ This. minnerstate); System.out.println ("Out state ="+state); }}//Compound -to-enjoy meta-objectClass Multiplecustomerstringimpl implements Icustomerstring {PrivateMap<character, icustomerstring> map =NewHashmap<> (); Public void Add(Character key, icustomerstring value) {Map.put (key, value); }@Override Public void opt(String State) {Icustomerstring temp; for(Character Obj:map.keySet ()) {temp = Map.get (obj); Temp.opt (state); } }}//Enjoy meta factory role classClass Customerstringfactory {//In general, the Meta factory object has only one in the entire system, so you can also use singleton mode PrivateMap<character, icustomerstring> map =NewHashmap<> ();//The simple-to-enjoy meta-mode of the above example PublicIcustomerstringFactory(Character State) {icustomerstring cachetemp = Map.get (state);if(Cachetemp = =NULL) {cachetemp =NewCustomerstringimpl (state); Map.put (state, cachetemp); }returnCachetemp; }//compound to enjoy meta mode PublicIcustomerstringFactory(list<character> states) {Multiplecustomerstringimpl Impl =NewMultiplecustomerstringimpl (); for(Character state:states) {Impl.add (state, This. Factory (state)); }returnImpl }}//Client Public class Main { Public Static void Main(string[] args) {list<character> states =NewArraylist<> (); States.add (' Y '); States.add (' A '); States.add (' N '); States.add (' B '); States.add (' O '); States.add (' Y '); States.add (' B '); Customerstringfactory factory =NewCustomerstringfactory (); Icustomerstring customerString1 = Factory.factory (states); Icustomerstring customerString2 = Factory.factory (states); Customerstring1.opt ("Mutex Object test!"); }}
sum up a
From the above code you can find that because of the complexity of the meta-mode, the actual application is not many, it is more difficult for us to see his true colors. However, the enjoyment of meta-model is not a chicken, it is the essence of sharing, is the optimization of our system is very beneficial, and this idea has not been more and more applications, this should be considered as the application of the meta-mode.
Benefits of the meta-mode:
- Can greatly reduce the number of objects in memory, so that the same or similar objects in memory only one copy, which can save system resources, improve system performance.
- The external state of the enjoy meta-mode is relatively independent and does not affect its internal state, allowing the enjoyment meta-object to be shared in different environments.
The disadvantage of the meta mode:
- The sharing meta-mode makes the system complex and requires the separation of internal and external states, which complicates the logic of the program.
- In order for objects to be shared, the enjoy meta-mode requires that part of the state of the enjoy meta-object be externally instantiated, while reading the external state will make the runtime longer.
"Craftsman Joshui Http://blog.csdn.net/yanbober" Continue reading "to be continued ... 》
design mode (structured) to enjoy the meta-mode (Flyweight pattern)