3

I'm posting this in Lua and Codea, since that's what I'm using, but it's a pretty general question I think.

I'm considering an overall design pattern for displaying graphics, and I'd like to know if there are problems with it.

Here's the design pattern I'm considering:

A setup() method in a Main class tells a Graphics class to create some graphic elements: for instance, two squares and an ellipse.

The Graphics class generates the parameters needed for each element, stores them as a table, and sends the table to a Data class.

When the app starts drawing, the draw() function in Main tells the Graphics class to draw the objects that were created.

Then the Graphics class asks the Data class to hand back all the tables that it sent over during setup(), and it uses them to draw the elements.

Main commands Graphics which commands and queries Data. I'm sure this is a known pattern: are there problems generally associated with it?

prapin
  • 6,395
  • 5
  • 26
  • 44
Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
  • 2
    You probably want to look into the [Model-View-Controller](http://en.wikipedia.org/wiki/Model-View-Controller) software design pattern. *Main* is your *Controller*, *Graphics* is *View* and *Data* is *Model* – dualed Dec 27 '12 at 08:46
  • If it's for a game, it could be a little too much general. Looks similar to MVC. – Bartek Banachewicz Dec 27 '12 at 08:51
  • @dualed: thank you for taking the time to respond. Are you aware of any known problems associated with this specific implementation of the pattern, i.e. for displaying graphics? – Le Mot Juiced Dec 29 '12 at 15:44
  • 1
    It is very hard to tell from this description alone, but it seems you put too much controller-logic into graphics. Also I would actually strip `main()` of anything but setup code and put the controller logic into a separate controller. There is nothing wrong with going a different route though, just keep in mind that there is a reason MVC was so successful (apart from people talking about MVC and even forcing it on projects unfit for it and not even knowing what it is) – dualed Dec 29 '12 at 16:46

1 Answers1

3

What you're doing - which is essentially model-view-controller, is commonly used in industry and application development. It works relatively well, although no programming paradigm is without its flaws. That being said, MVC is designed for large teams working on large projects. It is logistically impossible for multiple people to work together on a single Codea project, so given that you're working on your own I'm guessing the project is going to be small to medium scale. When working solo on projects like this, a pragmatic, intuitive approach is by far the best option.

Using MVC on something like this is a little bit like building an entire democracy, complete with a congress/parliament, head of state, and court system just to manage the goings-on of a single household. Democracy is good, and a robust system of elections and checks-and-balances is the only way to keep the system running smoothly. In a household, however, even though order and happiness still needs to be maintained, the approach is completely different.

For you, the best you can do is think about how the thoughts are structured in your head. Do you think of a hoard of enemies as a single entity, or as a collection of autonomous objects? Do you consider a spaceship to be a perfect mathematical collection of properties, or an image the user interacts with on the screen? When the pause screen appears, is the game screen still there, just hidden, or has it ceased to exist and been replaced?

Moreover, how do you structure ideas? Do you start with broad categories, working down into ever-finer detail, or do you have a vivid mental image in your head that you strive to build a world for? Does your concept of a program consist of a vast flowchart that dips into reality at certain points, or a collection of nodes sending messages to one another? All of these are questions only you can answer. If you naturally gravitated toward MVC, you may want to stick with it. If you read about it in a book, and decided that, even though you didn't really see why it was useful, it must be some sort of magic pixie dust you can sprinkle on any project to make it easy, I'd urge you to reconsider.

Happy coding!

By the way, I think this question is a little better suited for Stack-Exchange programmers than stack overflow. It's a subtle distinction, but Stack Overflow is for facts, like bug-fixes and algorithms, and Programmers is for opinions on how to write software.

Community
  • 1
  • 1
exists-forall
  • 4,148
  • 4
  • 22
  • 29
  • This is a fantastic answer. I'm a start-with-a-vivid-mental image guy, but my mind is always looking for reuseability. I hate writing anything twice. I'm not sure I'm going to take your advice entirely, but this is a fantastic answer. – Le Mot Juiced Jan 06 '13 at 22:57