views:

463

answers:

5

I've created a class with properties that have default values. At some point in the object's lifetime, I'd like to "reset" the object's properties back to what they were when the object was instantiated. For example, let's say this was the class:

public class Truck {
   public string Name = "Super Truck";
   public int Tires = 4;

   public Truck() { }

   public void ResetTruck() {
      // Do something here to "reset" the object
   }
}

Then at some point, after the Name and Tires properties have been changed, the ResetTruck() method could be called and the properties would be reset back to "Super Truck" and 4, respectively.

What's the best way to reset the properties back to their initial hard-coded defaults?

A: 

You'd probably need to save the values off in private fields, so that they can be restored later. Maybe something like this:

public class Truck
{
    private static const string defaultName = "Super Truck";
    private static const int defaultTires = 4;

    // Use properties for public members (not public fields)
    public string Name { get; set; }
    public int Tires { get; set; }

    public Truck()
    {
        Name = defaultName;
        Tires = defaultTires;
    }

    public void ResetTruck()
    {
        Name = defaultName;
        Tires = defaultTires;
    }

}
Andy White
If you took this approach, then I'd declare the default values as static const. After all you are using them like constants.
AaronLS
True, I'll change that
Andy White
+5  A: 

You can have the initialization in a method instead of inlining with the declaration. Then have the constructor and reset method call the initialization method:

public class Truck {
   public string Name;
   public int Tires;

   public Truck() {
      Init();
   }

   public void ResetTruck() {
      Init();
   }

   private void Init() {
      Name = "Super Truck";
      Tires = 4;
   }
}

Another way is not to have a reset method at all. Just create a new instance.

maxyfc
To some degree, this is actually what I was doing, but I was wondering if there was a better way.
Dylan Bennett
+2  A: 

If you did your initialization in a Reset method you can be good to go:

public class Truck {
   public string Name;
   public int Tires;

   public Truck() {
    ResetTruck();
  }

   public void ResetTruck() {
      Name = "Super Truck";
      Tires = 4;
   }
}
Rashack
+1  A: 

Unless creating the object is really expensive (and Reset isn't for some reason). I see no reason to implement a special reset method. Why don't you just create a new instance with a usable default state.

What is the purpose of reusing the instance?

Brian Rasmussen
Because the object itself is what will be doing the resetting. It's not up to the code that instantiates the object to initiate the reset.
Dylan Bennett
@Dylan: It sounds to me like the class may be doing too much then. Take a look at the Single Responsibility Principle for more info.
Brian Rasmussen
A: 

You're essentially looking for the State Design Pattern

Abhijeet Patel