Flyweight Examples in Java
Asked Answered
P

5

7

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!

Plumbago answered 27/9, 2011 at 2:0 Comment(2)
JTable Editors and Renderers are a good example.Petroglyph
Here's an explanation of the flyweight pattern: softwareengineering.stackexchange.com/questions/108630/…Schacker
M
3

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!");
}
Marcheshvan answered 27/9, 2011 at 2:5 Comment(6)
Essentially, when allocating an object, calling static on it just makes it flyweight?Plumbago
Er, no? The point of the flyweight pattern is that you're sharing a single instance of something rather than creating new, identical objects. The example given in the Wiki demonstrates this; there's nothing static involved. The factory only creates a new instance of a CoffeeFlavor the first time a specific flavor is requested. After that it returns a reference to that single instance of the flavor.Marcheshvan
How is there nothing static? static CoffeeFlavor[] flavorsPlumbago
That has nothing to do with the flyweight object. That's just in their test class to show the functionality.Marcheshvan
Got it - it's the factory that creates the instance of the "singleton" class.Plumbago
Right. A distinction though is that the factory itself is not a singleton (in this example); if you instantiated two factories, you'd (potentially) have multiple instances of a given flavor. Making the factory a singleton would of course solve that.Marcheshvan
B
1

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?

Brickwork answered 27/9, 2011 at 3:0 Comment(0)
C
1

I got this case. I think my solution was flyweight.

INPUT

  • A: C E
  • B: D C
  • C: E
  • A: B

It asked me to create a tree and sort its children by name. Something like this:

  • A: B C E
  • B: C D
  • C: E

It's an easy task actually. But please notice that the first 'A' and the second 'A' in the input must refer to same object. Hence I coded something like this

public Node add(String key){
  Node node = nodes.get(key);
  if (null == node){
    node = new Node(key);
    nodes.put(key, node);
  }
  return node;
}

This is the simplified version of the actual problem, but you should have the idea now.

Concave answered 27/9, 2011 at 3:4 Comment(1)
That's actually very similar to what I'm doing. I am creating a PRQuadtree. The Node class is an abstract class. The empty leaf node is a flyweight object.Plumbago
P
0

I also found this example, which has good Java code example.

Plumbago answered 27/9, 2011 at 3:4 Comment(0)
S
0

The "java.lang.Character" uses the flyweight pattern to cache all US-ASCII characters : see in class java.lang.Character$CharacterCache used by the Character.valueOf() method

Squire answered 9/1, 2014 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.