Reputation: 3851
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?
Upvotes: 3
Views: 282
Reputation: 4258
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.
Upvotes: 3