Enjoy meta mode (Flyweight pattern)

Source: Internet
Author: User

Flyweight pattern: Use shared technology to effectively support a large number of fine-grained objects.

Flyweight is a shared object that can be used in multiple scenarios (context) at the same time, and Flyweight can act as a standalone object in each scene-this is no different from an instance of a non-shared object. Flyweight cannot make any assumptions about the scenario it is running, the key concept here is the difference between the internal state and the external state . The internal state is stored in flyweight, which contains information that is independent of the flyweight scene, which allows flyweight to be shared. The external state depends on the flyweight scene and changes according to the scene, so it is not shareable. the user object is responsible for passing the external state to flyweight when necessary. The flyweight model models a concept or entity that is often too large to be represented by objects.

Structure

Flyweight

--Describes an interface through which flyweight can accept and act on an external state.

Concreteflyweight

--Implement the flyweight interface and increase 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.

Unsharedconcreteflyweight

-Not all flyweight subclasses need to be shared. A flyweight interface makes sharing possible, but it does not force sharing.

at some level of the flyweight object structure Unsharedconcreteflyweight objects typically use Concreteflyweight objects as child nodes.

Flyweightfactory

--Create and manage flyweight objects.

--Ensure that flyweight is properly shared. When the user requests a Flyweigh, the Flyweightfactory object provides a created instance or creates one (if it does not exist).

Client

--Maintain a reference to the flyweight.

-Calculates or stores the external state of a (multiple) flyweight.

Collaboration

The state required for flyweight execution must be internal or external. The internal state is stored in the Concreteflyweight object, while the external object is stored or computed by the client object. When the user invokes the action of the flyweight object, the state is passed to it. Users should not instantiate the Concreteflyweight class directly, but should get concreteflyweight objects from the Flyweightfactory object, which ensures that they are properly shared.

Effect

There are two ways to conserve storage: use sharing to reduce internal state consumption, and calculate time for external state storage. (Space change time)

Using the enjoy meta-mode requires maintaining a list of all the elements that are already in the system, which in itself consumes resources, and additional meta-mode makes the system more complex. In order for objects to be shared, some States need to be externally instantiated, which complicates the logic of the program.

Realize

1) Delete the external state;

2) Manage shared objects.

Reference Code

[CSharp] view plaincopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Collections;
  6. Namespace Myflyweight
  7. {
  8. abstract class Flyweight
  9. {
  10. public abstract void operation (int extrinsicstate);
  11. }
  12. class Concreteflyweight:flyweight
  13. {
  14. private int intrinsicstate = 0;
  15. public override void operation (int extrinsicstate)
  16. {
  17. Console.WriteLine ("Concrete flyweight:{0}",
  18. Intrinsicstate + extrinsicstate);
  19. }
  20. }
  21. class Unsharedconcreteflyweight:flyweight
  22. {
  23. private int allState = 0;
  24. public override void operation (int extrinsicstate)
  25. {
  26. Console.WriteLine ("unshared Concrete flyweight:{0}", extrinsicstate);
  27. }
  28. }
  29. class Flyweightfactory
  30. {
  31. private Hashtable flyweights = new Hashtable ();
  32. Public flyweightfactory ()
  33. {
  34. Flyweights.  ADD ("X", new Concreteflyweight ());
  35. Flyweights.  ADD ("Y", new Concreteflyweight ());
  36. Flyweights.  ADD ("Z", new Concreteflyweight ());
  37. }
  38. Public Flyweight getflyweight (string key)
  39. {
  40. return ((Flyweight) flyweights[key]);
  41. }
  42. }
  43. }
[CSharp] view plaincopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Namespace Myflyweight
  6. {
  7. Class Program
  8. {
  9. static void Main (string[] args)
  10. {
  11. int extrinsicstate = 23;
  12. Flyweightfactory f = new Flyweightfactory ();
  13. Flyweight FX = f.getflyweight ("X");
  14. Fx. Operation (--extrinsicstate);
  15. Flyweight fy = f.getflyweight ("Y");
  16. Fy. Operation (--extrinsicstate);
  17. Flyweight FZ = F.getflyweight ("Z");
  18. Fz. Operation (--extrinsicstate);
  19. var uf = new Unsharedconcreteflyweight ();
  20. uf. Operation (--extrinsicstate);
  21. Console.readkey ();
  22. }
  23. }
  24. }

application . NET, the string class is using the flyweight pattern.

[CSharp] view plaincopy < param name= "Menu" value= "false" >
    1. String Titlea = "Flyweight pattern";
    2. String Titleb = "Flyweight pattern";
    3. Console.WriteLine (Object.referenceequals (Titlea, Titleb));

Reference: "design mode", "Big Talk design mode"

Enjoy meta mode (Flyweight pattern)

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.