The beauty of the design model: Flyweight)

Source: Internet
Author: User

Implementation Method of the index intent structure participant applicability effect-related mode (1): Use FlyweightFactory to manage Flyweight objects. Intention to use the sharing technology to effectively support a large number of fine-grained objects. Use sharing to support large numbers of fine-grained objects efficiently. The object diagram below the structure shows how to share Flyweight: the participant Flyweight describes an interface through which Flyweight can accept and act on external states. ConcreteFlyweight implements the Flyweight interface and adds storage space for internal states. This object must be shareable. It must be stored in an internal state, that is, it must be independent of the object scenario. UnsharedConcreteFlyweight not all Flyweight subclasses need to be shared. The Flyweight interface makes sharing possible, but it does not force sharing. FlyweightFactory creates and manages Flyweight objects. Ensure that Flyweight is properly shared. The Client maintains a reference to Flyweight. Calculate or store the external status of Flyweight. The validity of the applicable Flyweight mode depends largely on how it is used and where it is used. You can use the Flyweight mode when the following conditions are met: An application uses a large number of objects. Because a large number of objects are used, the storage overhead is very high. Most States of an object can be changed to external states. If you delete the external state of an object, you can replace multiple groups of objects with a relatively small number of shared objects. Applications do not rely on object identifiers. The savings in the performance bucket offset the overhead for transferring, searching, and calculating external states. The savings increase as the sharing status increases. The related mode Flyweight mode is usually combined with the Composite mode to implement a logical hierarchy using the directed acyclic graph of the shared leaf node. Generally, it is best to use Flyweight to implement State and Strategy objects. Implementation Method (1): Use FlyweightFactory to manage Flyweight objects. The availability of the Flyweight mode depends largely on whether it is easy to identify external States and delete them from shared objects. Ideally, the external State can be calculated by a separate object structure, and the storage requirements of this structure are very small. Generally, because the Flyweight object is shared, users cannot instantiate it directly, because FlyweightFactory can help users find a specific Flyweight object. Sharing also means some form of reference counting and garbage collection. Copy code 1 namespace FlyweightPattern. implementation1 2 {3 public abstract class Flyweight 4 {5 public abstract string Identifier {get;} 6 public abstract void Operation (string extrinsicState); 7} 8 9 public class ConcreteFlyweight: flyweight10 {11 public override string Identifier12 {13 get {return "hello";} 14} 15 16 public override void Operation (string extrinsicState) 17 {18 // do something1 9} 20} 21 22 public class FlyweightFactory23 {24 private Dictionary <string, Flyweight> _ pool25 = new Dictionary <string, Flyweight> (); 26 27 public Flyweight CreateFlyweight (string identifier) 28 {29 if (! _ Pool. containsKey (identifier) 30 {31 Flyweight flyweight = new ConcreteFlyweight (); 32 _ pool. add (flyweight. identifier, flyweight); 33} 34 35 return _ pool [identifier]; 36} 37} 38 39 public class Client40 {41 public void TestCase1 () 42 {43 FlyweightFactory factory = new FlyweightFactory (); 44 Flyweight flyweight1 = factory. createFlyweight ("hello"); 45 Flyweight flyweight2 = factory. createFlyweight ("hello"); 46 flyweight1.Operation ("extrinsic state"); 47 flyweight2.Operation ("extrinsic state"); 48} 49} 50}

Related Article

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.