0

I'm working on a simple board game that will run in the browser. I'm thinking how to structure the program and I'd like to hear your opinions.

The program will consist of the user interface part (the logic that handles key presses, manipulates Html, etc.), and of the data-and-logic part (makes decisions on how to update the game and stores the state of the game).


I can think of two options regarding how to structure this:

A- The business logic module references the display module.

Advantages:

  • The display knows nothing about the business logic.

  • Easy to inject the display to the business logic.

Disadvantage: how can the business logic be notified when the user performs an action?

B- The display module references the logic module.

Advantage: the display can easily notify the business logic when the user performed an action.

Disadvantage: the display knows about the existence of the business logic.


Which design is better, and why?

0

1 Answer 1

1

Advantages: The display knows nothing about the business logic.

You can't display something you know nothing about. Trying to invert control so the business logic calls some sort of display API just turns part of the business logic into display logic and intertwines the two. Then your business logic will break if the display API or display logic ever changes.

Approach B is the better approach and is the basis for the Model-View-Controller architecture.

4
  • Doesn't it make sense for the business logic to call methods such as MovePiece (in the case of a checkers game) on an IDisplayer it owns? The business logic tells the displayer what to display. Then I can inject any IDisplayer I want. Isn't that the better approach?
    – Aviv Cohn
    Jan 9, 2015 at 2:01
  • @AvivCohn Like I said, once you do that, you've put display logic inside the business logic. Now you can't do anything business-logic-related without displaying things too.
    – Doval
    Jan 9, 2015 at 2:13
  • Since you refer MVC: do you consider the problem to be independent from the language used?
    – Wolf
    Jan 9, 2015 at 8:48
  • @Wolf Yes. I can't think of any languages used in practice where it's not feasible to separate the system's logic from the I/O.
    – Doval
    Jan 9, 2015 at 12:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.