[Mode overview] ---------- by xingoo
Pattern intent
The enjoy mode is also called the lightweight mode or the fly level mode ]. The main purpose is to reduce the consumption of fine-grained resources. For example, an editor uses a large number of letters, numbers, and symbols, but you do not need to create a letter object every time. You only need to share it somewhere, record the use context created each time.
For example, the restaurant's tables and tableware are the embodiment of the Yuan-sharing model. Customers are mobile, and the tableware is fixed in the hotel every time they eat, and the hotel does not need to buy new plate tableware every time new customers arrive.
Application scenarios
1. A system applies a large number of objects, and many of them are repeated.
2. The use of a large number of objects causes storage efficiency overhead.
3. The State of an object is mostly external and does not interfere with the State itself.
4. If you remove these external states, you can use a group of small-scale objects to represent shared objects.
This application scenario is available in recent projects. For example, some file icons can be used repeatedly and put into the cache, in the future, each call can be directly read from the cache.
Mode Structure
FlyweightfactortyThe factory method that provides shared objects contains a clustering object. Generally, hashmap is used. Generally, this factory class is created in singleton mode.
1 class FlyweightFactory{ 2 private HashMap map = new HashMap(); 3 4 public FlyweightFactory() { 5 } 6 7 public Flyweight factory(int state){ 8 if(map.containsKey(state)){ 9 return (Flyweight)map.get(state);10 }else{11 map.put(state, new ConcreteFlyweight(state));12 return (Flyweight)map.get(state);13 }14 }15 16 public void CheckMap(){17 System.out.println("*****************************************");18 int i=0;19 for(Iterator it=map.entrySet().iterator();it.hasNext(); ){20 Map.Entry e = (Map.Entry)it.next();21 System.out.println("map.get("+(i++)+") : "+ e.getKey());22 }23 System.out.println("*****************************************");24 }25 }
FlyweightShared object interface, Uniform Description identifier
1 interface Flyweight{2 public int getState();3 }
ConcreteflyweightReal implementation class
1 class ConcreteFlyweight implements Flyweight{2 private int state;3 public ConcreteFlyweight(int state) {4 this.state = state;5 }6 public int getState() {7 return state;8 }9 }
All code
package com.xingoo.Flyweight;import java.util.HashMap;import java.util.Iterator;import java.util.Map;class FlyweightFactory{ private HashMap map = new HashMap(); public FlyweightFactory() { } public Flyweight factory(int state){ if(map.containsKey(state)){ return (Flyweight)map.get(state); }else{ map.put(state, new ConcreteFlyweight(state)); return (Flyweight)map.get(state); } } public void CheckMap(){ System.out.println("*****************************************"); int i=0; for(Iterator it=map.entrySet().iterator();it.hasNext(); ){ Map.Entry e = (Map.Entry)it.next(); System.out.println("map.get("+(i++)+") : "+ e.getKey()); } System.out.println("*****************************************"); }}interface Flyweight{ public int getState();}class ConcreteFlyweight implements Flyweight{ private int state; public ConcreteFlyweight(int state) { this.state = state; } public int getState() { return state; }}public class Client { public static void main(String[] args) { FlyweightFactory factory = new FlyweightFactory(); factory.factory(4); factory.factory(2); factory.factory(2); factory.factory(1); factory.factory(1); factory.factory(3); factory.CheckMap(); }}
Running result
*****************************************map.get(0) : 1map.get(1) : 2map.get(2) : 3map.get(3) : 4*****************************************
[Design mode] -- enjoy the metadata mode flyweight