Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:26:29+00:00 2026-05-17T15:26:29+00:00

Recently I came up with the idea of simple pattern for dynamic creation of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:26:30+00:00Added an answer on May 17, 2026 at 3:26 pm

    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); } });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just recently I came over an idea called the Application Strangler Pattern . As
I recently came upon the scary idea that Integer.count loops in Ruby start from
I need your help on an idea that came to my mind recently. Speaking
I recently came across a Windows library called AHK that gives me great control
I recently came to know that in C++ pure virtual functions can optionally have
I recently came upon a Microsoft article that touted new defensive enhancements of Windows
I recently came across a html email that contained _label attributes on the <
I came across some code recently that used a custom error handler to turn
I recently came across a CSS pseudo class that can do an effect for
I recently came across a server that's sending a Location header (for a redirect)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.