How to maintain software engineering skill when you no longer work as a programmer. Nasty is just a reference to "The Rutles" a parody of the Beatles circa late 70s. And Old Dog refers to the new tricks I've taught myself now that I am a Doctor and no longer a full time software engineer. Refactoring is a fancy name used in objected oriented programming for restructuring and redesigning code to make it more readable or more elegant or just easier to understand. In the case of the Blackjack Program example I purposely designed it to use very little object orientedness if that's even a word. Now it's time to refactor the Blackjack code to take advantage of object oriented programming. I find the new code much more readable hopefully you will as well. More importantly the new code structure will allow us to go beyond AP Computer Science constraints and turn this into a graphical card game. That will be the subject of another article. The BlackJack object will be the place where the main routine resides. I used the style of object oriented programming I proposed in the previous "hello world". The main routine will instantiate a BlackJack object and call the 'run' method. All the program logic will be in the 'run' method. This may seem like a bit of overkill but what if you wanted to run a BlackJack server? Each player that logs in needs their own BlackJack game. Each game needs to run in a separate thread of execution threading is a way of portioning off processor access so that objects do not have to wait for other objects to execute. While that may still seem like overkill if you pursue Computer Science as a major you will ultimately come across this 'pattern' of program structure. From my point of view this structure is easy to implement has no effect on performance is pure object oriented programming so why not. When you grab a real deck of cards you will notice there are 52 objects with a back cover design and a face design. This is a natural object that we can model as an object in our program. The CardDeck object is the object model for a deck of cards. In computer terms the deck of cards is a 'container' for 'PlayingCard's. The Hand object is the object model of a set of cards. In the original program these were arrays with code for tracking the dealer hand and code for tracking the player hand. The Hand object greatly simplifies the code. Separate tracking is done by instantiating 2 'Hand' objects. A player 'Hand' and a dealer 'Hand'. Many Computer Scientists are very poor artists. But sometimes a picture really is worth 1000 words. To standardize the drawing of object relationships and provide computer tools to assist the artistically challenge Unified Modeling Language was developed. The Object diagram is a box with the name of the Object ie. Class in the title of the box located at the top of the box. Following the title are fields and methods listed below the title. Arrows can be drawn between the class objects to signify relationships and usage. You have probably seen some of these diagrams already while studying this topic. The following represents the class objects created for the Blackjack program: The above diagram represents the all the classes created to make the new BlackJack program. In the code that follows I have commented out the old code I used and tried to highlight the new code. It makes the code a little longer but it will give you an idea of the refactoring process. The only thing that didn't fit well into the model is the scoring of the hands. This needs a little work. Even though this has been about BlackJack you would like to reuse the classes for other card games. Hands would be scored differently for different games. There is an object oriented way to handle this but it is a little beyond the scope of this article and level of programming. For now it's hard coded into the PlayingCard object so it's only good for BlackJack and you would have to adapt that class for a different type of game. The full code is reproduced below sans the comments from the old code. This should give you a better feel for just what the Object Oriented style is doing for us.

Sitemap

1 2 3 4 5