Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tic Tac Toe Design Pattern

I was wondering if I could get your thoughts and advice as to what would be the best/most advantageous design pattern for a networked Tic Tac Toe game?

I have been looking at the following design patterns: Factory, Abstract Factory, Singleton, Prototype, and Builder.

In your experience, which would be the best to use, and why?

Right now, my Tic Tac Toe game is a threaded client/server game that can be played over the internet via sockets. However, I am going to refactor the game in some way to make use of a design pattern.

I was thinking about setting up a client/server architecture that can be used for playing many different types of games, such as tic tac toe, connect 5, etc...

What direction should I go? I am looking to go into a direction that will really give me some experience with design patterns...

Thanks!

like image 421
littleK Avatar asked Nov 04 '09 17:11

littleK


3 Answers

Caveat: I hate when people use "design patterns" as a GOAL. Design good code and if what you are doing looks like a DP, use the DP to help you improve the code. But don't start out saying "I want to write my program using the Striking Crane Pattern." That's putting the cart before the horse. DPs should emerge from your design. They shouldn't drive the design.

EndCaveat

You are already using design patterns. DPs are just jargon for commonly found "patterns" used in programming. What you should first be asking yourself is "How do I recognize the design patterns in the network game I've written?"

After that you can worry about going forward. To have multiple games you will probably want to use an abstract factory pattern to handle your games. The server calls the factory to get a game object that knows how to play the chosen game.

like image 159
jmucchiello Avatar answered Sep 29 '22 17:09

jmucchiello


A design pattern is just a name for a way to design something. A tic-tac-toe game could use all of the design patters, or it could do without them. They're just a pattern on how to do a particular thing.

You want to refactor your code, fair enough, but don't do it because you want more design patterns in it. Do it because you want to clean up your code, make it more general and maybe extend it, to other games.

To get a good answer on which design pattern to use, give a specific example:

Q. How should I limit the network_handler to one instance? - A. Use Singleton

Otherwise you could find a reason to include practically every design pattern.

like image 44
Jonas Avatar answered Sep 29 '22 15:09

Jonas


Different patterns are used for different reasons. More often than not you'll see more than one design pattern used in a single app, it's a case of using the right pattern for the right job.

For instance in one web app I might use the singleton pattern for a settings class and a factory class for database connections. The singleton ensures I only have on settings object and the factory class takes care of handing me a database adapter appropriate to my environment.

like image 20
RMcLeod Avatar answered Sep 29 '22 15:09

RMcLeod