1 Replies - 3549 Views - Last Post: 19 October 2012 - 05:19 AM

#1 Lavace  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-January 11

MVC Pattern - Where to run logic?

Posted 18 October 2012 - 04:07 PM

I've searched long and far, but no joy. I've come to you, at last.

I'm creating a simulation for displaying an automated Maze Solver, in which there is little user input.

The MVC architecture is used and is implemented in Java. The View is to receive and draw data from the Model, through the Controller.

As it stands, I have two options available to update the Model logic and in turn update the View:
- Controller implements Runnable
- Controller creates a thread:
Thread runControl = new Thread(this);

- Thread can then execute run() in Controller, which updates the Model logic and pass through the new data to the View

This isn't my preferred method, however, as I'd sooner have the Model running it's own logic. I would prefer:
- Model implements Runnable
- Model contains all logic in run()
- Controller creates a Thread for the Model and executes the model

I'm lost at this point. The Model can then update it's own logic accordingly, but then whilst the Model Thread is running, how can the Controller be notified of changes in the Model data and in turn retrieve that data (that is still being manipulated/updated) to then notify the View?

Many thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: MVC Pattern - Where to run logic?

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: MVC Pattern - Where to run logic?

Posted 19 October 2012 - 05:19 AM

I can see your concern. Option one gives you an anaemic model -- a common problem with MVC implementations that leaves your model exposing most of its internals with get/set methods. Option two seems to make the control a little redundant.

Let's think about what's good about MVC. It lets you swap different implementations of your model, your control and your view. A good place to start thinking is what you would expect to change if you start swapping these implementations.

Here are some examples to get thinking about:

- Text view of the solution
- Animated view of the solution
- Animated view of the entire exploration

- Depth first search
- Breadth first search
- Random walk
- Interactive game where the human player tries to solve the maze

- 2D boolean array, true for a wall, false for not wall
- Linked network of Cell objects
- Edge set graph implementation
- Adjacency matrix graph implementation

If your design can represent all these choices through the same set of MVC interfaces then you are going in the right direction.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1