使用Java的易于访问对象获取器



方法1:传统getter/setter

丰田类:

public class ToyotaCar implements Serializable {
  private static final long serialVersionUID = 2011932556974180375L;
  private int miles;
  public void addMiles(int miles){  
    this.miles = miles;
  }
  public int getMiles(){
  return miles;
  }
}

人类阶级:

public class Human implements Serializable {
  private static final long serialVersionUID = 1748193556974180375L;
  private ToyotaCar car;
  public void setCar(ToyotaCar car){  
    this.car = car;
  }
  public int getCar(){
  return car;
  }
  public void addCarMiles(int num){
    getCar().addMiles(num);
  }
}

方法2:"其他"

丰田班: - 上述丰田类 -

其他容器手班:

public enum HumanContentsContainer {
  CAR{
    @Override public Object getContainer(){
      return new ToyotaCar();
    }
  },
  HOUSE;
  public Object getContainer(){ //because cannot be static enum constant as every human has different items
  return null;
  }
}

人类阶级:

public class Human implements Serializable {
  private static final long serialVersionUID = 1748193556974180375L;
  private HashMap<HumanContentsContainer, Object> contents;
  public void setContents(){  
    for (HumanContentsContainer c : HumanContentsContainer.values()){
      contents.put(c, c.getContainer());
    }
  }
  public HashMap<HumanContentsContainer, Object> getContents(){
    return contents;
  }
  public void addCarMiles(int num){
    //TODO how to replicate this: getCar().addMiles(num);???
  }
  //TODO i dont want to use the below method because whats the point of creating a whole container handler if im just going to use a traditional getter again?
  //public ToyotaCar getCar(){
 //   return (ToyotaCar) contents.get(HumanContentsContainer.CAR);
 // }
}

那么,如何使用传统的Getter而不实际创建Getter来复制GetCar((。addmiles(x(方法?

请注意,我也不想执行此操作(以下代码(:因为再次不值得,然后不值得:

public void addCarMiles(int num){
    ((ToytotaCar)contents.get(HumanContentsContainer.CAR).addMiles(num);
  }

寻找一些简单的用法,例如:

human.getContentsThatIsIntanceOf(ToyotaCar).addMiles(1);

,但不知道getContentsThatisinstanceF会如何

我会选择:

public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private ToyotaCar car;
public void setCar(ToyotaCar car){
    this.car = car;
}
public int getCar(){
    return car;
}
public void addCarMiles(int num){
    getCar().addMiles(num);
}
public Map<HumanContentsContainer, Object> getContents(){
    Map<HumanContentsContainer, Object>map = new HashMap();
    map.put(CAR,this.car );
    //same for all the shoes and clothes and whatever the Human has
}
public void setContents(){
    for (HumanContentsContainer c : HumanContentsContainer.values()){
        switch (c){
            case CAR:{
                this.car=c.getContainer();
            }
        }
        //and so on
    }
}

}

编辑

如果您需要具有 dynamic 一组功能,我建议您确实保留对象图,并摆脱" addcarmiles"方法,因为这意味着每个人都有一辆车。

我将在人类'perfercommand(CaperapityType,CablecityCommand(上实现公共方法,"命令将接收能力并在其中执行操作。您可以查看命令模式教程。

编辑2:

如果您要创建一个将返回动态类型的getter,则可以使用generics。

import java.io.Serializable;
import java.util.HashMap;
import java.util.NoSuchElementException;
public class Human implements Serializable {
    private static final long serialVersionUID = 1748193556974180375L;
    private HashMap<Class, Object> contents;
    public void setContents(){
        for (HumanContentsContainer c : HumanContentsContainer.values()){
            contents.put(c.getContainer().getClass(), c.getContainer());
        }
    }
    public HashMap<Class, Object> getContents(){
        return contents;
    }
    public <T> T getContentsThatIsIntanceOf(Class<T> type){
        Object object = contents.get(type);
        if (object==null){
            throw new NoSuchElementException("No such element: "+type.getName());
        }
        return type.cast(object);
    }

    public void usageExample(){
        this.getContentsThatIsIntanceOf(ToyotaCar.class).addMiles(10);
    }
}

最新更新