使用哪种设计模式来重写代码



Q:对于以下代码片段,确定应该使用哪个设计模式提高代码的质量。使用您确定的设计模式重写代码。你的答案应该包括i( 描述设计模式的语句ii(重写Java代码,以及iii(来自重写的Java代码的测试结果。

public class FitnessCustomer {
private static enum Level {
BRONZE, SILVER, GOLD
}
private Level level;
public void setLevel(Level level) {
this.level = level;
}
public double getFees() {
switch (level) {
case BRONZE: return CustomerConstants.BRONZE_FEES;
case SILVER: return CustomerConstants.SILVER_FEES;
case GOLD: return CustomerConstants.GOLD_FEES;
}
throw new IllegalStateException("How did I get here?");
}
public boolean canAccessPool() {
return (level == Level.SILVER || level == Level.GOLD);
}
public boolean hasOwnLocker() {
return (level == Level.GOLD);
}
public double getEquipmentDiscount() {
switch (level) {
case BRONZE: return CustomerConstants.BRONZE_DISCOUNT;
case SILVER: return CustomerConstants.SILVER_DISCOUNT;
case GOLD: return CustomerConstants.GOLD_DISCOUNT;
}
throw new IllegalStateException("How did I get here?");
}

我是一个研究设计图案的新人,知道一些图案,比如观察图案、装饰图案和工厂图案。。。但是,老实说,我真的不知道如何识别和使用它们来改进代码。对于这个问题,我认为可以通过模板模式来改进代码,因为健身客户可以作为骨架。而铜牌、银牌、金牌可以作为健身客户的子类。我不确定这个问题是否正确。代码如下:

FitnessCustomer.class:

package ASS2_Q2;
public abstract class FitnessCustomer {
private Level level;
public final void setLevel(Level level){
this.level = level
}
public final double getFees(){
switch(level){
case BRONZE: return CustomerConstants.BRONZE_FEES;
case SILVER: return CustomerConstants.SILVER_FEES;
case GOLD: return CustomerConstants.GOLD_FEES; 
}
throw new IllegalStateException("How did I get here?");
}
public final double getEquipmentDiscount(){
switch(level){
case BRONZE: return CustomerConstants.BRONZE_DISCOUNT;
case SILVER: return CustomerConstants.SILVER_DISCOUNT;
case GOLD: return CustomerConstants.GOLD_DISCOUNT;
}
throw new IllegalStateException("How did I get here?");
}
public abstract  boolean canAccessPool();
public abstract boolean hasOwnLocker();

}

青铜等级:

package ASS2_Q2;
public class BRONZE extends FitnessCustomer{
public BRONZE(){
this.level = "BRONZE";
}

@Override
public boolean canAccessPool(){
return false;
}
@Override
public boolean hasOwnLocker(){
return false;
}

}

GOLD.等级:

package ASS2_Q2;
public class GOLD extends FitnessCustomer{
public GOLD(){
this.level = "GOLD";
}
@Override
public boolean canAccessPool(){
return true;
}
@Override
public boolean hasOwnLocker(){
return true;
}
}

SILVER.class:

package ASS2_Q2;
public class SILVER extends FitnessCustomer{
public SILVER(){
this.level = "SILVER";
}

@Override
public boolean canAccessPool(){
return true;
}
@Override
public boolean hasOwnLocker(){
return false;
}

}

我想问一下答案对不对请帮帮我!谢谢

我没有使用任何特定的设计模式,但我认为我们可以按照以下方式进行设计:

您可以有以下接口:

CustomerWithLockerAccess
CustomerWithPoolAccess
CustomerWithEquipmentDiscount

这种接口确保我们可以让客户拥有任何访问组合。

由于每个客户都有一个级别,并且他们必须支付费用,因此您可以创建一个抽象类FitnessCustomer,如下所示:

public abstract class FitnessCustomer {
private static final Level level;
public FitnessCustomer(Level level){
this.level = level
}
public Level getLevel(){ return this.level};
public final double getFees();
}

然后你可以按照以下方式设计你的类:

GoldCustomer extends FitnessCustomer implements CustomerWithLockerAccess, CustomerWithPoolAccess, CustomerWithEquipmentDiscount

SilverCustomer extends FitnessCustomer implements CustomerWithPoolAccess, CustomerWithEquipmentDiscount
BronzeCustomer extends FitnessCustomer implements CustomerWithEquipmentDiscount

最新更新