|
-
December 24th, 2008, 03:22 PM
#1
Flyweight Pattern
Hi!
Please explain about Flyweight Pattern
From this site:http://www.allapplabs.com/java_desig...ht_pattern.htm
The typical example you can see on this in every book will be of folders. The folder with name of each of the company employee on it, so, the attributes of class Folder are: ‘Selected’ , ‘Not Selected’ and the third one is ‘employeeName’. With this methodology, we will have to create 2000 folder class instances for each of the employees.
I cann't understand this example..May you someone expalin more about that
-
December 25th, 2008, 05:28 AM
#2
Re: Flyweight Pattern
The article is poorly written, but did you read the rest of it about the grinding wheel example?
Have you tried Googling for other examples?
Exactly what is it that you don't understand?
One can think effectively only when one is willing to endure suspense and to undergo the trouble of searching...
J. Dewey
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 25th, 2008, 01:02 PM
#3
Re: Flyweight Pattern
The concept of the FlyWeight pattern is very simple. It is a concrete (instanciatable) class that has (ideally) NO data members. It exists to encapsulate FUNCTIONALLITY but NOT state.
Because they take little or no memory, it becomes posible to instanciate a great quantity of these instances.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 5th, 2009, 02:30 PM
#4
Re: Flyweight Pattern
I cann't understand the code in that article,I need explain by code!
-
January 5th, 2009, 03:14 PM
#5
Re: Flyweight Pattern
 Originally Posted by Abalfazl
I cann't understand the code in that article,I need explain by code!
Probably because that is such a poor example.
Consider instead a Mailing list. One design would be to implement a class which maintained the state for an individual addresse, along with a collection of these objects.
If you werre to use a flyweight patterns. You would maintain the state in the collection itself, and the individual addressee class would implement its accessors by referencing the collection.
psuedo-code
Code:
class addressee
{
private int ID;
public string get_Name { return MailingList.get_Name(ID); }
}
The size of each addressee now drops from a fairly large object (say 4-7 fields of up to 50 characters) down to just the side of an integer.
This makes it easy to replace an im-memory store of the mailing list collection, with a file or database store, and eliminate the memory utilization without having to go and re-work your addressee class.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 11th, 2009, 03:31 PM
#6
Re: Flyweight Pattern
http://www.developer.com/design/article.php/3677501
If I use the flyweight pattern so that I only store unique Time instances, I can reduce the maximum number of typical time objects to 96 (24 hours times four possible times per hour). The appointment application will still support odd times, but predictable use suggests that flyweight will dramatically minimize object creation and memory usage.
When we use flyweight we share common data between objects,Which data in these objects iin this example are similar that he share?
-
January 11th, 2009, 04:07 PM
#7
Re: Flyweight Pattern
 Originally Posted by Abalfazl
Which data in these objects iin this example are similar that he share?
In that example whole Time objects are shared.
Time objects are not created using new directly. Instead a factory method is called. The factory method keeps track of all Time objects it creates. If a specific Time object already has been created before, the factory method returns a reference to this existing object rather than creating a new one. So if you do,
Time t1 = TimeFactory.create(12, 50);
Time t2 = TimeFactory.create(12, 50);
the factory method (called create) will make sure both t1 and t2 refers to the same object. So t1==t2 will be true. This is the Flyweight sharing.
Note that String literals are treated according to the same principle. If you do,
String s1 = "hello";
String s2 = "hello";
Then both s1 and s2 will refer to the same String object. So s1==s2 will be true.
Also note that this kind of Flyweight sharing requires objects to be immutable (after creation the object state cannot be changed).
Last edited by _uj; January 13th, 2009 at 06:35 AM.
-
January 12th, 2009, 07:04 PM
#8
Re: Flyweight Pattern
Integers are also treated the same way.
I found it interesting to open up the code for Integer. You'll see that the first call goes through the first hundred or so integers and creates objects for them. Then each subsequent call returns that object.
This way if you have 1000 Integer(0) objects in reality its just one. Its an excellent way to save memory.
-
January 13th, 2009, 06:27 AM
#9
Re: Flyweight Pattern
 Originally Posted by compavalanche
I found it interesting to open up the code for Integer.
In fact this is not true just for the particular Java implementation you happened to look at. This behaviour is defined by the Java language. The JLS states that Integers in the -128 to 127 range are to be given the Flyweight sharing treatment (but it doesn't use these exact words ).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|