views:

116

answers:

5

Hi,

Recently I came up with the idea of simple pattern for dynamic creation of object. And I real love it.
I am sure that that "wheel" was invented and named. Could someone could point to some GOF pattern?

Problem: have huge number of objects that I don't want to have all initialized at the beginning. All are instances of one class.

I want to use an object and then free the memory (or allow garbage collection), each object is correlated to some string, so I have a map ({"str1", obj1}, {"str2",obj2},...) When the request comes with str1 I need to return obj1,....
I could have (pseudocode)

if(str == str1)
     return new Obj(obj1_init_params);  
else if(str == str2)  
    return new Obj(obj2_init_params);

...

However this is:

  1. Inefficient - to go over all if conditions.
  2. Ugly:)

To solve 1. you could use map:

map.put(str1, new Obj(obj1_init_params))
map.put(str2, new Obj(obj2_init_params))

then:

map.get(str1)

This is fast solution but ALL IS ONCE created not on demand. So....

Create a wrapper interface:

 IWrapper {
    Obj getObj();
}

Then I put it in map:

 map.put("str1", new IWrapper(){Obj getObj() {return new Object(object1_params)};

Now I am in home:

  1. Fast: map.get("str1").getObj();
  2. Dynamic - getObj() <- creation of object is postponed.

It is so simple, nice that someone had named it before. I am java programmer, so it work nice here. Can you came with similarly elegant solution?

A: 

This sounds a bit like an object factory.

JoshD
+1  A: 

This is a factory pattern: http://en.wikipedia.org/wiki/Factory_pattern

The factory pattern is a creational design pattern used in software development to encapsulate the processes involved in the creation of objects. The creation of an object often requires complex processes not appropriate to include within a composing object.

The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns.

Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object.

Andreas Paulsson
+4  A: 

EDIT I've mixed "what" with "how", originally.

What have you achieved? A lazy initialization. How have you achieved it? Through the Factory pattern (as many others have already written).

UPDATE You can use the standard Callable interface instead of IWrapper:

map.put(str, new Callable<Obj>() { Obj call() { return new Obj(str); } });
Bolo
A lazy Factory :)
volothamp
@Bolo - very nice explanation, generally I know factory, and lazy evaluation (which terrifies me as always my first thought is Singleton Pattern - which I started to hate). But I could not fit my concept into them, as it is composition of both. Simple "what" and "how" view makes all clear! Thanks a lot!
Gadolin
@Gadolin I've added a note about the `Callable` interface.
Bolo
A: 

I remember writing a CodeProject article a long time ago about factories. You could take a look at it: http://www.codeproject.com/KB/architecture/all_kinds_of_factories.aspx.

Also, DO NOT overdo it. There's an excellent article on factoryfactories on http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12 to give you an idea on how you could go too far in abstracting things.

Patrick
+1  A: 

As other have said before, this is called "Factory pattern". Well done on figuring that out for yourself.

If you want to take this one step further, create a map which just contains the class name (a String). That allows you to read the names from a file and you end up with "Inversion of Control" which leads to "Dependency Injection".

Aaron Digulla