我已经阅读了Head First OOAD的第一章,其中有Guitar示例,

吉他班

public class Guitar {
private String serialNumber;
private double price;
private String model;
private Type type;
private Builder builder;
private Wood backWood;
private Wood topWood;
}


在上面的Builder类中,Wood和Type是枚举类型。
库存类具有吉他列表,并提供添加,搜索和获取方法。

库存类别

class Inventory {
List<Guitar> guitarList;

public Inventory() {
    guitarList = new ArrayList<Guitar>();
}


public boolean addGuitar(Guitar guitar){
    return guitarList.add(guitar);
}

public Guitar getGuitar(Guitar guitar) {
    for(Guitar guitarData : guitarList ) {
        if(guitarData.getSerialNumber().equals(guitar.getSerialNumber())){
            return guitarData;
        }
    }
    return null;
}

public List<Guitar> search(Guitar guitar) {
    List<Guitar> list = new ArrayList<Guitar>();
    for(Guitar guitarData : guitarList ) {
        if(guitar.getBuilder() == null || !guitar.getBuilder().equals(guitarData.getBuilder())) {
            continue;
        }

        if(guitar.getTopWood() == null || !guitar.getTopWood().equals(guitarData.getTopWood())) {
            continue;
        }

        if(guitar.getBackWood() == null || !guitar.getBackWood().equals(guitarData.getBackWood())) {
            continue;
        }

        if(guitar.getType() == null || !guitar.getType().equals(guitarData.getType())) {
            continue;
        }

        if(guitar.getModel() == null || !guitar.getModel().equals(guitarData.getModel())) {
            continue;
        }
        list.add(guitarData);
    }
    return list;
}
}


在书中,为库存中的搜索方法创建另一个类GuitarSpec,因为在这种方法中,客户端永远不会提供吉他的序列号和价格。

更新的课程:

吉他班

class Guitar {
private String serialNumber;
private double price;
private GuitarSpec guitarSpec;

public String getSerialNumber() {
    return serialNumber;
}
public double getPrice() {
    return price;
}
public GuitarSpec getGuitarSpec() {
    return guitarSpec;
}

}


吉他规格分类:

class GuitarSpec {
private String model;
private Type type;
private Builder builder;
private Wood backWood;
private Wood topWood;

public String getModel() {
    return model;
}
public Type getType() {
    return type;
}
public Builder getBuilder() {
    return builder;
}
public Wood getBackWood() {
    return backWood;
}
public Wood getTopWood() {
    return topWood;
}

}


和更改库存类搜索方法,

public List<Guitar> search(GuitarSpec searchSpec) {
    List<Guitar> list = new ArrayList<GuitarSpec>();
    for(Guitar guitar : guitarList ) {
        GuitarSpec guitarSpec = guitar.getGuitarSpec();
        if(searchSpec.getBuilder() == null || !searchSpec.getBuilder().equals(guitarSpec.getBuilder())) {
            continue;
        }

        if(searchSpec.getTopWood() == null || !searchSpec.getTopWood().equals(guitarSpec.getTopWood())) {
            continue;
        }

        if(searchSpec.getBackWood() == null || !searchSpec.getBackWood().equals(guitarSpec.getBackWood())) {
            continue;
        }

        if(searchSpec.getType() == null || !searchSpec.getType().equals(guitarSpec.getType())) {
            continue;
        }

        if(searchSpec.getModel() == null || !searchSpec.getModel().equals(guitarSpec.getModel())) {
            continue;
        }
        list.add(guitar);
    }
    return list;
}


我不明白为什么要创建新的GuitarSpec,根据要求,这是必需的吗?

最佳答案

这是因为每把吉他都有一个规格,但并非每一个规格都有/是一个吉他
但是,当您搜索具有特定规格的吉他时,
您将基本上将每种吉他的规格与要搜索的规格进行比较。
但是您不会搜索序列号和价格。

这样,您可以在必须创建吉他的情况下使用Guitarspec类

08-06 04:22