Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: API singleton theory

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Wink API singleton theory

    Okay, i've been struggling with this for a while now so i thought i'd ask the experts.

    I have a file Combat.java
    Another file, Actions.java, uses methods stored in Combat.java
    So i have a class, MethodProvider.java, which initializes the classes. I extend MethodProvider.java to use these variables.

    Situation:


    I know why this results in this error, but how can i do it correctly?
    How would i make MethodProvider appropriate?
    Last edited by dunnkers; February 22nd, 2011 at 04:29 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: API singleton theory

    Well, clearly you are doing something very memory heavy, most likely some sort of recursion, in your dunkscripts.game.Actions() constructor. Go through that constructor (and the methods it calls) and see if you are forgetting anything in either recursion or looping (perhaps on line 10 of the Actions class).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: API singleton theory

    theres obviously recursion going on, but i don't know how to do it the right way. anyone?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: API singleton theory

    Well without seeing the code where the recursion is done incorrectly, there is no way we can tell what you are doing wrong and provide help.


    EDIT: Oh wait, I see what is happening. The problem is that Actions extends MethodProvider, but an Actions object is created when a new MethodProvider is created. So basically, when you create an Actions object, it creates an Actions object.

    So I guess my question would be: Why do you think that either:
    a) Actions needs to inherit from MethodProvider?
    b) An Actions object needs to be created in your MethodProvider?

    The way to fix this will depend entirely on how you have your project structured and how you are planning on using the Actions/MethodProvider objects.
    Last edited by aussiemcgr; February 22nd, 2011 at 05:48 PM. Reason: Epiphany
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: API singleton theory

    I need them so i'd easily type

    combat.someMethod();

    instead of

    new dunkscripts.game.Combat().someMethod();

    and i'd wouldn't need to initialize another Combat in each class i'd like to use it.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: API singleton theory

    Ok, well what you currently have I have no idea how to describe. It is inheritance, but not a practical use of inheritance.

    It sounds like what you are wanting to do is create a class where you have variables that can be used throughout your project. There are a few tidbits about doing this. Namely, in order to be invoking calls on the same object (that contains the variables) throughout your project, you will have to pass that object around your project. Those variables (such as combat) will be passed alongside the object.

    Now, I think what you have assumed is that you can simply say:
    //Assuming the MethodProvider is passed around and not creating a new one each time
    MethodProvider provider = new MethodProvider();
     
    provider.combat.someMethod();
    and it will know what you are talking about since Combat extends MethodProvider. Unfortunately, it doesn't quite work that way. Now, it will work that way if Combat didn't extend MethodProvider.

    The reason is because there is no logistical reason for Combat to be a MethodProvider, which is what happens when you extend MethodProvider. Keep in mind that if the code you have for MethodProvider did work, that the Combat class would contain a variable named actions (which would be an Actions object), a variable named handling (which would be a Handling object), a variable named dcombat (which would be a Combat object), and a variable named data (which would be a Data object) simply because Combat extends MethodProvider. That is not what I think you are trying to do.

    I can see how you could think what you did makes sense, but it is an example of one of those things that is great on paper but doesn't really translate to practice. Now, that doesn't mean that we can't get something similar to work, but it will take some feedback between you and me to get our brains synced up. I'm still trying to figure out what you think this can do and what you are trying to do. Once I have a better understanding of how this entire thing will be used, I will actually be able to get you to an answer.
    Last edited by aussiemcgr; February 22nd, 2011 at 06:18 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    dunnkers (February 23rd, 2011), JavaPF (February 23rd, 2011)

  8. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: API singleton theory

    Quote Originally Posted by aussiemcgr View Post
    Ok, well what you currently have I have no idea how to describe. It is inheritance, but not a practical use of inheritance.

    It sounds like what you are wanting to do is create a class where you have variables that can be used throughout your project. There are a few tidbits about doing this. Namely, in order to be invoking calls on the same object (that contains the variables) throughout your project, you will have to pass that object around your project. Those variables (such as combat) will be passed alongside the object.

    Now, I think what you have assumed is that you can simply say:
    //Assuming the MethodProvider is passed around and not creating a new one each time
    MethodProvider provider = new MethodProvider();
     
    provider.combat.someMethod();
    and it will know what you are talking about since Combat extends MethodProvider. Unfortunately, it doesn't quite work that way. Now, it will work that way if Combat didn't extend MethodProvider.

    The reason is because there is no logistical reason for Combat to be a MethodProvider, which is what happens when you extend MethodProvider. Keep in mind that if the code you have for MethodProvider did work, that the Combat class would contain a variable named actions (which would be an Actions object), a variable named handling (which would be a Handling object), a variable named dcombat (which would be a Combat object), and a variable named data (which would be a Data object) simply because Combat extends MethodProvider. That is not what I think you are trying to do.

    I can see how you could think what you did makes sense, but it is an example of one of those things that is great on paper but doesn't really translate to practice. Now, that doesn't mean that we can't get something similar to work, but it will take some feedback between you and me to get our brains synced up. I'm still trying to figure out what you think this can do and what you are trying to do. Once I have a better understanding of how this entire thing will be used, I will actually be able to get you to an answer.
    Thanks for your reply aussiemcgr,
    but this is not quite what i mean.

    What i try to prevent is that i have to create a large number of variables in each class i want to use other classes in. I'd have to write this in each class i'd like to use these methods in:
    	public final dunkscripts.game.Actions actions = new dunkscripts.game.Actions();
    	public final dunkscripts.game.Handling handling = new dunkscripts.game.Handling();

    And in programming we don't want to write anything twice right? This can get smarter.
    What if we'd initialize these variables in a different class and extend this class to use them?

    The only problem is that when calling MethodProvider in i.e. Actions, it will also initialize another Actions, defined in MethodProvider because i.e. Handling needs Actions. This will also call MethodProvider, and so on.

    This is what i'm trying to do and what i tried to explain. I hope you'll understood this and hopefully you can help me now.

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: API singleton theory

    Ok, so are you saying that Handling is extending MethodProvider because method provider contains a variable (actions) that is an Actions object and Handling needs to be able to use that Actions object for various reasons?

    If so, then the Actions class does not need to extend MethodProvider, since it does not need anything from MethodProvider and an Actions object is created in MethodProvider. This would also mean that you would not need to create a Handling object (handling) in the MethodProvider class, since Handling is a MethodProvider object.

    That would all be assuming of course that Actions does not need access to any of the variables in MethodProvider.

    What if we'd initialize these variables in a different class and extend this class to use them?
    Not necessarily. Remember that when you create a class, you are giving yourself the ability to create a new type of Object. If the new Object is created in a class, the class we have created the new object in has access to all of the new Object's public instance variables (as opposed to private and protected) by using that new object. When you extend a class, you are saying that the extending class is a version of the class it is extending, but with more features. So by making Actions extend MethodProvider, Actions becomes a MethodProvider object (with the added features specified in the Actions class). But, if Actions didn't extend MethodProvider, and we created a MethodProvider object in the Actions class, we have access to all of MethodProvider's instance variables (provided they are public). There is a small catch with this though. We will need 2 constructors in the Actions class. The first constructor will be a constructor that we will call from all the code other than MethodProvider, and the second will be a constructor that MethodProvider will call when it attempts to create the Actions object. The difference between the two constructors will be that a MethodProvider object SHOULD NOT be initialized in the constructor that is called from the MethodProvider class, or we have the same problem all over again (since Actions and MethodProvider will recursively create each other). Alternatively, you can have 1 constructor and a "trigger-method", which will NOT BE CALLED in the MethodProvider and will serve the sole purpose of initializing a MethodProvider object.

    Tell me if that 1) makes sense and 2) goes along with what you are trying to do.

    I kind of wish one of the other helpers will step in this with us since one of them might know alternatively solutions that I know nothing about.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: API singleton theory

    The thread is titled 'API Singleton Theory', but there's no mention of singletons in the discussion. The OP doesn't describe what he's trying to achieve functionally (e.g. what are the classes supposed to do?), only in vague architectural terms, without providing any helpful code. So it's difficult to suggest a solution.

    AIUI, if you want to provide effectively global access to an object and its methods, the use of singletons is generally eschewed in favour of Inversion of Control techniques, e.g. dependency injection.

    Inheritance is generally not the way to do it. Try to restrict inheritance to 'IS A' relationships to follow LSP (Liskov Substitution Principle).

  11. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: API singleton theory

    Sorry for all the confusion guys, but what im basically trying to do is using my API in a class without defining variables in everytime.

  12. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: API singleton theory

    Quote Originally Posted by dunnkers View Post
    Sorry for all the confusion guys, but what im basically trying to do is using my API in a class without defining variables in everytime.
    That's even less helpful...

    What API, what variables? You can't call a method without an object to call it on. In a method of class A you can call the methods of class A directly and the methods of other classes via variables - parameters, local variables, or member variables. A class should have a single, clearly defined role, and its methods should relate to that role. That is why we don't put everything into a single class.

    Maybe you could explain what you're trying to achieve - just describe the application in terms of function, and explain what Actions, Combat, Handling, and Data are for - what they do, how they interact. Then perhaps it will be clearer. You may know what you're talking about, but I don't.

Similar Threads

  1. Problem with loop theory
    By Lord eMO in forum Loops & Control Statements
    Replies: 1
    Last Post: October 27th, 2017, 07:46 PM
  2. Theory/Question,
    By Time in forum Java Theory & Questions
    Replies: 7
    Last Post: November 9th, 2010, 05:26 PM
  3. Some Theory based questions
    By Bacon n' Logic in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 1st, 2010, 06:23 PM
  4. Theory Inquiry
    By b_jones10634 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 19th, 2010, 08:21 AM
  5. Singleton pattern: the purpose of empty constructor
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: August 6th, 2010, 08:58 AM