使用哪种设计模式重写这段代码?

huangapple go评论74阅读模式
英文:

Use which design pattern to rewrite the code?

问题

Sure, here's the translated content you provided:


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 可以作为骨架,而 BRONZE、SILVER、GOLD 可以作为 FitnessCustomer 的子类。我不太确定是否对问题的理解正确。下面是修改后的代码:

FitnessCustomer.java

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

BRONZE.java

package ASS2_Q2;

public class BRONZE extends FitnessCustomer {
    public BRONZE() {
        this.level = Level.BRONZE;
    }

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

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

GOLD.java

package ASS2_Q2;

public class GOLD extends FitnessCustomer {
    public GOLD() {
        this.level = Level.GOLD;
    }

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

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

SILVER.java

package ASS2_Q2;

public class SILVER extends FitnessCustomer {
    public SILVER() {
        this.level = Level.SILVER;
    }

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

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

我想问一下答案是否正确?请帮帮我,谢谢!

英文:

Q:For the following code snippets, identify which design pattern should have been used to
improve the quality of the code. Rewrite the code using design pattern that you’ve identified.
Your answer should include
i) the statement to describe the design pattern
ii) the rewritten
Java code, and
iii) the test result from the rewritten Java code.

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?");
 }

I'm an fresh man to study the design pattern, and know some patterns likes observe-pattern,decorate-pattern and factory pattern... But, honestly, I'm not really understand how to identify and use them to improve the codes. For the question, I think the code can be improved by the template pattern cause the fitness-customer can be as the skeleton. And BRONZE, SILVER, GOLD can be as the sub-classes for the fitness-customer. I'm not sure it's correct for the question. And the codes are below:

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();


}

BRONZE.class:

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.class:

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



}

I want to ask whether the answer is right or not?Please help me! Thanks!

答案1

得分: 3

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

你可以有以下接口:

    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
英文:

I am not using any particular design pattern but I think we could design it in following manner:

You can have following interfaces:

CustomerWithLockerAccess
CustomerWithPoolAccess
CustomerWithEquipmentDiscount

This interfaces ensures that we can have customer with any combinations of access.

Since every customer has a level and they have to pay a fee, You can create an abstract class FitnessCustomer as follow:

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

Then you can design your classes as follows:

GoldCustomer extends FitnessCustomer implements CustomerWithLockerAccess, CustomerWithPoolAccess, CustomerWithEquipmentDiscount


SilverCustomer extends FitnessCustomer implements CustomerWithPoolAccess, CustomerWithEquipmentDiscount

BronzeCustomer extends FitnessCustomer implements CustomerWithEquipmentDiscount

huangapple
  • 本文由 发表于 2020年4月4日 18:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61026462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定