Create a set method for different variables

3

I want to create a setter for a Character object, where it will modify the attributes String name , int intellect , int strength and int stamina , but what is the best method for if you do that?

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
    
asked by anonymous 12.08.2014 / 02:16

2 answers

7

There are a few 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 - Pattern with Get / Set + Builder:

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 - Pattern 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 that contains all the examples follows the code below:

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);

Traditionally:

Character ca3 = new Character();
ca3.setName("Nome 1");            
ca3.setIntellect(1);
ca3.setStrength(2);
ca3.setStamina(3);
    
12.08.2014 / 02:58
3

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

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 might ask!

    
12.08.2014 / 02:43