Java Tic Tac Toe —Command vs Factory Method Pattern

Dan Pelensky
3 min readMay 18, 2017

--

One of my stories in this week’s iteration was to learn about the Factory method pattern and use it in place of the Command pattern on a branch.

The point of the exercise wasn’t necessarily to change how I’m creating objects, but to try something new and and make my own opinion on what I prefer.

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Wikipedia

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method — either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes — rather than by calling a constructor. Wikipedia

I have been using the Command Pattern to create my different board types, game types, players, and to choose to play again; but this isn’t entirely what the pattern is for.

Command pattern example

Board Interface

public interface Boards extends Menu {
Board execute();
String title();
}

Normal Board

public class NormalBoard implements Boards{
@Override
public Board execute() {
return new Board(3);
}

@Override
public String title() {
return "Normal (3x3)";
}
}

Big Board

public class BigBoard implements Boards {
@Override
public Board execute() {
return new Board(4);
}

@Override
public String title() {
return "Large (4x4)";
}
}

AppRunner

private List<Boards> boardTypes() {
return Arrays.asList(new NormalBoard(), new BigBoard());
}
private Board selectBoardSize(int choice) {
Boards board = boardTypes().get(choice - 1);
return board.execute();
}
private Board createBoard() {
print.boardSize();
print.options(boardTypes());
Board board = selectBoardSize(getSelection(boardTypes()));
}

Print

void boardSize(){
output.println("Select Board Size");
}

public void options(List<? extends Menu> options) {
StringBuilder instructions = new StringBuilder();
for (int i = 0; i < options.size(); i++) {
instructions
.append(i + 1)
.append(") ")
.append(options.get(i).title())
.append(System.lineSeparator());
}
output.println(instructions.toString().trim());
}

Factory method pattern example

BoardFactory

class BoardFactory {

Board board;

Board createBoard(int choice) {
if (choice == 1) {
board = new Board(3);
} else if (choice == 2) {
board = new Board(4);
} else {
return createBoard(size);
}
return board;
}

List<String> boardTypes() {
return Arrays.asList("Normal (3x3)", "Large (4x4)");
}
}

AppRunner

private Board createBoard() {
print.selectBoard();
print.options(boardFactory.boardTypes());
return boardFactory.createBoard(getSelection(boardFactory.boardTypes()));
}

Print

void selectBoard() {
output.println("Select Board Size");
}
void options(List<String> options) {
StringBuilder instructions = new StringBuilder();
for (int i = 0; i < options.size(); i++) {
instructions
.append(i + 1)
.append(") ")
.append(options.get(i))
.append(System.lineSeparator());
}
output.println(instructions.toString().trim());
}

There is no difference in the user experience with either of these methods — in both instances the following is printed to the command line:

Select Board Size

1) Normal (3x3)

2) Large (4x4)

Based on their selection, a board is created.

Command Pattern Pros

  • Elegant solution
  • Can easily add more board sizes, and no need to change existing code (except to add it to the boardTypes() List)
  • Do not hard code any numbers in — the numbers are based on the array position

Command Pattern Cons

  • Significantly more code
  • Potentially more complex than it needs to be
  • More classes/dependencies

Factory Method Pattern Pros

  • Less code
  • Easy to read
  • Obvious what it does

Factory Method Pattern Cons

  • Doesn’t feel as extendible
  • Choice numbers are hard coded
  • Would be extra work to change the order of the options

I think ultimately it comes down to the use case. In this situation, I think using the Factory method pattern is the right choice as it is simpler and less code. In a more complex case with more possibilities of different combinations being added, I think I would still prefer to use the Command pattern.

If there is a way of getting around using a switch statement and hard coding values in, I think I would be more inclined to use the Factory method pattern more often.

--

--