具有所有参数组合的继承体系结构

时间:2016-07-25 20:36:02

标签: java inheritance architecture combinations

我对架构/功能有疑问:

让我们说对于给定的问题我有一个解决问题的算法。它可以使用一定量的信息,由包含泛型类型的一些实例变量的对象表示。事实上,有n个变量是相关的,但算法可以解决未知子集的问题。为了找到该子集,我希望能够将每个可能的变量组合传递给算法。所以我想要一个允许我尽可能少地在信息类型之间切换的结构。

当只有当前调查的变量子集相等时,特别是两个信息对象应该相等。所以我似乎需要覆盖等于。这意味着为每个可能的子集编写一个类,导致总共2 ^ n个类。

有更优雅的方法吗?如果它必须是2 ^ n类,你将如何设计继承?

回应Andy Turners和mellowmaroons评论:我认为求解器是函数f:S - &gt; {0,1}告诉我给定状态s的正确动作(0或1)。状态s由长度为k的向量给出。为了保持简单,假设S = Z ^ k,例如,整数。但是,用未知的变量子集构建状态室可能就足够了,导致S = Z ^ l,l <1。 ķ。为了测试可能的子集,我需要让算法分别运行相应状态室的对象。因此,在此过程中,将生成状态室的对象并将其传递给求解器对象。解算器应该能够接受它们并且他必须能够识别已经存在的状态。 一般问到,这个结构的方法是什么?

这是我当前方法的一个例子

//abstract superclass to wrap the information types

   public abstract class Information {

   }

// concrete class that contains 1st and 2nd parameters

    public class InformationA extends Information{

        int size;
        int weight;

    // two InformationA objects are equal if only 1st and 2nd parameter are equal

    public boolean equals(Object o){
        if(o instanceof InformationA){
           InformationA info = (InformationA) o;
           return info.size == size && info.weight == weight;
        }
        return false
    }

// concrete class that contains 2nd and 3rd Parameters

    public class InformationB extends Information {

       int weight;
       int color;

       //another equals method with the same structure than before

   }

import java.util.HashMap;
import java.util.Random;

// the solver class contains the solve function
// It associates an action with every possible status/information

public class Solver {

    HashMap<Information, Boolean> solveFunction;

    // If a status/information has been treated already return the value
    // else generate a random value

    public Boolean getAction(Information i){
        if(solveFunction.containsKey(i)) return solveFunction.get(i);
        solveFunction.put(i, new Random().nextInt(2) == 0);
        return solveFunction.get(i);
    }

    public void solve(Problem problem){
//      while(!problem.isSolved()){
//##        somehow extract only the needed status Parameters 
//          pass them to getAction method and invoke the response on the problem
//      }
    }

}

public class Problem {

//    public boolean isSolved(){
//        return some signal
//    }

//   public void process(Boolean action){
//        the process develops according to the action and reaches a new state   
//      }

//## public getStatus(){
//       return the status in a way that the solver can extract the needed subset of information
//   }

}

public class Controller {

//  generate the problem and the solver
//##somehow tell the solver which Information type to use
//  Let the solver solve the problem    

    public static void main(String[] args){
        Problem problem = new Problem();
        new Solver().solve(problem);
    }

}

很明显,这种方法会导致生成所需子集并将其传递给求解器的问题。这可能是可以解决的。但是仍然存在使用不同参数组合编写所有信息类的问题。

1 个答案:

答案 0 :(得分:0)

那个问题很老了。我认为我在解决它方面取得了一些进展,所以我发布了我的方法,希望得到一些反馈或改进。

为了在不同的参数组合之间切换,我尝试使用超类“信息”的子类。相反,我现在使用HashMap表示Information类的一个实例,而HashMaps的HashMap表示将Action分配给每个Information实例的函数f。

// the class that represents first parameter

public class A{
     //can be a selfmade class or a String or some generic type
}

// same for parameters B,C,D,E...

import java.util.HashMap;
import java.util.Random;

// the solver class fills the solveFunction HashMap
// It associates an action with every possible status/information

public class Solver {

    HashMap<HashMap<String,Object>, Boolean> solveFunction;

    // If a status/information has been treated already return the value
    // else generate a random value

    public Boolean getAction(HashMap<String, Object> info){
        if(solveFunction.containsKey(info)) return solveFunction.get(info);
        solveFunction.put(info, new Random().nextInt(2) == 0);
        return solveFunction.get(info);
    }

    public HashMap<HashMap<String, Object>, Boolean> solve(Problem problem, HashMap<HashMap<String, Object>, Boolean> solveFunction){
        this.solveFunction = solveFunction;
        while(!problem.isSolved()){
            problem.process(getAction(problem.getStatus()));
        }
    }

}

public class Problem {

String[] usedParameters;

public Problem(String[] usedParameters){
    this.usedParameters = usedParameters;
}

    public boolean isSolved(){
//        return a signal
    }

   public void process(Boolean action){
//        the process develops according to the action and reaches a new state   
      }

 public getStatus(){
    HashMap<String, Object> status = new HashMap();
    for(String s : usedParameters){
        switch(s){
            case "A": status.add("A", new Object());
            case "B": status.add("B", new Object());
            case "C": status.add("C", new Object());
// and so on
        }
    }
    return status;
}
}

public class Controller {

//  generate the problem and the solver
//  Let the solver solve the problem    

    public static void main(String[] args){
        String[] usedParameters = {"A","C"};
        HashMap<HashMap<String, Object>, Boolean> solveFunction = new HashMap();
        Problem problem = new Problem(usedParameters);
        new Solver().solve(problem, solveFunction);
    }

}

上述代码当然不起作用,但应该只提出想法。 必须捕获随机生成的求解函数失败的情况。我认为其余的代码无法在不失一般性的情况下完成。