java - 使用哪种设计模式重写代码?

标签 java design-patterns

问:对于以下代码片段,确定应该使用哪种设计模式 提高代码的质量。使用您确定的设计模式重写代码。 你的答案应该包括 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?");
 }

我是研究设计模式的新手,知道一些模式,如观察模式、装饰模式和工厂模式......但是,老实说,我不太了解如何识别和使用它们改进代码。对于这个问题,我认为可以通过模板模式改进代码,因为健身客户可以作为骨架。 BRONZE、SILVER、GOLD 可以作为健身客户的子类。我不确定这个问题是否正确。代码如下:

健身客户类:

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


}

黄金类:

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



}

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

最佳答案

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

您可以有以下接口(interface):

CustomerWithLockerAccess
CustomerWithPoolAccess
CustomerWithEquipmentDiscount

此接口(interface)确保我们可以让客户拥有任意组合的访问权限。

由于每个顾客都有等级并且他们必须支付费用,您可以创建一个抽象类 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

关于java - 使用哪种设计模式重写代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61026462/

相关文章:

java - Primefaces动态树

Java/安卓/ Kotlin : Reflection on private Field and call public methods on it

java - 如何在 JAVA FX 2.2 中重新绘制窗口(舞台)

c# - 为每个提供商实现电子邮件验证的正确设计模式是什么?

c++ - 选择正确的子类以编程方式实例化

java - 错误: Could not find or load main class error when compiling in eclipse and command prompt But works in intelliJIDEA

java - 将日期添加到 Java util 记录器 fileHandler 名称

java - 需要重新设计动态输入的现有逻辑

design-patterns - 可以用工厂代替单例吗?

JAVA FX - 从输入中获取大写字母的数量