Create a set method for different variables

Asked

Viewed 432 times

3

I’m looking to create a Setter for an object Character, where it will modify the attributes String name, int intellect, int strength and int stamina, but what is the best method to do this?

public class Character {

    private String name;
    private int intellect;
    private int strength;
    private int stamina;

    public void setAttribute( ??? ) {

    } // fim do método setAttribute
} // fim da classe Character
  • 1

    Better in what sense? It is not clear what your doubt is. It would be a matter of coding style, or maybe how to create a good API for calling methods, etc?

2 answers

7


There are some forms, and they follow the pattern of Object-Oriented Programming and are:

1 - Default with Get/Set:

public class Character 
{       
    private String name;
    private int intellect;
    private int strength;
    private int stamina;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getIntellect() {
        return intellect;
    }
    public void setIntellect(int intellect) {
        this.intellect = intellect;
    }
    public int getStrength() {
        return strength;
    }
    public void setStrength(int strength) {
        this.strength = strength;
    }
    public int getStamina() {
        return stamina;
    }
    public void setStamina(int stamina) {
        this.stamina = stamina;
    }
}

2 - Standard with Get/Set + Constructor:

public class Character 
{       
    public Character(){ }
    public Character(String name, int intellect, int strength, int stamina){ 
        this.name = name;
        this.intellect = intellect;
        this.strength = strength;
        this.stamina = stamina;
    }
    private String name;
    private int intellect;
    private int strength;
    private int stamina;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getIntellect() {
        return intellect;
    }
    public void setIntellect(int intellect) {
        this.intellect = intellect;
    }
    public int getStrength() {
        return strength;
    }
    public void setStrength(int strength) {
        this.strength = strength;
    }
    public int getStamina() {
        return stamina;
    }
    public void setStamina(int stamina) {
        this.stamina = stamina;
    }
}

3 - Standard with Get/Set + Constructor + Fluent Interface

public class Character {
    public Character() { }
    public Character(String name, int intellect, int strength, int stamina){ 
        this.name = name;
        this.intellect = intellect;
        this.strength = strength;
        this.stamina = stamina;
    }
    private String name;
    private int intellect;
    private int strength;
    private int stamina;
    public String getName() {
        return name;
    }
    public Character setName(String name) {
        this.name = name;
        return this;
    }
    public int getIntellect() {
        return intellect;
    }
    public Character setIntellect(int intellect) {
        this.intellect = intellect;
        return this;
    }
    public int getStrength() {
        return strength;
    }
    public Character setStrength(int strength) {
        this.strength = strength;
        return this;
    }
    public int getStamina() {
        return stamina;
    }
    public Character setStamina(int stamina) {
        this.stamina = stamina;
        return this;
    }
}

How to use:

Following the 3 containing all the examples below follows the code:

Fluent Interface:

Character ca1 = new Character();
        ca1
            .setName("Nome 1")            
            .setIntellect(1)
            .setStrength(2)
            .setStamina(3);

By the Builder:

Character ca2 = new Character("Nome 1", 1, 2, 3);

Traditional Form:

Character ca3 = new Character();
ca3.setName("Nome 1");            
ca3.setIntellect(1);
ca3.setStrength(2);
ca3.setStamina(3);
  • 1

    Thanks! I haven’t seen it yet Fluent Interface, I will search, I only saw the implementation interface for now, but I didn’t understand very well when you say Code. in his examples.

  • @Brunoneuman the Code is the package, I will remove to be more readable, I’m sorry !!!

  • 1

    I understood =P so yes as the Keys would say :)

  • Taking advantage, there is no way to create a Setter universal for all variables, where I declared constants to reference the variables of the method Setter?

  • 1

    With Reflection you could achieve yes, but honestly it would be a gambiarra, the thinner your class the better is your performace and Standard in POO helps too much in maintenance, ... the more correct are these ways and the standards employed by POO!

  • If you could just take a look, would that be PURE gambiarra or code saving? http://pastebin.com/xP1inyY3

  • @Brunoneuman in my experience as a programmer says that it is functional, but out of standard and for me gambiarra. Excuse me in giving my opinion ...

  • 1

    I WANT your opinion hahah, I am starting in programming and I would like to avoid to the maximum these means "functional" so it is very worth your opinion :) Thank you even for your attention!

  • 1

    @Brunoneuman since you want opinions, I’d say what you’ve done is unnecessary: myChar.setAttribute( myChar.STAMINA, 45 ); for example. You spend a lot of time doing something that will make your code more complex and the end result is the same, assigning a value to a private attribute of the object. There are numerous good reasons for you not to do so, one of them is the Javabean Standard, one of the things that tries to standardize are the getters and setters to facilitate communication between developers, another reason is that Ides already generate getters and setters automatically for you.

  • 1

    @Maria, what is this "Fluent Interface"? Is it something that is commonly practiced and has this name? I’m only asking out of curiosity because I’ve never heard of.

  • 1

    @Math is a design standard and is used yes in all object-oriented languages, is a facilitator to my view at the time of typing and you can see this pattern and a lot when using expressions to assemble a string of settings and at the end runs. In fact they speak "Builder" with features of "Fluent interface", but, it means the same thing. At this link: http://pt.slideshare.net/kindblad/the-fluent-interface-pattern it may be clearer for you! In the example 1 and 2 does not change much but, the three set a difference because, points to the class repair that item.

  • 1

    Boy, I always liked wearing that pattern Fluent but never knew the name, very good to have quoted this pattern. + 1

Show 7 more comments

3

Would you like to create just one method to set the 4 variables? If yes, I think the best way would be this one:

public class character {

    private String name;
    private int intellect;
    private int strength;
    private int stamina;

    public void setAttribute(String name, int intellect, int strenght, int stamina) {
         this.name = name;
         this.intellect = intellect;
         this.strength = strenght;
         this.stamina = stamina;
    }
}

Any other questions you may ask!

  • Yes, I would like to create a single method Setter so that it can be used for at least the 3 variables of type int, where I could "know" when calling the method Setter inform who I would like to assign the value, for intellect variable, for Strength or for stamina

Browser other questions tagged

You are not signed in. Login or sign up in order to post.