OOPS (Design Patterns) [closed]
Asked Answered
C

6

4

hey, hi i want put limit on object creation means a class can have at most suppose 4 objects not more than that how to achieve this?

Copt answered 24/2, 2010 at 5:8 Comment(4)
It is language dependent. Which language are you interested in?Bagel
It's worth reading Steve Yegge's condemnation of the Singleton pattern, and see if any of those warnings apply to you: steve.yegge.googlepages.com/singleton-considered-stupid Clients of your class shouldn't know that there are limited instances of the class. For one thing, that makes it harder to unit test.Pruett
What in the world are you trying to accomplish?Eyelash
What should the behaviour be on the request for the 5th object instance?Foundry
C
10

One approach is using an object factory that creates at most 4 instances. It's an interesting need... Would an object pool serve the same need?

Cantus answered 24/2, 2010 at 5:12 Comment(2)
+1 for both limited Factory and Object Pool.Salop
A Factory class with an object pool or cache would be the way to go hereShirlshirlee
Q
3

You can count the numbers of instances created by using a static class property to store the count. This can either be done in the class constructor or you can make use of a factory pattern. It is a bit difficult to answer this more precisely without knowing the target language.

Quoits answered 24/2, 2010 at 5:13 Comment(0)
S
3

Try 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.

Silsbye answered 24/2, 2010 at 5:15 Comment(1)
That variant is called the MultiTon :)Felly
R
1

One way to achieve is the Singleton Design pattern, Whenever we make a call to create an instance, check the count of the instance which are already created, if the instance count is already reached 4, then use the same instance for your application. TO have a count, Creat Static Int Counter = 0; and keep incrementing it to get the results.

Ronnie answered 11/3, 2010 at 8:47 Comment(0)
M
0

The simplest way to do this would be to have a class level attribute called "count", and in your constructor, just make sure that "count" isn't above a certain number.

//pseudocode
class foo
  static count = 0

  def constructor()
    if count < 4
      //create object
    else
      //there are too many!
Methuselah answered 24/2, 2010 at 5:15 Comment(3)
In this case, each object will have its own copy of count, which'll always be 0. The count variable needs to be static.Silsbye
It's pseudo code, it doesn't need to be perfect.Methuselah
but at leat the essential should be right (as they are now)Chyou
C
0

This is short code snippest that will give the above result in c#

sealed class clsInstance
    {
        public static int count = 0;
        private static readonly clsInstance inst = new clsInstance();
        clsInstance()
        {

        }

        public static clsInstance Inst
        {
            get
            {
                if (count < 4)
                {

                    Console.WriteLine("object : " + count);
                    count++;
                    return inst;
                }
                return null;
            }
        }


    }

   class MainClass
   {
       public static void Main(String[] args)
       {
           clsInstance c1 = clsInstance.Inst;
           clsInstance c2 = clsInstance.Inst;
           clsInstance c3 = clsInstance.Inst;
           clsInstance c4 = clsInstance.Inst;
           Console.ReadLine();
           clsInstance c5 = clsInstance.Inst;
           Console.ReadLine();
       }
   }
Copt answered 19/4, 2010 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.