Java design pattern--Structural mode----Enjoy meta-mode

Source: Internet
Author: User

enjoy meta-mode overview use shared technology to effectively support a large number of fine-grained objects. Applicability use flyweight mode when all of the following conditions are in place:1. An application uses a large number of objects. 2completely because of the large number of objects that are used, the storage overhead is high. 3most of the state of an object can become an external state. 4If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects. 5the 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. Participants1.    Flyweight describes an interface that Flyweight can accept and act on an external state through this interface. 2.      The Concreteflyweight implements the flyweight interface and increases the storage space for the internal state, 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 subclasses 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). 

Test class:

1  Public classTest {2 3      Public Static voidMain (string[] args) {4Flyweight fly1 = Flyweightfactory.getflyweight ("a");5Fly1.action (1);6         7Flyweight fly2 = Flyweightfactory.getflyweight ("a");8System.out.println (Fly1 = =fly2);9         TenFlyweight fly3 = flyweightfactory.getflyweight ("b"); OneFly3.action (2); ASystem.out.println (fly1==fly3); -          -Flyweight fly4 = flyweightfactory.getflyweight ("C"); theFly4.action (3); -          -Flyweight fly5 = flyweightfactory.getflyweight ("D"); -Fly5.action (4); +          - System.out.println (Flyweightfactory.getsize ()); +     } A}

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4  Public classFlyweightfactory {5 6     Private Staticmap<string, flyweight> flyweights =NewHashmap<string, flyweight>();7     8      Publicflyweightfactory (String Arg) {9Flyweights.put (ARG,NewFlyweightimpl ());Ten     } One      A      Public StaticFlyweight getflyweight (String key) { -         if(Flyweights.get (key) = =NULL) { -Flyweights.put (Key,NewFlyweightimpl ()); the         } -         returnFlyweights.get (key); -     } -      +      Public Static intGetSize () { -         returnflyweights.size (); +     } A}

 Public Interface Flyweight {    void action (int  arg);}

1  Public class Implements Flyweight {23      Public void Action (int  arg) {4         //  TODO auto-generated method Stub5         System.out.println ("parameter value:" + arg); 6     }7 }

Java design pattern--Structural mode----Enjoy meta-mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.