views:

189

answers:

6

Hi guys, I would like to write code without a lot of switch, if/else, and other typical statements that would execute logic based on user input.

For example, lets say I have a Car class that I want to assemble and call Car.Run(). More importantly, lets say for the tires I have a chocie of 4 different Tire classes to choose from based on the user input.

For the, i dunno, body type, letS say i have 10 body type classes to choose from to construct my car object, and so on and so on.

What is the best pattern to use when this example is magnified by 1000, with the number of configurable parameters.

Is there even a pattern for this ? Ive looked at factory and abstract factory patterns, they dont quite fit the bill for this, although it would seem like it should.

+3  A: 

The problem you tell of can be solved using Dependency Injection.

There're many frameworks implementing this pattern (for example, for .NET - excellent Castle.Windsor container).

elder_george
Unity is pretty good, too, and I found the documentation very helpful to get started. http://www.codeplex.com/unity/
Mathias
A: 

You could use something like this:

  1. Define a class representing an option within a set of options, ie. a TireType class, BodyType class.

  2. Create an instance of the class for each option, get the data from a store. Fill a collection, ie TireTypeCollection.

  3. Use the collection to fill any control that you show the user for him to select the options, in this way the user selects actually the option class selected.

  4. Use the obejcts selected to build the class.

If any functionality requires chnges in behavior, you could use lamdas to represent that functionality and serialize the representation of the code to save it the store; or you could use delegates, creating a method for each functionality and selecting the correct method and saving it into a delegate on object creation.

What I would consider important in this approach is that any option presented to the user is fully functional, not only a list of names or ids.

Wilhelm
A: 

You can try the policy class technique in C++.

http://beta.boost.org/community/generic%5Fprogramming.html#policy

Hari
+4  A: 

I don't think the factory pattern would be remiss here. This is how I would set it up. I don't see how you can get away from switch/if based logic as fundamentally, your user is making a choice.

public class Car {
   public Engine { get; set; }
   //more properties here
}

public class EngineFactory {
  public Engine CreateEngine(EngineType type {
     switch (type) {
        case Big:
           return new BigEngine();
        case Small:
           return new SmallEngine();
     }
  }   
}

public class Engine {

}

public class BigEngine : Engine {

}

public class SmallEngine : Engine {

}

public class CarCreator {
   public _engineFactory = new EngineFactory();
   //more factories

   public Car Create() {
    Car car = new Car();

    car.Engine = _engineFactory.CreateEngine(ddlEngineType.SelectedValue);
    //more setup to follow

    return car;
   }
}
Richard Nienaber
+1  A: 

I think elder_george is correct: you should look into DI containers. However, you might want to check the builder pattern (here too), which deals with "constructing" complex objects by assembling multiple pieces. If anything, this might provide you with some inspiration, and it sounds closer to your problem than the Factory.

Mathias
A: 

Are you simply asking if you can create an instance of a class based on a string (or maybe even a Type object)? You can use Activator.CreateInstance for that.

Type wheelType = Type.GetType("Namespace.WheelType");
Wheel w = Activator.CreateInstance(wheelType) as Wheel;

You'd probably want to checking around the classes that you wind up creating, but that's another story.

ninj