Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45568799
I. Overview
Use shared technology to effectively support a large number of fine-grained objects.
Second, applicability
Use flyweight mode when all of the following conditions are in place:
1. An application uses a large number of objects.
2. Due to the use of a large number of objects, resulting in a large storage overhead.
3. Most of the state of an object can become an external state.
4. If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects.
5. The application does not depend on the object identity. Because flyweight objects can be shared, the identity test returns true for objects that are conceptually distinct.
Third, participants
1.Flyweight describes an interface that Flyweight can accept and act on an external state through this interface.
The 2.ConcreteFlyweight implements the flyweight interface and increases storage space for internal states, if any. The Concreteflyweight object must be shareable. The state it stores must be internal, that is, it must be independent of the scene of the Concreteflyweight object.
3.UnsharedConcreteFlyweight Not all flyweight sub-classes need to be shared. A flyweight interface makes sharing possible, but it does not force sharing. At certain levels of the flyweight object structure, Unsharedconcreteflyweight objects typically use Concreteflyweight objects as child nodes.
4.FlyweightFactory Create and manage flyweight objects. Ensure that flyweight is properly shared. When the user requests a flyweight, the Flyweightfactory object provides a created instance or creates one (if it does not exist).
Four, class diagram
V. Examples
Flyweight
Package com.lyz.design.flyweight;/** * Flyweight * @author Liuyazhuang * */public interface Flyweight { void action (in t arg);}
Concreteflyweight
Package com.lyz.design.flyweight;/** * Concreteflyweight * @author Liuyazhuang * */public class Flyweightimpl Implements Flyweight {public void action (int arg) { //TODO auto-generated method stub System.out.println (" Parameter value: "+ arg"; }}
flyweightfactory
Package Com.lyz.design.flyweight;import java.util.hashmap;import java.util.map;/** * flyweightfactory * @author Liuyazhuang * */public class Flyweightfactory { private static Map flyweights = new HashMap (); Public flyweightfactory (String Arg) { flyweights.put (ARG, New Flyweightimpl ()); } public static Flyweight Getflyweight (String key) { if (flyweights.get (key) = = null) { flyweights.put (key, New Fly Weightimpl ()); } Return (Flyweight) flyweights.get (key); } public static int GetSize () { return flyweights.size ();} }
Test
Package com.lyz.design.flyweight;/** * Test * @author Liuyazhuang */public class Test {public static void main (St Ring[] (args) { //TODO auto-generated method stub Flyweight fly1 = Flyweightfactory.getflyweight ("a"); Fly1.action (1); Flyweight fly2 = Flyweightfactory.getflyweight ("a"); System.out.println (fly1 = = fly2); Flyweight fly3 = flyweightfactory.getflyweight ("b"); Fly3.action (2); Flyweight fly4 = flyweightfactory.getflyweight ("C"); Fly4.action (3); Flyweight fly5 = flyweightfactory.getflyweight ("D"); Fly4.action (4); System.out.println (Flyweightfactory.getsize ());} }
Talking about Java design mode------------Flyweight