- Joined
- Sep 25, 2003
- Location
- Mesa, AZ
- Thread Starter
- #21
So to update... since it's been a while. I finished my dice rolling project after this most recent post. I showed my buddy (the senior software engineer). He suggested that I refactor it based on what I've learned. (I had just finished reading Object Primer and started in on a book on design patterns.)
The first iteration of my project was more of a mish-mash of tossing form controls on a blank form in Visual C# Express and hard coding all the functionality into the event handlers. I did have one (only one) class that controlled my dice rolls. So my refactor was done with the intent to create a class hierarchy for my dice.
- I created an individual class for each die type that would inherit from an abstract dice class.
- I created a framework for my dice rolls in an interface, then implemented that interface into a single concrete class. To generate a dice roll, I would pass one of my dice objects into my roll framework. The roll method would get the number of sides on the dice, then pass that as an int into a random object to return the appropriate value.
- I also implemented a framework to dynamically assign the appropriate type of dice object based on user input.
So in this refactor, I'm more or less using 2 patterns. The Strategy Pattern, to decouple my roll functionality from my dice objects. I also employ the Factory Method Pattern to dynamically create an object, then pass it into my roll class to retrieve the appropriate random number.
So my buddy proofed what I'd done and thought it was a huge improvement in terms of OO Design. I tend to agree. He mentioned a way of streamlining it that I still can't quite get my head around...
He suggested looking at the Builder Pattern and implementing that rather than Factory Method to dynamically create my dice objects. He also suggested storing my dice objects in an enum. At first, I saw some benefit... but after reading a bit more about Builder and Factory Method, I'm a little confused as to the advantage of one over the other in this scenario.
I understand that Builder really excells at creating complex objects. But for this project, my objects that I'm instantiating are fairly simplistic (all my dice objects really "know" is how many sides they have). Is there something I'm missing. Is Builder generally better practice? Is Builder generally considered to be easier to implement, or more elegant than Factory Method?
The first iteration of my project was more of a mish-mash of tossing form controls on a blank form in Visual C# Express and hard coding all the functionality into the event handlers. I did have one (only one) class that controlled my dice rolls. So my refactor was done with the intent to create a class hierarchy for my dice.
- I created an individual class for each die type that would inherit from an abstract dice class.
- I created a framework for my dice rolls in an interface, then implemented that interface into a single concrete class. To generate a dice roll, I would pass one of my dice objects into my roll framework. The roll method would get the number of sides on the dice, then pass that as an int into a random object to return the appropriate value.
- I also implemented a framework to dynamically assign the appropriate type of dice object based on user input.
So in this refactor, I'm more or less using 2 patterns. The Strategy Pattern, to decouple my roll functionality from my dice objects. I also employ the Factory Method Pattern to dynamically create an object, then pass it into my roll class to retrieve the appropriate random number.
So my buddy proofed what I'd done and thought it was a huge improvement in terms of OO Design. I tend to agree. He mentioned a way of streamlining it that I still can't quite get my head around...
He suggested looking at the Builder Pattern and implementing that rather than Factory Method to dynamically create my dice objects. He also suggested storing my dice objects in an enum. At first, I saw some benefit... but after reading a bit more about Builder and Factory Method, I'm a little confused as to the advantage of one over the other in this scenario.
I understand that Builder really excells at creating complex objects. But for this project, my objects that I'm instantiating are fairly simplistic (all my dice objects really "know" is how many sides they have). Is there something I'm missing. Is Builder generally better practice? Is Builder generally considered to be easier to implement, or more elegant than Factory Method?