Dynamic object creation pattern - I love [closed]
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this questionRecently 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:
- Inefficient - to go over all if conditions.
- 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:
- Fast: map.get("str1").getObj();
- 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?
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); } });
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.
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".
This sounds a bit like an object factory.
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.
精彩评论