This is the 22nd day of my participation in Gwen Challenge
The flyweight pattern
The share mode can be described as an implementation of object pooling to minimize memory usage and improve performance to avoid memory waste. For scenarios where a large number of duplicate objects are used, sharing objects in a cached form reduces the need to create too many objects. The shareable part of the share object is called the internal state. The internal state does not change with the environment. An unshareable state is an external state that changes with the environment. For example, if the share mode is a Map container, Key represents the internal state of the share object, and Value represents the share object itself.
Share pattern definition: Use shared objects to support a large number of fine-grained objects. Enjoy yuan mode role:
- Flyweight share object abstraction
- ConcreteFlyweight concrete share object
- FlyweightFactory (FlyweightFactory
In actual combat
ConcreteFlyweight implements the Flyweight abstract method and implements its own call to operation. FlyweightFactory The FlyweightFactory is used to hold the ConcreteFlyweight collection of flyweight objects. As described above, the free object is saved in the Map. When you need to create a “Window” object, you will check whether the free object exists in the Map through the free factory. If the free object has not been created, you will create a new object and save the new object in the Map. The second time an object with the same mark “Window” is retrieved from the Map and used directly without the need to recreate the object.
public abstract class Flyweight{public abstract void operation(a);
}
class ConcreteFlyweight extends Flyweight{privateString string;public ConcreteFlyweight(String str){string = STR; }public void operation(a){/ / /...}}public class FlyweightFactory{private Hashtable flyweights = newHashtable();public Flyweight getFlyWeight(String obj){Flyweight Flyweight = (Flyweight) flyweights.get(obj);if(flyweight == null) {flyweight =newConcreteFlyweight (obj); Flyweights. Put (obj, flyweight); }returnThe flyweight. } } ConcreteFlyweight fly1 = FlyweightFactory.getFlyWeight("Window");
fly1.operation();
ConcreteFlyweight fly2 = FlyweightFactory.getFlyWeight("Window");
fly2.operation();
ConcreteFlyweight fly3 = FlyweightFactory.getFlyWeight("Linux");
fly3.operation();
Copy the code
Share elements in Android
The message mechanism in Android source code is used to enjoy the yuan mode. The Android messaging mechanism is roughly implemented through several classes such as handler-message-loop. Android keeps the system running by constantly sending messages, processing messages, and destroying messages. In the process of encapsulating the Message in getPostMessage, it is found that a Message object is returned through the message.obtain () method, rather than creating a new one through new. The object pool will recycle the Message that has been used and processed, and mark the position in the Message as the used state. When a new Message needs to be created, some parameters in the recycling will be overwritten into the new Message content and put back into the linked list, so that the Message pool in the object pool will not increase dramatically. The amount maintained within a reasonable range will not affect memory usage.
conclusion
The implementation of share mode is relatively simple, but the application scenario is more important, especially in the scenario of high memory usage and performance requirements. The advantage of the free mode is to reduce the program memory footprint, enhance the performance of the program. The disadvantage is that the share mode increases the complexity of the system and the need to externalize some state in order to share objects increases the logical complexity.