The meaning of the design pattern lies in: Facing the business content, the business data structure and the system architecture, the cohesion is low-coupling, graceful the plane logic three-dimensional.
1 PackageDesignpattern;2 ImportJava.util.HashMap;3 ImportJava.util.Map;4 5 /**6 * Enjoy meta mode7 * @authorAdministrator8 */9 Public classB14_flyweighttest {Ten One /** A * Use sharing technology to effectively support a large number of fine-grained objects. - * Applicable to: - * 1. An application uses a large number of objects. the * 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. + */ - Public Static voidMain (string[] args) { +Flyweight f1 = Flyweightfactory.getflyweight ("a"); AF1.action (1); at -Flyweight F2 = flyweightfactory.getflyweight ("a"); -F2.action (2); - -Flyweight F3 = Flyweightfactory.getflyweight ("b"); -F3.action (3); in -Flyweight F4 = flyweightfactory.getflyweight ("C"); toF4.action (4); + -Flyweight f5 = flyweightfactory.getflyweight ("D"); theF5.action (5); * $Flyweight f6 = flyweightfactory.getflyweight ("e");Panax NotoginsengF6.action (6); -SYSTEM.OUT.PRINTLN ("Size:" +flyweightfactory.getsize ()); the } + } A //1.Flyweight describes an interface that Flyweight can accept and act on an external state through this interface. the InterfaceFlyweight + { - voidActioninti); $ } $ //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. - classFlyweightimplImplementsFlyweight the { - Public voidActioninti) {Wuyi System.out.println (i); the } - } Wu //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. About classUnsharedflyweightimplImplementsFlyweight $ { - Public voidActioninti) { - System.out.println (i); - } A } + //4.FlyweightFactory Create and manage flyweight objects. the //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). - classflyweightfactory $ { the@SuppressWarnings ("Rawtypes") the Private StaticMap flyweights=NewHashMap (); the@SuppressWarnings ("Unchecked") the Publicflyweightfactory (String s) { -Flyweights.put (s),NewFlyweightimpl ()); in } the@SuppressWarnings ("Unchecked") the Public StaticFlyweight getflyweight (String key) About { the if(Flyweights.get (key) = =NULL) { theFlyweights.put (Key,NewFlyweightimpl ()); the } + return(Flyweight) flyweights.get (key); - } the Public Static intGetSize ()Bayi { the returnflyweights.size (); the } -}
Environment: Jdk1.6,maven,tomcat,eclipse
Source Address: Http://files.cnblogs.com/files/xiluhua/designPattern.rar
Welcome to the comments of the pro-members.
Enjoy meta mode