1

I have an app, where I have some heavy objects, I want to limit the object creation to some x number (it won't affect my app). how to do it?

2
  • 3
    Use a Pool Pattern. Commented May 25, 2015 at 8:39
  • are the pooled objects have any data associated state with them? Is it fine to return any one of the objects from the pool for a request? My query : if you have different state for these pooled objects, Singleton pool getInstance() method should be modified accordingly and get method should be aware of the state of pooled object Commented Jul 22, 2015 at 15:43

2 Answers 2

3

Modifying the Singleton pattern. You can use a count variable. You'll need to keep the Constructor private to have control over the no. of instances.

Sign up to request clarification or add additional context in comments.

2 Comments

This is called a "pool".
Why constructor must be private?
0

You could use Active Object Pattern, as an alternative to standard pool.

Since you need a couple of such objects, the objects probably maintain state.

Therefore you could have List of Active objects to which you dispatch requests via some proxy.

Idea is the following:

1) There is "scheduler", which in simplest form is a thread waiting on Blocking Queue and handle requests one by one. Notice how method run is executed, not thread.start().

2) You have blocking queue of services to which you dispatch requests which are picked up by the "scheduler" thread.

3) Your Active Object exposes services which are processed by Scheduler.

Next, you can wrap static list of these Active Objects behind some Proxy which would be responsible for delegating requests to your active objects, via round robin or whichever other balancing strategy.

Here is an example from wikipedia:

class BecomeActiveObject
{
    private double val = 0.0;
    private BlockingQueue<Runnable> dispatchQueue
            = new LinkedBlockingQueue<Runnable>();

    //
    public BecomeActiveObject()
    {
        new Thread(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        while (true)
                        {
                            try
                            {
                                dispatchQueue.take().run();
                            } catch (InterruptedException e)
                            {   // okay, just terminate the dispatcher
                            }
                        }
                    }
                }
        ).start();
    }

    //
    void doSomething() throws InterruptedException
    {
        dispatchQueue.put(
                new Runnable()
                {
                    public void run() { val = 1.0; }
                }
        );
    }

    //
    void doSomethingElse() throws InterruptedException
    {
        dispatchQueue.put(
                new Runnable()
                {
                    public void run() { val = 2.0; }
                }
        );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.