Jump to content

Program Design

namarino

Hey guys, I'm currently writing a program, Conway's game of Life with a GUI, in java. I have relatively little experience with programming as I have only take two classes, but I really understand all that I have learned so far. I am really wondering right now, though, what the best way of writing a program is. Is it best to create many classes each embodying a different object, or is it best to have as little code and classes as possible? Or is it really just personal preference? Right now, I have about 5 classes for this program. I have an Application class (main), a window class (JFrame), a panel class (JPanel), a Grid class, and a Cells class. I fell like it is better organized if I do it this way, but I don't really know. Anyway, thanks for your help, I appreciate it!

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

Having a few more classes/functions is fine if you document them well.

Have a small section explaining what it does like this:

//Interacts with this other function.//Does this and that//returns some value to some other function or main

So you know what everything does.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys, I'm currently writing a program, Conway's game of Life with a GUI, in java. I have relatively little experience with programming as I have only take two classes, but I really understand all that I have learned so far. I am really wondering right now, though, what the best way of writing a program is. Is it best to create many classes each embodying a different object, or is it best to have as little code and classes as possible? Or is it really just personal preference? Right now, I have about 5 classes for this program. I have an Application class (main), a window class (JFrame), a panel class (JPanel), a Grid class, and a Cells class. I fell like it is better organized if I do it this way, but I don't really know. Anyway, thanks for your help, I appreciate it!

If it's hard to understand for u, treat classes like a different real-life objects: each of them has it's special options, abilities and etc.

So, basically, u're on the right way.

Link to comment
Share on other sites

Link to post
Share on other sites

They're making you do Game of Life with only two programming classes? That's bold. I like it. Just be glad you only have to do Conway cells and not Fredken cells as well.

 

I did mine in C++ but the design should be the same for the most part. I would definitely recommend using a good variety of classes and objects.

 

Quick note on object oriented designs:

 

Encapsulating the behavior of an object, say a Conway cell, in a class by itself makes it easier to make changes later on. So long as you keep the arguments and return type the same, you can make any changes you want inside the object and the rest of the program should not be affected. From what you wrote it seems you are really on the right track and thinking about the problem in the correct way.

Turnip OC'd to 3Hz on air

Link to comment
Share on other sites

Link to post
Share on other sites

A fairly common piece of advice given regarding using classes and functions is "One class should do one thing". Examples of these "things" would be:

  • GUI
  • Number-crunching
  • Accessing files
  • Program-specific objects (such as rooms in an RPG)
  • Sub-programs

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

One thing that is very important while you are still learning (which you could argue is forever), is to experiment. It is very important to try out different things to see what works, and what doesn't. When you get more experienced you can learn about programming patterns and anti-patterns, but just playing around with different set-ups and styles will help you tons.

After a while you'll start to notice that you develop your own programming style.

Link to comment
Share on other sites

Link to post
Share on other sites

Questions to ask yourself:

  • Can I test each component of the application independently?
  • If I change the internal implementation of a component (but not the interface) does it necessitate changing tests or other parts of the application?
  • Does my code contain large branched/nested if statements or switches?

These are indications that you should take another look at your design. The last point in particular suggests that you are not taking proper advantage of OOP principles.

Link to comment
Share on other sites

Link to post
Share on other sites

 

A fairly common piece of advice given regarding using classes and functions is "One class should do one thing". Examples of these "things" would be:

  • GUI
  • Number-crunching
  • Accessing files
  • Program-specific objects (such as rooms in an RPG)
  • Sub-programs

 

I think GUI would be better as a namespace containing the different classes like Button, Label, Progressbar....

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys, I'm currently writing a program, Conway's game of Life with a GUI, in java. I have relatively little experience with programming as I have only take two classes, but I really understand all that I have learned so far. I am really wondering right now, though, what the best way of writing a program is. Is it best to create many classes each embodying a different object, or is it best to have as little code and classes as possible? Or is it really just personal preference? Right now, I have about 5 classes for this program. I have an Application class (main), a window class (JFrame), a panel class (JPanel), a Grid class, and a Cells class. I fell like it is better organized if I do it this way, but I don't really know. Anyway, thanks for your help, I appreciate it!

 

 

Use the MVC pattern, or more like adapt it to your needs, it will do wonders to make things clear in your code. 

 

Here is what I do:

 

=> If you need to use a WEB API, or some sort of data fetching mechanism WRAP it up like this:

- APIWrapper (the class you use to call the different data functions)

- A client (the class you use to access the stuff)

- Endpoint interface class where all the endpoints are stored

 

=> Model classes

You use those classes to store your data that you fetched. The classes are basically just Getters and Setters that hold your data. Example:

- Car

- Potatoto

- Enemy

 

I would work with some kind of serialization somewhere.

 

=> Controller classes

- Those are the classes that take care of user interaction and manipulation of the data. Then they tell the View classes to display something.

 

=> View Classes

- Everything related to the displaying of the said information

- The handling of the user interaction

 

This is a basic example, but works pretty well to make sense of larger projects.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×