【问题标题】:Use which design pattern to rewrite the code?使用哪种设计模式来重写代码?
【发布时间】:2020-04-04 10:01:43
【问题描述】:

Q:对于下面的代码sn-ps,确定应该使用哪种设计模式 提高代码质量。使用您确定的设计模式重写代码。 你的答案应该包括 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 可以作为健身客户的子类。我不确定这个问题是否正确。代码如下:

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


}

黄金级:

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



}

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

【问题讨论】:

    标签: java design-patterns


    【解决方案1】:

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

    您可以有以下接口:

    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
    

    【讨论】:

    • 想!好主意。也许,我们可以使用策略模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多