Results 1 to 5 of 5
  1. #1
    pifrely is offline Member
    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Default 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();
    		}
    	}
    }
    ========

    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++;
    		}
    	}
    }
    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?
    Last edited by pifrely; 01-22-2013 at 08:19 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    pifrely is offline Member
    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Default 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 properties
    Last edited by pifrely; 01-24-2013 at 04:46 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: Designing Software

    Pretty much.
    So Thing would hold a reference to Map.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    pifrely is offline Member
    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Default Re: Designing Software

    Hmm, alright; I understand now. Thanks.

Similar Threads

  1. Replies: 0
    Last Post: 11-09-2010, 06:48 PM
  2. Help designing a program
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 02:31 AM
  3. Replies: 0
    Last Post: 06-19-2010, 08:11 PM
  4. Replies: 0
    Last Post: 01-15-2010, 07:16 AM
  5. Need help for coding and designing
    By g.ganiraju in forum New To Java
    Replies: 11
    Last Post: 11-01-2008, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •