【问题标题】:Class Designing in OOPS ( JAVA )OOPS 中的类设计(JAVA)
【发布时间】:2020-06-20 04:22:09
【问题描述】:
  1. 有什么方法可以从接口中移除 totalPermutationCount(),因为该方法的唯一目的是从具体类中获取 permutationCombination 值,如果有更多具体类或更多实例变量,那么接口将变得团块和大。
  2. 除了构造函数之外,是否还有其他最佳方法可以为整个类所依赖的实例变量赋值。

参考我的使用方法,请帮助我变得更好。

界面

interface Word {
  void performPermutation();
  int totalPermutationCount();
}

接口实现

class WordImpl implements Word{

//  "word" is to store the word from the user  
private String word;
// "convert" is to convert the word into a char array to perform the logic 
private char[] convert;
// "permutationCombination" is to find the total number of combination that is done 
private int permutationCombination = 0;

// Constructor
public WordImpl(String wordCom){
    setWord(wordCom);
}

//getter setter of word instance variable
public void setWord(String wordCom){
    if(!wordCom.isEmpty() && wordCom.trim().length() > 0){
    word = wordCom;
    }else{
        word = "Default";
    }
    convertToChar();
}

// convert the given word to char array
private void convertToChar(){
    convert = new char[word.length()];
    for(int i = 0 ; i < word.length() ; i++){  
    convert[i] = word.charAt(i);    
} 
}

public int totalPermutationCount(){
    return permutationCombination;
}

// -------------------------------- Below is the Temporary Logic Ignore it  ---------------------------------------------
private void performSwap(char[] list , int from, int to){
    char temp;
    temp = list[from];
    list[from] = list[to];
    list[to] = temp;
}

public void performPermutation(){
    char[] list = convert.clone();
    Set<String> listData = new HashSet<>();
    System.out.println(convert);
    for (int i = 0 ; i < word.length() ; i++){
        for (int j = i + 1 ,  reverse = i - 1 ; j < word.length() || reverse >= 0  ; j++ , reverse--){
            if(j < word.length()){
            performSwap(list,i,j);
            System.out.println(convertToString(list));
            list = convert;
            permutationCombination++;
            }
            if(reverse >= 0 && i != 0){
                performSwap(list,i,reverse);
                 System.out.println(convertToString(list));
                 list = convert;
                 permutationCombination++;
            }
        }
    }
}
 // ----------------------------------------------------------------------------------------
private String convertToString(char[] list){
    String value = "";
    for(int i = 0 ; i < word.length() ; i++){  
    value = value +  list[i];
     }  
    return value;
}}

主类

 public class MyClass {
    public static void main(String args[]) {
    Word wordImplReference = new WordImpl("home");
    wordImplReference.performPermutation();
    System.out.println(wordImplReference.totalPermutationCount());
    }
   }

【问题讨论】:

标签: java oop design-patterns class-design


【解决方案1】:

以下内容可以帮助您解决问题:

  1. 如果您从接口中删除 totalPermutationCount 方法,您将失去从接口类实现的抽象的好处。如果您仍然需要这样做,那么您将需要使用 WordImpl wordImplReference = new WordImpl("home");在你的主要方法中。

  2. 在这种情况下,通过构造函数赋值会更好。可以在 WordImpl 类中使用 Set 方法设置,但需要在 Interface 中添加 setWord 方法。

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2012-02-14
    • 2015-05-23
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多