Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern to use when you need to initialize your object?

I have a class, which has an Initialize method, which creates a bunch of tables in a database. This class looks like this:

public class MyClass
{
  private bool initialized = false;

  public void Initialize()
  {
    if(!initialized)
    {
        //Install Database tables
        initialized = true;
    }
  }

  public void DoSomething()
  {
    //Some code which depends on the database tables being created 
  }

  public void DoSomethingElse()
  {
    //Some other code which depends on the database tables being created 
  }
} 

The two methods DoSomething and DoSomethingElse need to make sure that the Initialize method has been called before proceeding because they depend on having the tables in the database. I have two choices:

  1. Call the Initialize method in the constructor of the class - this does not seem like a good idea because constructors should now call methods, which are non-trivial and could cause an exception.

  2. Call the Initialize method in each of the two methods - this does not seem like a great solution either especially if there are more than a handful of methods.

Is there a design pattern which could solve this in a more elegant way?

like image 586
Milen Kovachev Avatar asked Apr 25 '16 14:04

Milen Kovachev


People also ask

Which method is used to initialize the object?

A constructor is used to initialize an object when it is created.

What are the 3 ways of object initialization?

Assigning value to a variable is called initialization of state of an object. Initialization of variable means storing data into an object. 3. We can assign the value of variables in three ways: by using constructor, reference variable, and method.

Which design pattern will you use to create a complex object?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.

What is an object how do you initialize an object?

Object initializers enable you to declare and instantiate an instance of a class in a single statement. In addition, you can initialize one or more members of the instance at the same time, without invoking a parameterized constructor.


1 Answers

I would use a static factory method in which Initialize is invoked, and make the constructor private, to force use of the static factory method:

public class MyClass
{
  private MyClass() { ... }

  public static MyClass createInstance() {
    MyClass instance = new MyClass();
    instance.Initialize();
    return instance;
  }
}

Also, I would remove the initialized variable - in part because you don't need it any more - but also because it requires some means of guaranteeing visibility (e.g. synchronization, volatile or AtomicBoolean) for thread safety.

I think that Miško Hevery's blog post on (not) doing work in constructors is an interesting read.

like image 168
Andy Turner Avatar answered Sep 25 '22 21:09

Andy Turner