Tutorial Why to use void and returning the main instance for building objects! / Chain-able Constructor build

Discussion in 'Resources' started by ChipDev, Dec 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ChipDev

    Hello guys!
    I was building this cool thing a while ago, before @MiniDigger beat me to this! It is a chainable itemstack that allows you to call different (executing, not void) methods on the same object!
    example:
    Code:
    ChipDev me = new ChipDev().name("blake").location("Chicago").age(int).height(5F); (etc)
    You see them in signatures a-lot!
    With a void method:
    Code:
    ChipDev me = new ChipDev();
    me.name("blake");
    me.location("Chicago");
    //etc
    How do I do these magical instance-building objects? Its really easy!
    (You can also convert voids to them, But you need a .build() method in the end to make your whatever to a Bukkit object.. If this is for bukkit!)
    Code:
    public *YourConstructorOrClassName* *methodname* {
           return this.object;
          //That means the constructor!
    }
    now, you can do this:
    *YourConstrunctorOrClassName*.*MethodName*().*MethodName*();
    Thanks for reading this crappy tutorial :p
    Cya!
     
    Last edited: Dec 24, 2014
  2. Offline

    teej107

    You can return what ever to choose to.
     
  3. Offline

    ChipDev

    Yeah, but for this, Its common to return the main instance. (Im not saying that you HAVE the object, that can be whatever), so you are right.
     
  4. Offline

    Shawckz

    Why to use void and returning the main instance for building objects!

    How to make a chainable builder*
     
    ChipDev and Avygeil like this.
  5. Offline

    Avygeil

    @ChipDev It's not related to Bukkit. This kind of pattern is very common in Java and you really didn't have to make a new resource about your new favorite way to make objects...
    As a general practise, I don't think it's good to mix the two. If you needed to call 2-3 chainable methods on your new object to give it sense, then your object lacks a constructor with more arguments. If your constructor has too many arguments, then it lacks a Builder/Factory. ;)

    Also note that you can also use chainables to control the flow of your program. You have good examples in Guava and Java 8 : Optional#orElseGet, Optional#orElseThrow, ...
     
    ChipDev likes this.
  6. Offline

    ChipDev

    Its optional, But you are right!
     
  7. @ChipDev I was reading this and was like "It's going so well", until I read "reutrn" in your code.
     
  8. Offline

    ChipDev

    Well, You have to return the constructor (WHICH I PUT AS this.object).
    EDIT: Oh.
    How the heck did that get by me?
     
    Last edited by a moderator: Dec 24, 2014
    DJSkepter likes this.
  9. Offline

    MisterErwin

    @ChipDev
    1) You do not return a constructor...
    2) Terminology...
    3) This is NOT a tutorial...
    So... then show us HOW you do it....

    4) Merry Christmas - But you still should consider buying a java book...
     
    Shawckz likes this.
  10. Offline

    ChipDev

    By that I mean returning a object which is created by a constructor.
     
  11. Offline

    MisterErwin

    @ChipDev But then that is no chainable method...
     
  12. Offline

    ChipDev

    Returning the NEW object.
     
  13. Offline

    MisterErwin

    @ChipDev Then name the method build, and not *methodname*

    But otherwise... the other bullets still aply...
     
  14. Offline

    _Filip

    With a void method:
    Code (Text):
    1. ChipDev me = new ChipDev();
    2. me.name("blake");
    3. me.location("Chicago");
    4. //etc
    First of all, "a-lot" is written as "a lot".
    Second, your methods in the "void method" example use incorrect naming conventions...
    Lastly, why are you making a JAVA tutorial in a BUKKIT API forum?
     
  15. Offline

    ChipDev

    Hey, aren't all tutorials on bukkit for java..?
     
  16. @ChipDev Not always, most of them are trying to aid people and guide them towards using the BukkitAPI effectively.
     
  17. Offline

    ChipDev

  18. Offline

    _Filip

    @ChipDev What are you even saying? Learn english...
     
  19. Offline

    ChipDev

    I know English.
    "I Estimated on the 'All' A little bit'
    I thought you knew that.
     
  20. @ChipDev
    You said "approximated". And "estimated" is still wrong, I believe you mean "exaggerated". This thread has derailed already, just like all of your threads. I recommend a lock.
     
  21. Offline

    ChipDev

    1) Thats the word Im looking for
    2)That post isn't helping in the derailing.. I just replied to someone who was derailing someone in the first place.
    3) Recommend a lock? No. I like whenever I get an alert!
     
  22. Offline

    _Filip

    API_Tutorials and ChipDev like this.
  23. Offline

    teej107


    You think this tutorial is crappy? That's bad self esteem! If you think this tutorial is crappy, improve it rather than argue about it.
     
    Totom3 likes this.
Thread Status:
Not open for further replies.

Share This Page