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
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Collections;
- Namespace Myflyweight
- {
- abstract class Flyweight
- {
- public abstract void operation (int extrinsicstate);
- }
- class Concreteflyweight:flyweight
- {
- private int intrinsicstate = 0;
- public override void operation (int extrinsicstate)
- {
- Console.WriteLine ("Concrete flyweight:{0}",
- Intrinsicstate + extrinsicstate);
- }
- }
- class Unsharedconcreteflyweight:flyweight
- {
- private int allState = 0;
- public override void operation (int extrinsicstate)
- {
- Console.WriteLine ("unshared Concrete flyweight:{0}", extrinsicstate);
- }
- }
- class Flyweightfactory
- {
- private Hashtable flyweights = new Hashtable ();
- Public flyweightfactory ()
- {
- Flyweights. ADD ("X", new Concreteflyweight ());
- Flyweights. ADD ("Y", new Concreteflyweight ());
- Flyweights. ADD ("Z", new Concreteflyweight ());
- }
- Public Flyweight getflyweight (string key)
- {
- return ((Flyweight) flyweights[key]);
- }
- }
- }
[CSharp] view plaincopy
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Namespace Myflyweight
- {
- Class Program
- {
- static void Main (string[] args)
- {
- int extrinsicstate = 23;
- Flyweightfactory f = new Flyweightfactory ();
- Flyweight FX = f.getflyweight ("X");
- Fx. Operation (--extrinsicstate);
- Flyweight fy = f.getflyweight ("Y");
- Fy. Operation (--extrinsicstate);
- Flyweight FZ = F.getflyweight ("Z");
- Fz. Operation (--extrinsicstate);
- var uf = new Unsharedconcreteflyweight ();
- uf. Operation (--extrinsicstate);
- Console.readkey ();
- }
- }
- }
application . NET, the string class is using the flyweight pattern.
[CSharp] view plaincopy < param name= "Menu" value= "false" >
- String Titlea = "Flyweight pattern";
- String Titleb = "Flyweight pattern";
- Console.WriteLine (Object.referenceequals (Titlea, Titleb));
Reference: "design mode", "Big Talk design mode"
Enjoy meta mode (Flyweight pattern)