简体   繁体   中英

What is the best approach for initialize object

I have a basic question about initalization of object. UPDATE SomeClass is just other class. Class Foo just using methods of someClass. For example: Controller (Foo) that uses methods of DAO object(SomeClass).

What is the best approach?

1 approach - using constructor

 public class Foo{

      private SomeClass someClass;

      public Foo()
      {
         someClass=new SomeClass();
      }
      public void method1(){//uses someClass}
      public void method2(){//uses someClass}
  }

2 approach - initalization in every method

      public class Foo{

       public void method1(){SomeClass someClass = new SomeClass();}
       public void method2(){SomeClass someClass = new SomeClass();}
  }

3 approach - initalization with no constructor

       public class Foo{

        private SomeClass someClass=new SomeClass();

        public void method1(){//uses someClass}
        public void method2(){//uses someClass}
 }

The answer to your question really depends on how SomeClass works, and what you want it to do.

Approach 1: Creating an object instance in the constructor.

  • Advantages: You can create different instances in different constructors. You can use the same instance across all methods of Foo.
  • Disadvantages: You may not want to use the same instance across all methods of Foo.

Approach 2: Creating an object instance in a method.

  • Advantages: You can use many different SomeClass instances in one method.
  • Disadvantages: You have to instantiate SomeClass multiple times. This could be expensive depending on what SomeClass does.

Approach 3: Creating an object instance in the attribute declaration.

  • Advantages: You get the same instance across all methods of Foo despite which constructor is called (unless you overwrite the value).

  • Disadvantages: If methods change the state of SomeClass, it could cause issues if you don't think of this in your design.

Approach 4: Dependency Injection. Inject the instance of SomeClass into the constructor of Foo.

  • Advantages: You can specify what instance of SomeClass to use at runtime.
  • Disadvantage: Calling code needs to supply an instance, which could be un-necessary depending on what you are trying to accomplish.

     private mySomeClass; public Foo(SomeClass mySomeClass) { this.mySomeClass = mySomeClass; } public void method1(){ if(mySomeClass != null) mySomeClass.runSomething(); } 

This would depend on what you want to do with the initialized object:

  • For a prototype, initialize in method
  • For one-to-one relation, do a composition
  • For a many-to-one create a singleton object (hide the constructor of SomeClass - make it private) and get hold of the object it all the related classes using a method like SomeClass.getSingleton()

Beware that each of the above cases have their own consequences during Multithreading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM