0

I've got a class called ArtificialIntelligenceBase from which you can create your own artificial intelligence configuration sending some variables to the constructor or you can make a class that inherits from ArtificialIntelligenceBase and in the constructor of this new class just call the function super() with the parameters of the configurations.

I've also created some examples of artificial intelligences in classes, AIPassive, AIAgressive and AIDefensive. Obviously all of them inherits from ArtificialIntelligenceBase.

The point is that there're only few public functions in the base class. The variables in the base class are read only and the non public functions are protected in case you need to apply some modifications on them when created another pre-defined AI.

You can also create another AI just calling the base class sending some parameters in the constructor like this: new ArtificialIntelligenceBase(param1, param2, param3, param4);

I've tought about make the classes as a singleton because the classes can never change and once setted, their variables never change.

The question is: Is the singleton the best pattern to do this? Because I'm not sure.

PD: You don't need to explain any patter, just mention the name and I'll search for how it works

PPD: I'm developing in AS3. Just in case it helps

Thanks

1 Answers1

2

In general, singletons are evil. I don't see any reason in your case to use a singleton, either. It sounds like you're using your own version of a factory method pattern (using a constructor somehow?) or maybe a prototype (I don't know AS3 one bit), but if you're looking for other patterns a couple of other ones are abstract factory and builder.

You don't need to use the singleton pattern to limit yourself to using only one instance per type of class, though. It doesn't help avoid redundancy.

Community
  • 1
  • 1
arussell84
  • 2,443
  • 17
  • 18
  • I though about the singleton to avoid redundancy as all the variables are going to remain the same. For example: Once I've got an instance of new AIPassive(), to create somewhere else another instance of new AIPassive() will end having 2 instances of the exactly the same class and as all the variables are read only, I though it could make sense. – iamnewhere May 03 '11 at 23:39
  • @iamnewhere I think you should separate framework-level issues from application-level ones. Most of singleton's drawbacks are framework-level (like unit-testing troubles). In other hand spending extra resources on accidentally created instance is a problem for the concrete, well-designed and tested application. So my advice is to add necessary factory methods to the AI library and create singleton instances for your application. This would be a bit fragile, because you will have access to both factory methods and singleton instances, so make sure all decisions are clear for the entire team. – CheatEx May 04 '11 at 07:48