Results 1 to 5 of 5
Thread: Designing Software
- 01-22-2013, 08:16 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 10
- Rep Power
- 0
Designing Software
Hi, I've been looking into the Model-View-Controller software architecture pattern. But what I constantly wonder is: how do I assign responsibilities to the different parts? ie. I'm stuck between some possible ways of organizing my code:
========Java Code:public class Thing{ public int ypos; public void moveUp(){ if(this Thing is not blocked on the map){ ypos++; } } } public class Model{ public Thing thing; public Map map; public void moveUp(){ thing.moveUp(); } } public class Controller { Model m; View v; public void processInput(){ if(the key w is down){ m.moveUp(); } } }
========Java Code:public class Thing{ public int ypos; } public class Model{ public Thing thing; public Map map; public void moveUp(){ if(thing is not blocked on map){ thing.ypos++; } } } public class Controller { Model m; View v; public void processInput(){ if(the key w is down){ m.moveUp(); } } }
Which one should I go with? And, for future reference, are there any rules of thumb or guidelines for deciding where I ought to start performing the 'actual work' in this manner (as compared to just passing it to another function or data member); should I do it at the very top (ie. Controller does all the work of setting and comparing) or should I pass everything through to the bottom, and let the bottom (the Thing) do the work?Java Code:public class Thing{ public int ypos; } public class Model{ public Thing thing; public Map map; } public class Controller { Model m; View v; public void processInput(){ if(the key w is down && m.thing is not blocked on map){ m.thing.ypos++; } } }Last edited by pifrely; 01-22-2013 at 08:19 PM.
- 01-23-2013, 09:44 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Designing Software
First off, don't have public attributes.
Second, it depends (as does all design).
I would say that Thing knows about the Map. To me that makes more sense than a Map knowing about a Thing (and how it moves), or the Controller knowing how a Thing moves. This is based on the idea that you might want Things that can be blocked in different ways, and the Controller should not know that ThingA is blocked by water, and ThingB is blocked by land.
So, on a move call, Thing asks Map what is in (x,y), which is the location it would be in if the move was valid. Thing can then check whether that is a valid move for it. If not it doesn't move, or supplies a return value indicating it cannot move that direction, but that depends on how you want your system to look.Please do not ask for code as refusal often offends.
- 01-24-2013, 01:01 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 10
- Rep Power
- 0
Re: Designing Software
Hmm, I see. That makes sense. Then, when I encounter this issue again, should I ask myself "Is this class supposed to know (or is responsible for knowing, maybe?) about how another class works in this regard"? And if it shouldn't (as in the case of the Controller not knowing how a Thing moves) then I pass the data to the other class for it to operate on?
Also, just to recap, would the sequence of events in your implementation then be as follows?
Controller sends message to Thing telling it to move up
Thing asks Map what's found on the tile it would go to if it was not blocked
Thing examines that tile and moves up or not depending on that tile's propertiesLast edited by pifrely; 01-24-2013 at 04:46 AM.
- 01-24-2013, 10:04 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Designing Software
Pretty much.
So Thing would hold a reference to Map.Please do not ask for code as refusal often offends.
- 01-26-2013, 08:33 AM #5
Member
- Join Date
- Jan 2013
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Full time Position for Senior Software Engineer / Software Engineer 2 in San Diego
By Illumina Dev in forum Jobs OfferedReplies: 0Last Post: 11-09-2010, 06:48 PM -
Help designing a program
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 02:31 AM -
JAVA - Principal Software Engineer (KeneXa "MNC" Product based Software Company)
By saisrinivaskenexa in forum Jobs OfferedReplies: 0Last Post: 06-19-2010, 08:11 PM -
Software Engineer/Architect at Fast Model Software in Summit, NJ
By CarlSayres in forum Jobs OfferedReplies: 0Last Post: 01-15-2010, 07:16 AM -
Need help for coding and designing
By g.ganiraju in forum New To JavaReplies: 11Last Post: 11-01-2008, 04:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks