Results 1 to 5 of 5
- 04-17-2011, 11:00 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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.
-
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
- 04-17-2011, 11:33 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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.
-
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 organisedLast edited by ozzyman; 04-17-2011 at 11:46 AM.
- 04-17-2011, 01:15 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
Similar Threads
-
Access Control Exception
By bonjovi4u in forum Java AppletsReplies: 5Last Post: 08-26-2010, 02:16 PM -
network access control
By yuhobebbho in forum NetworkingReplies: 0Last Post: 05-05-2010, 01:05 PM -
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 11:06 AM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 05:19 AM -
access control exception,while running rmi
By megha in forum Advanced JavaReplies: 3Last Post: 10-26-2008, 05:10 PM
Bookmarks