patternMinor
Factory Class for Game World
Viewed 0 times
factorygameworldforclass
Problem
The world in my strategy game is comprised of a number of towers. At the start of the game, only one tower is generated, and when the player discovers new towers they are created and added to the array of towers that the Game controls. Towers after the first tower start with different settings. For example, they do not start with workers and animals, and different floors are revealed and configured.
Previously, I had all of this code in the initialization of the Tower class. That class has too many responsibilities and has grown to over 1200 lines, so I have been looking for ways to refactor it to remove as much code as possible so that it is easier to understand. To achieve this goal, I have created a factory class to create a tower. The Game calls the class method of the factory class, and gets a fully configured tower in return. This has reduced the Tower class by almost 200 lines.
This is the first time that I have created a factory class, and I think that my implementation could definitely use some feedback. I decided to go with a class method so that I did not need an actual instance of this factory class to create the towers. The result of this is that I needed to use C functions in the factory class. Other options would have been to create a class like normal, and create an instance of the factory class at the start of the Game. The other option would be to create a singleton to get similar syntax to that of a class method while also having the ability to have instance methods.
I should note that the reason that worldSize is passed around everywhere is to make sure that the game is compatible with different screen resolutions.
I am open to criticism about all aspects of the code, but the most important thing is whether this is a good implementation of a factory class.
The header only contains the class method, so I will omit that.
DTTowerFactory.m
```
#import "DTAnimal.h"
#import "DTDwarf.h"
#import "DTTower.h"
#import "DTTowerFactory.h"
#i
Previously, I had all of this code in the initialization of the Tower class. That class has too many responsibilities and has grown to over 1200 lines, so I have been looking for ways to refactor it to remove as much code as possible so that it is easier to understand. To achieve this goal, I have created a factory class to create a tower. The Game calls the class method of the factory class, and gets a fully configured tower in return. This has reduced the Tower class by almost 200 lines.
This is the first time that I have created a factory class, and I think that my implementation could definitely use some feedback. I decided to go with a class method so that I did not need an actual instance of this factory class to create the towers. The result of this is that I needed to use C functions in the factory class. Other options would have been to create a class like normal, and create an instance of the factory class at the start of the Game. The other option would be to create a singleton to get similar syntax to that of a class method while also having the ability to have instance methods.
I should note that the reason that worldSize is passed around everywhere is to make sure that the game is compatible with different screen resolutions.
I am open to criticism about all aspects of the code, but the most important thing is whether this is a good implementation of a factory class.
The header only contains the class method, so I will omit that.
DTTowerFactory.m
```
#import "DTAnimal.h"
#import "DTDwarf.h"
#import "DTTower.h"
#import "DTTowerFactory.h"
#i
Solution
I think the factory class is completely unnecessary. I don't see how you're saving 200 lines out of a single file either.
The way I see it, the code in
The way I see it, the code in
DTTowerFactory.m comes from several different files (and probably needs to go back to those files).towerWithWorldSize:isFirstTower belongs in DTTower.mcreateTowerFloors() ought to return an NSDictionary object and belongs in DTTowerFloor.m. You should be doing:someTower.towerDict = createTowerFloor(worldSize);setBackgroundTypes() probably should just be part of the createTowerFloors() function. Is there any reason why you can't set a floor's background type as you create the floor?setupInitialTowerFloors() and setupTowerFloors() again could be part of DTTowerFloor.m, just like createTowerFloors().startingDwarf() and createTowerDwarves can be moved into DTDwarf.m.startingAnimal() and createTowerAnimals() can be moved into DTAnimal.m.Code Snippets
someTower.towerDict = createTowerFloor(worldSize);Context
StackExchange Code Review Q#62755, answer score: 2
Revisions (0)
No revisions yet.