class instantiates itself

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?YmlsbCB0aWU=?=

    class instantiates itself

    class Program
    {
    ...
    ...

    static void Main()
    {
    Program program = new Program();
    }
    }

    - Is it a good practice to have a class instantiate itself?
    - What is the rationale for doing it?
    - Is Program a quasi singleton?

    Thank you.

  • Peter Duniho

    #2
    Re: class instantiates itself

    On Mon, 28 Jul 2008 13:28:08 -0700, bill tie
    <billtie@discus sions.microsoft .comwrote:
    class Program
    {
    ...
    ...
    >
    static void Main()
    {
    Program program = new Program();
    }
    }
    >
    - Is it a good practice to have a class instantiate itself?
    That depends on the rest of the design. The basic question of "a class
    instantiating itself" is neither good nor bad in and of itself.
    - What is the rationale for doing it?
    A common example would be if you have a "factory" method in the class
    itself, or as you seem to have guessed, have a "singleton" . Another
    example might be a data structure where instances create new instances of
    itself; cloning, recursive data structures, or other "self-reproducing"
    data structures.
    - Is Program a quasi singleton?
    Define "quasi singleton". It's not my opinion that simply creating an
    instance of itself makes a class "singleton-like", but since that is one
    part of implementing a singleton, and since you haven't defined the term
    "quasi singleton" it's hard to know whether that's enough to justify
    calling it that.

    Pete

    Comment

    • Andrew Faust

      #3
      Re: class instantiates itself



      "bill tie" <billtie@discus sions.microsoft .comwrote in message
      news:2DD4ADB4-A0D1-4CC9-B601-4B0062EBE930@mi crosoft.com...
      class Program
      {
      ...
      ...
      >
      static void Main()
      {
      Program program = new Program();
      }
      }
      >
      - Is it a good practice to have a class instantiate itself?
      It's not bad practice. There are plenty of reasons to do so. The .Net
      framework itself does this in cases. For example look at the IClonable
      interface.

      Andrew Faust

      Comment

      • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

        #4
        Re: class instantiates itself

        bill tie wrote:
        class Program
        {
        ...
        ...
        >
        static void Main()
        {
        Program program = new Program();
        }
        }
        >
        - Is it a good practice to have a class instantiate itself?
        Yes, if it makes sense for that specific class.
        - What is the rationale for doing it?
        It's done whenever a method needs to return a new instance of the class.

        For example, all the methods in the String class that returns a new
        string (Substring, Format, Concat, Replace, ToUpper, ToLower, Insert,
        Remove, Trim, et.c.) are creating new instances of the String class.
        - Is Program a quasi singleton?
        No, it's just a class that it doesn't make sense to create more than one
        instance of. If you would eventhough, they would be separate instances.

        However, as the program is written to have only one instance of the
        class, the person implementing the class might have used short cuts that
        depends on this fact, which could cause problems if you would create
        more than one instance of it.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: class instantiates itself

          On Jul 28, 4:28 pm, bill tie <bill...@discus sions.microsoft .com>
          wrote:
          class Program
          {
            ...
            ...
          >
            static void Main()
            {
              Program program = new Program();
            }
          >
          }
          >
          - Is it a good practice to have a class instantiate itself?
          Of course!!! , it's a VERY common used practice in different pattern,
          like Singleton.
          - What is the rationale for doing it?
          There can be a lot of reasons, a couple of reasons:
          1- You do not want the class to have a public constructor (take a look
          at the Guid class in the framework) so you expose a method that
          returns a created instance
          2- The class is non modificable (like string) so each method that
          implicate a change returns a new instance.
          - Is Program a quasi singleton?
          Not really, You can even have a singleton that do not follow this.
          but yes, they are related in a way as the singleton usually use this
          techinique

          Comment

          Working...