Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyweight Examples in Java

I am trying to create a flyweight object in Java. I've worked with a similar concept in Objective-C (Singleton Classes in Objective-C // I believe they are the same thing).

I am trying to find a tutorial or an example or explanation online to learn how to create a flyweight object and use it, but I've searched on Google and I can't find anything descent. I went through 10 pages and they basically all plagiarize from one website which just explains the concept. I understand the concept - I need something to help me/teach me how to implement it in Java.

Anyone has any suggestions/tutorials?

Thanks!

like image 774
darksky Avatar asked Sep 27 '11 02:09

darksky


People also ask

What is flyweight in Java?

Flyweight is a structural design pattern that allows programs to support vast quantities of objects by keeping their memory consumption low. The pattern achieves it by sharing parts of object state between multiple objects. In other words, the Flyweight saves RAM by caching the same data used by different objects.

How is flyweight implemented in Java?

To apply flyweight pattern, we need to divide Object property into intrinsic and extrinsic properties. Intrinsic properties make the Object unique whereas extrinsic properties are set by client code and used to perform different operations.

Where is flyweight pattern used?

Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.

How does a flyweight pattern work?

The Flyweight pattern describes how to share objects to allow their use at fine granularity without prohibitive cost. Each "flyweight" object is divided into two pieces: the state-dependent (extrinsic) part, and the state-independent (intrinsic) part. Intrinsic state is stored (shared) in the Flyweight object.


2 Answers

The Wikipedia entry for the flyweight pattern has a concrete Java example.

EDIT to try and help the OP understand the pattern:

As noted in my comment below, The point of the flyweight pattern is that you're sharing a single instance of something rather than creating new, identical objects.

Using the Wiki example, the CoffeeFlavorFactory will only create a single instance of any given CoffeeFlavor (this is done the first time a Flavor is requested). Subsequent requests for the same flavor return a reference to the original, single instance.

public static void main(String[] args) 
{
    flavorFactory = new CoffeeFlavorFactory();
    CoffeeFlavor a = flavorFactory.getCoffeeFlavor("espresso");
    CoffeeFlavor b = flavorFactory.getCoffeeFlavor("espresso");
    CoffeeFlavor c = flavorFactory.getCoffeeFlavor("espresso");

    // This is comparing the reference value, not the contents of the objects
    if (a == b && b == c)
        System.out.println("I have three references to the same object!");
}
like image 75
Brian Roach Avatar answered Sep 19 '22 14:09

Brian Roach


To follow up on the Wikipedia example that Brian cited...

Usually, if you want to cache some objects (such as CoffeeFlavors) and have them shared between a number of flyweights (the CoffeeOrders), then you would make them statically available. But this is not at all necessary. The important part is that the CoffeeOrders are being given the shared objects when they're constructed.

If the Orders are always only created by one singleton, like a "CoffeeOrderFactory," then the factory can keep a non-static cache of Flavors. However you accomplish it, your goal is to get all the Orders in the whole system to use the same exact set of Flavor objects. But at the end of the day, if you want to avoid creating many instances of CoffeeFlavor, then it usually needs to be created statically, just to make sure there's only one cache.

Get it?

like image 36
Neil Traft Avatar answered Sep 21 '22 14:09

Neil Traft