Design Mode -- flyweight)

Source: Internet
Author: User
Design Mode (12): flyweight)

Flyweight)
Definition
Use the sharing technology to effectively support a large number of fine-grained objects.
Overview
The object-oriented thinking solves the abstract problem well, and generally does not have performance problems. However, in some cases, the number of objects may be too large, resulting in the runtime cost. So how can we avoid a large number of fine-grained objects without affecting customers?ProgramAre operations performed in an object-oriented manner?

Meta Mode(Flyweight)
Flyweight class, which is a superclass or interface of all the Metadata classes. Through this interface, flyweight can accept and act on the external state.
Abstract   Class Flyweight
{
Public   Abstract   Void Operation ( Int Extrinsicstate );
}

Concreteflyweight inherits the flyweight superclass or implements the flyweight interface. And adds storage space for the internal status.

Class Concreteflyweight: flyweight
{
Public   Override   Void Operation ( Int Extrinsicstate)
{
Console. writeline ( " Specific flyweight: "   + Extrinsicstate );
}
}

Unsharedconcreteflyweight refers to the flyweight subclasses that do not need to be shared, because the flyweight interface sharing is possible, but it is not forced to share.

Class Unsharedconcreteflyweight: flyweight
{
Public   Override   Void Operation ( Int Extrinsicstate)
{
Console. writeline ( " Do not share the specific flyweight: "   + Extrinsicstate );
}
}

Flyweightfactory is a metadata factory used to create and manage flyweight objects. It is mainly used to ensure reasonable sharing of Flyweight,
When a user requests a flyweight, The flyweightfactory object provides a created instance or creates one (if it does not exist)

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]);
}
}

Client Code:

Static   Void Main ( String [] ARGs)
{
Int Extrinsicstate = 22 ;
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 );

Unauthenticated concreteflyweight UF = New Unmarshconcreteflyweight ();
Uf. Operation ( -- Extrinsicstate );
}

Result:

Extrinsicstate: 21
Extrinsicstate: 20
Extrinsicstate: 19
Extrinsicstate: 18

In. NET Framework, hashtable is a container provided by the system. Collections namespace. It is used to process and present key-value pairs similar to key/value.

Keys are usually used for quick search, and keys are case-sensitive. values are used to store values corresponding to keys. In hashtable, key/value pairs are of the object type, so hashtable can support any type of key/value pairs.

Ii. Simple operations on Hash Tables
Add a key/value pair in the hash table: hashtableobject. Add (Key, value );
Remove a key/value pair in the hash table: hashtableobject. Remove (key );
Remove all elements from the hash table: hashtableobject. Clear ();
Determine whether the hash table contains the specified key: hashtableobject. Contains (key );

A shared part that does not change with the environment inside the object. It can be called the internal state of the object, but changed with the environment change, the State that cannot be shared is the external state.

The metadata mode avoids the overhead of a large number of similar classes. In program design, a large number of fine-grained class instances are sometimes required to represent data. If we can find that these instances are basically the same except several parameters, sometimes the number of classes to be instantiated can be greatly reduced. If you can move those parameters outside the class instance and pass them during method calling, you can greatly reduce the number of individual instances by sharing them. That is to say, when the flyweight mode is executed, the state is internal or external, and the internal state is stored in the concreteflyweight object, the external object should be stored or computed by the client object. When the flyweight object operation is called, the state is passed to it.

Public   Class User
{
Private   String Name;
Public User ( String Name)
{
This . Name = Name;
}
Public   String Name
{
Get { Return Name ;}
}
}
Abstract   Class Website
{
Public   Abstract   Void Use (User user ); // The "use" method must pass the "user" Object
}

Class Concretewebsite: Website
{
Private   String Name = "" ;
Public Concretewebsite ( String Name)
{
This . Name = Name;
}
Public   Override   Void Use (User user) // Implement the "use" Method
{
Console. writeline ( " Website category: " + Name + "User :" + User. Name );
}
}

Class Websitefatory
{
Private Hashtable flyweights = New Hashtable ();
Public Website getwebsitecategory ( String Key)
{
If ( ! Flyweight. containskey (key ))
Flyweight. Add (key, New Concretewebsite (name ));
Return (Website) flyweights [Key]);
}
Public   Int Getwebsitecount ()
{
Return Flyweight. count;
}
}

Static   Void Main ( String ARGs)
{
Websitefactory F = New Websitefactory ();

Website FX = F. getwebsitecategory ( " Product presentation " );
FX. Use ( New User ( " James " ));

Website FY = F. getwebsitecategory ( " Product presentation " );
FY. Use ( New User ( " Xiaoyang " ));

Website FZ = F. getwebsitecategory ( " Blog " );
FZ. Use ( New User ( " Yangfan " ));

Website fl = F. getwebsitecategory ( " Blog " );
Fl. Use ( New User ( " Tait " ));
Console. writeline ( " The total number of website categories is {0} " , F. getwebsitecount ());
}

Display result
Website category: Product Display User: James
Website category: Product Display User: Xiaoyang
Website category: blog User: Yangfan
Website category: blog User: tatte
The total number of website categories is 2

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.