The Artima Developer Community
Sponsored Link

Java Answers Forum
Controlling number of Objects

2 replies on 1 page. Most recent reply: Jun 9, 2004 3:17 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Amit

Posts: 23
Nickname: amitactsh
Registered: Nov, 2002

Controlling number of Objects Posted: Jun 9, 2004 1:43 AM
Reply to this message Reply
Advertisement
Hi all,
I am searching solution for a problem and seek help from you... I want to have control on the maximum number of instances, that I could have ,on a particular class.
How could I implement it ? One possible solution that I was suggested was to use the Singleton class but It allows only on object of a class. I need a specific number of objects.

Please help me out of this.
Thanks and Regards
Amit


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Controlling number of Objects Posted: Jun 9, 2004 3:17 AM
Reply to this message Reply
By Singlton method, u can create multiple instances also.

Check this out

Class FixedInstanceExample
{
 private instanceAllowed= 5 ;
 //fix the number of instance to 5
 private Vector fixedInstance = new Vector();
 private FixedInstanceExample()
 {
 }
 public static FixedInstanceExample getInstace()
 {
   if (fixedInstance.size()<instanceAllowed)
    {
      FixedInstanceExample ex = new FixedInstanceExample();
      fixedInstance.add(ex);
      return (ex);
    }
    else
    {
       //return any of the instance
    }
      
 }
}
 
//NOTE THAT THE CLASS IS JUST AN IDEA
// U CAN IMPLEMENT UR FINALIZER METHOD TO REMOVE INSTANCES FROM VECTOR
 
[java]

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Controlling number of Objects Posted: Jun 9, 2004 3:17 AM
Reply to this message Reply
By Singlton method, u can create multiple instances also.

Check this out

Class FixedInstanceExample
{
 private instanceAllowed= 5 ;
 //fix the number of instance to 5
 private Vector fixedInstance = new Vector();
 private FixedInstanceExample()
 {
 }
 public static FixedInstanceExample getInstace()
 {
   if (fixedInstance.size()<instanceAllowed)
    {
      FixedInstanceExample ex = new FixedInstanceExample();
      fixedInstance.add(ex);
      return (ex);
    }
    else
    {
       //return any of the instance
    }
      
 }
}
 
//NOTE THAT THE CLASS IS JUST AN IDEA
// U CAN IMPLEMENT UR FINALIZER METHOD TO REMOVE INSTANCES FROM VECTOR
 

Flat View: This topic has 2 replies on 1 page
Topic: convert an int into a byte array Previous Topic   Next Topic Topic: help, need help for assignment

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use