Results 1 to 2 of 2
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    45
    Rep Power
    0

    Default Generalizing iterators for a CAD program? (want function pointers - maybe)

    I am working on a CAD program in Java.

    I have a class called a "Block". The data model consists of an ArrayList of these Blocks. Each type of block has a number of "Pins" which is how the Blocks are connected to each other. The actual Blocks are extended classes from the base Block. The Pins for each type of Block are generated by the Blocks' constructors.

    I'm noticing that my code is starting to get a million sections of code where I'm iterating through the list of blocks, and also within each block, iterating through the list of pins. What I'd like to do is to abstract these iterators out to methods in my "Model" class, and then within the other functional areas of the program, be able to do something like (pseudo code example):

    Saving the model to a file:

    foreach Block:
    ----Save Block to file.
    ----foreach Pin
    --------if (Pin.connection != null)
    ------------ save Pin.connection to file

    Redrawing the model on screen:

    foreach Block:
    ----[Draw the block].
    ----foreach Pin
    --------if (Pin.connection != null)
    ------------ [Draw the pin connection]

    etc. etc.

    Sometimes I just need to iterate the blocks and other times I need to iterate the blocks and the pins, and other times I just need to iterate the pins.

    If I were using C++ I would use function pointers to pass in for the per-block or per-Pin functions. There doesn't seem to be direct support for function pointers in Java although I've read a few things about getting around it. I can't honestly say that I understand the few things I have read, such as:

    closures - What's the nearest substitute for a function pointer in Java? - Stack Overflow

    Suggestions?
    Last edited by Digital Larry; 03-31-2013 at 05:16 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Generalizing iterators for a CAD program? (want function pointers - maybe)

    I'm really not sure what you're asking, or more specifically what problem you're trying to solve. Can you provide an SSCCE demonstrating exactly what the problem is?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Iterators
    By Archer in forum New To Java
    Replies: 5
    Last Post: 04-09-2011, 12:13 PM
  2. generics and iterators
    By TopNFalvors in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 05:18 PM
  3. Understanding Iterators
    By Domo230 in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 12:03 AM
  4. Function pointers?
    By AedonetLIRA in forum New To Java
    Replies: 8
    Last Post: 01-18-2011, 04:01 AM
  5. Why doesn't my program function correctly?
    By robertbob in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 01:03 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
  •