Q:

Defining "types" of objects

I'm trying to implement a class that may have only a defined set of objects. These Objects obviously have all the same properties, but only differ in the values of these properties.As an example think of class car. It has the properties name and ps. It should be (only) possible to create the following Objects:

car with name:'Porsche', PS:350;
car with name:'BMW', PS:250;

It should not be possible to initialize i.e. a car with name:'Porsche', PS:500;
This list might change or expand while coding is continued.
Is it a good Idea to create the types as subclasses of car, setting it's values in the constructor? Or would it be smart to set up some kind of factory / creator pattern?
Thanks a lot for your ideas
Simon

A:

Basically, you want to hide the details of the construction process. Since you don't have subclasses, the GoF solutions with Factory, Prototype and Builder are likely over-design. 
I solved this problem in a Java game before with static initializers. The following example shows configurations of Weapons that are standard in the game.
public class Weapon {
    ...
    public static final Weapon 
        DAGGER = new Weapon("Dagger", 0, 1, -1, true, false), 
        RAPIER = new Weapon("Rapier", 9, 1, 0, false, false),
        CLUB = new Weapon("Club", 9, 1, 0, true, false),
    ...
}

You access the objects as Weapon.DAGGER, etc. I didn't do it, but could/should have made the constructor private. Thus, others can't instantiate invalid variants of the weapon class. 
If you needed to have multiple instances of the same configuration of object, an alternative constructor could take an enumeration value, e.g., PORSCHE350, and it would construct the proper values for that object. 
The extreme case of this kind of control would be using something like the Object Constraint Language (OCL). 

