1

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.

But my question is why can't we have both intrinsic and extrinsic property as an instance variable (See Email class below) and just create one object outside loop and set the parameters in loop and send multiple emails with different parameters.

public class Test {
    public static void main(String[] args) {
        Email ob = new Email();
        for (int i = 0; i < 10; i++) {
            ob.sender = String.valueOf(i);
            ob.sendEmail();
        }
    }
}

public class Email {
    public String sender;
    public void sendEmail()
    {
        System.out.println("Email sent to sender:"+sender);
    }
}
Aditya W
  • 652
  • 8
  • 20
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
  • Indeed Flyweight is not a pattern, a work around to save memory in Java. It has nothing to do with oop – Supun Wijerathne Oct 03 '16 at 05:38
  • 1
    @SupunWijerathne, flyweight is a pattern from the canonical Design Patterns book, _Elements of Reusable Object-Oriented Software_. Saying that it is not a pattern and has nothing to do with OOP is complete nonsense; and the pattern is not specific to Java. The GoF OOP patterns are language agnostic. – jaco0646 Oct 03 '16 at 17:56
  • @jaco0646 not Java specific: yh, I would agree with that with some note. But It definitely has nothing to do with OO. Why do you say its a nonsense? Then what it has with OO? A design pattern is something which tells a solution for a problem, considering the structural aspects of the software. If flyweight is a pattern, I don't see wrong, if some one says 'merge-sort' is a pattern. would like to see your idea. :)) – Supun Wijerathne Oct 04 '16 at 01:13

2 Answers2

5

Sometimes, patterns are not obvious but it doesn't mean they are useless. And I'm afraid you understood the flyweight pattern incorrectly.

The main idea is to minimize memory use by sharing same objects which have already been used before. Usually, there is a data structure internally which is responsible for keeping values and returning them by some criteria. It looks up already existing element rather than creating a new one.

Actually, it is useful. For example, JDK uses this pattern to provide the Integer cache (keeps a small range of values to give them back effectively) and the String pool (look at the intern() method).

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

Simply what Flyweight tells is 'The way how the immutable objects should be handled'.

  • If some type of objects are immutable (I generally prefer all objects being immutable as far as possible),
  • And you have that kind of an object created inside the application, with some set of attributes, Ex: An immutable Person object is created with name = "John", age = 20
  • And you have another object exactly like that after some time in application excecution (name = "John", age = 20),
  • Simply use the previously created one, no need to create another (No need to worry since it is immutable, attributes won't change there after)

So it is basically a work around to save memory. It has nothing to do with object orientation.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87