Results 1 to 5 of 5
  1. #1
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default How do I control access to data in a class from specific other classes?

    I'm working on my path finding classes and am having real issues controlling the flow of data, I have a PathFinder class that finds a path between point A on the level and point B, and I have a Path class that stores the data that describes how to follow that path.

    My problem is that I need full access to the data in the Path class from the PathFinder, but want to limit access to just a few getters for anything else. How do I do that in Java?

    Sorry I can't post the actual code, it's a hell of a lot more complicated than I make it sound, the path finding stuff is over 1000 lines long alone and interacts with many other classes that together would be at least another 2000 or so lines. But the basics is this:

    The classes:
    Java Code:
    public class Path
    {
    	private ArrayList<Short> m_pathInstructions;
    		
    	public int GetInstructionCount()
    	{
    		return pathInstructions.size();
    	}
    		
    	public int GetDirection(int instructionIndex)
    	{
    		//get the direction
    	}
    
    	public int GetDistance(int instructionIndex)
    	{
    		//get the distance
    	}
    }
    
    
    
    public class PathFinder
    {
    	public boolean FindPath(Path outputPath, /*other parameters*/)
    	{
    		//Find path
    		
    		if (pathFound)
    		{
    			//problem here since m_pathInstructions is private
    			outputPath.m_pathInstructions.clear();
    
    			for (/*loop over the found path which uses different data to a Path*/)
    			{
    				//work out what the next instruction should be
    
    				//problem here since m_pathInstructions is private
    				outputPath.m_pathInstructions.add(instruction);
    			}
    		}
    		return pathFound;
    	}
    }

    And then use it something like the following without being able to access m_pathInstructions directly as the implementation may need to change:
    Java Code:
    boolean pathFound = pathFinder.FindPath(path, /*other parameters*/);
    if (pathFound)
    {
    	for (int i = 0; i < path.GetInstructionCount(); ++i)
    	{
    		int direction = GetDirection(i);
    		int distance = GetDistance(i);
    		//use direction and distance to follow path
    	}
    }
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    nest pathFinder into path so that any path can have a method to getDirections(this,anotherPath) ? then pathFinder can be private but only accessed from the Path

  3. #3
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    nest pathFinder into path so that any path can have a method to getDirections(this,anotherPath) ? then pathFinder can be private but only accessed from the Path
    It's not just PathFinder that needs access to the data in Path, there's another massive class like PathFinder that finds a Path using pre-calculated path nodes.

    Would creating an empty interface or abstract nested class in Path, then having PathFinder and the other class implement/extend it still grant them access to the data in Path?

    I would like to keep them in separate files if possible as I hate having 5000+ line files.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Maybe sometimes you do need a 5000+ line file.

    Other than that I think you can keep the files separate but have them in a different sub-package, and limit access to the Paths package as you need

    EDIT:

    download chapter 18 in this tutorial
    Developing Games in Java

    look in the 'src' folder to see how the sub packages are organised
    Last edited by ozzyman; 04-17-2011 at 11:46 AM.

  5. #5
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    Maybe sometimes you do need a 5000+ line file.

    Other than that I think you can keep the files separate but have them in a different sub-package, and limit access to the Paths package as you need

    EDIT:

    download chapter 18 in this tutorial
    Developing Games in Java

    look in the 'src' folder to see how the sub packages are organised
    Already had it all set out in sub-packages, but didn't realize that protected gave access to other classes in the same package not just to classes that extend it. That should suit my needs just fine :).

    Thanks :).
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

Similar Threads

  1. Access Control Exception
    By bonjovi4u in forum Java Applets
    Replies: 5
    Last Post: 08-26-2010, 02:16 PM
  2. network access control
    By yuhobebbho in forum Networking
    Replies: 0
    Last Post: 05-05-2010, 01:05 PM
  3. Replies: 0
    Last Post: 04-04-2010, 11:06 AM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 05:19 AM
  5. access control exception,while running rmi
    By megha in forum Advanced Java
    Replies: 3
    Last Post: 10-26-2008, 05:10 PM

Posting Permissions

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