Results 1 to 9 of 9
  1. #1
    Briksins is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default Call method in all objects which implement certain interface

    Hello

    I got a question here, hope that some of you would be able to explain to me how it should be implemented.

    For example if some of you worked with Unity Game engine (C#) the idea is that game has main loop and once per cycle it call for example Update() method in all objects which implement certain interface.
    I would like to repeat such pattern in Java for another another program, not even game related, but I would still need a main loop and event driven behaviour with async call backs

    So question is how to implement the fallowing scenario:

    Imagine i have interface which implement some methods and one of them is Execute()
    I have the main controller class which implement main loop, also multiple other classes which implement the same interface with method Execute()

    How can i call this Execute() method on all objects which implement that interface each loop cycle?
    Should i keep and track reference of each object which was implemented with this interface and go through inner "for" loop trough each reference and call manually Execute() method in each of them?

    what if each object implementing interface have to run Execute() method simultaneously? in parallel independent from each other?

    Referring back to Unity engine and their Update() method - there is exactly the same situation:
    you can have multiple objects with script attached, thats script implement interface which has multiple methods and one of them is Update()
    and once per cycle all objects with that Update() method will be executed in parallel independently


    Thank you for hints in advance

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Call method in all objects which implement certain interface

    There are two ways that quickly come to mind. First, have all the objects in an array or list and simply iterate thru them calling the Execute method. Another way is to have your Execute method event driven. Then each class that implements the interface can register with the appropriate controller to receive events and use that to call the method. Which of course usually results in the first recommendation occurring. If you want parallel execution you will have to learn about threads. There are quite a few thread utility classes in the java.util.concurrent package.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Briksins is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default Re: Call method in all objects which implement certain interface

    I believe from what you describe more appropriate and mature solution would be 2nd variant with registration for an event...
    The problem is that i have no idea how to do it in Java, all this parallel tasking, events and call back are very poorly made in Java comparing to C# with delegates and proper async tasks.

    Thank you in anyway for your replay!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Call method in all objects which implement certain interface

    Quote Originally Posted by Briksins View Post
    I believe from what you describe more appropriate and mature solution would be 2nd variant with registration for an event...
    The problem is that i have no idea how to do it in Java, all this parallel tasking, events and call back are very poorly made in Java comparing to C# with delegates and proper async tasks.

    Thank you in anyway for your replay!
    If you don't know how to do it in Java, then how can you claim it is poorly done?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    rushtojess is offline Member
    Join Date
    Apr 2015
    Posts
    21
    Rep Power
    0

    Default Re: Call method in all objects which implement certain interface

    Hi all,

    I am hoping you can help me with a problem I am facing with a JAVA code needed for my assignment.
    We are implementing a Card Game.. There is a Card Class which i have not included in the code below .
    We have a Dec class that has various arrays..
    private Card[] masterPack >> Is a master Pack that is initialized once and has all the 52 cards.
    private Card[] cards >> Is the array that has the card values from masterPack array.

    My problem is :
    After I called the Deck object D1, it goes to the Deck COnstrucutor OK
    It then populates the values of masterPack as per the allowmasterPack method.
    Then it stores the values of masterPack into cards array in the init method..
    I get an exception when line below is executed..

    cards[pack * 52 + k]= masterPack[k];

    I get a java.lang.NullPointerException error..
    Please can anyone help me. Much appreciated


    public class debug
    {
    public static void main(String[] args)
    {
    int pack = 1;
    Dec D1;
    D1 = new Dec(pack);
    }
    }

    class Dec
    {

    public static final int MAX_CARDS = 312;
    private Card[] masterPack;
    private Card[] cards;
    private int topCard;
    private int numPacks = 1;
    public static int count;

    private void allocateMasterPack()
    {
    System.out.println("allocateMasterPack() activated");
    Card[] masterPack = new Card[13];

    System.out.println(" Construting the masterPAck");
    for (Card.Suit st: Card.Suit.values())
    {
    masterPack[13 * k] = new Card('A', st);
    for (val='2',j = 1; val<='9'; val++, j++)
    masterPack[(13 * k) + j] = new Card(val, st);
    masterPack[(13* k)+ 9] = new Card('T', st);
    masterPack[(13* k)+ 10]= new Card('J', st);
    masterPack[(13* k)+ 11]= new Card('Q', st);
    masterPack[(13* k)+ 12]= new Card('K', st);
    k++;
    }

    public Dec(int val)
    {
    allocateMasterPack();
    System.out.println("Printing the values in constructor");
    cards = new Card[52];
    for (int k = 0; k < cards.length; k++)
    cards[k]= new Card();
    init(val);

    }

    public void init(int val)
    {
    int k, pack;

    if (val < 1 || val > 6)
    val= 1;

    // hand over the masterPack cards to our deck
    for (pack = 0; pack < val;pack++)
    for (k = 0; k < 52; k++)
    cards[pack * 52 + k]= masterPack[k]; >> Geting an error here -- Null Pointer exception
    this.numPacks = val;
    topCard = val* 52;
    }
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Call method in all objects which implement certain interface

    Hijacking someone elses thread is bad form. Start your own thread. And before you post again, please read the forum rules.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Call method in all objects which implement certain interface

    Please start your own threads for your own problems.

    You have declared masterPack in the allocateMasterPack method AGAIN. Which means it's only declared for the lifetime of the method. Leave out Card[] so the method uses the field. To make sure, prefix the variable with "this", to make sure you are referencing the field.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    rushtojess is offline Member
    Join Date
    Apr 2015
    Posts
    21
    Rep Power
    0

    Default Re: Call method in all objects which implement certain interface

    Sorry, I ddint mean to hyjack another thread..
    I didn't know where to start a new thread.
    Can someone please point me to the right direction.

    I have initilaised masterPack only once , in the allowMasterpack method.
    cards is another array that we use to copy the cards from masterpack[] to cards[]

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Call method in all objects which implement certain interface

    Quote Originally Posted by rushtojess View Post
    Sorry, I ddint mean to hyjack another thread..
    I didn't know where to start a new thread.
    Can someone please point me to the right direction.
    Go to Forum. Pick an appropriate forum (e.g. New to Java). Then click on Post New Thread, top left of screen.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. implement interface
    By Harshit in forum New To Java
    Replies: 1
    Last Post: 04-19-2014, 01:17 PM
  2. Implement Comparable interface
    By JojoDiaz in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 07:10 AM
  3. how to implement interface
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-09-2008, 02:39 PM
  4. Can generic types implement an interface?
    By jdgallag in forum New To Java
    Replies: 3
    Last Post: 10-30-2008, 04:07 AM
  5. How to implement the Icon interface in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:17 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
  •