使用具有内部或“依赖”类的抽象或接口

时间:2012-11-19 17:13:05

标签: java interface matrix abstract

我发现自己反复制作一个带有“Cell”对象的2D数组的“Matrix”类(我的标题中的“依赖”类,不确定是否有一个标准术语,一个外部的类,但只能从调用其构造函数和方法的类访问)。所以当然,我希望为自己构建一些可重用的类和/或接口。

矩阵看起来像这样:

public class Matrix {

private Cell matrix[][];
private float spaceWidth, spaceHeight;
private int xCols, yRows;

public Matrix(int xCols, int yRows, float width, float height){
    matrix = populateCells(xCols,yRows);
    spaceWidth = width / xCols;
    spaceHeight = height / yRows;
    this.xCols = xCols;
    this.yRows = yRows;
}

public Cell[][] populateCells(int xCols, int yRows) {
    Cell c[][] = new Cell[xCols][yRows];
    int k = 0;
    for (int i = 0; i < xCols; i++){
        for(int j = 0; j < yRows; j++){
            c[i][j] = new Cell(i,j,k);
            k++;
        }
    }
    return c;
}
...
}

public class Cell {
private int x, y, num;

public Cell(int x, int y, int num, String name){
    this.x = x;
    this.y = y;
    this.num = num;
}

public float getX(){return x;}
public float getY(){return y;}
public int getNum(){return num;}
}

我希望每个Cell和每个Matrix都具有这些属性。当我想扩展Cell以添加属性时,问题就来了。无论我尝试什么界面和/或抽象的组合,我似乎无法弄清楚如何扩展Matrix和Cell而不用重写Matrix.populateCells,只需要新的Cell名称。换句话说,我想扩展Cell而不会破坏Matrix中对它的每一个引用。

基本上,我如何组织这个以便抽象做它应该做的事情,即限制重复代码,封装等等?

<小时/> 只是总结一下我对所选答案的实现:

public interface ICell {

public abstract int getX();
public abstract int getY();
public abstract int getNum();

}

public class Cell implements ICell {
private int x, y, num;

public Cell(int x, int y, int num){
    this.x = x;
    this.y = y;
    this.num = num;
}

    //getters...
}

public class WaveCell extends Cell implements ICell {
private boolean marked;

public WaveCell(int x, int y, int num) {
    super(x, y, num);
    marked = false;
}

public boolean isMarked() {return marked;}
public void setMarked(boolean marked) {this.marked = marked;}

    // More new stuff...    
}

public class WaveMatrix extends Matrix {

public WaveMatrix(int xCols, int yRows, float width, float height) {
    super(xCols, yRows, width, height);
}

@Override
public ICell newCell(int i, int j, int k){
    ICell c = new WaveCell(i,j,k);
    return c;
}
    ...
 }

 public class Matrix {

private ICell matrix[][];
private float spaceWidth, spaceHeight;
int xCols, yRows;

public Matrix(int xCols, int yRows, float width, float height){
    matrix = populateCells(xCols,yRows);
    spaceWidth = width / xCols;
    spaceHeight = height / yRows;
    this.xCols = xCols;
    this.yRows = yRows;
}

public ICell[][] populateCells(int xCols, int yRows) {
    ICell c[][] = new ICell[xCols][yRows];
    int k = 0;
    for (int i = 0; i < xCols; i++){
        for(int j = 0; j < yRows; j++){
            c[i][j] = newCell(i,j,k);
            k++;
        }
    }
    return c;
}

public ICell newCell(int i, int j, int k){
    ICell c = new Cell(i,j,k);
    return c;
}
    ...
}

3 个答案:

答案 0 :(得分:2)

有很多方法可以解决这个问题。


一个矩阵实施,一个单元实施

在我看来,最不可取的是,如果你只在Cell类中拥有一个'customizable'属性,只需将属性传递给Matrix类的构造函数,以便在填充调用期间使用。

public Matrix(String cellName, int xCols, int yRows, float width, float height){
    matrix = populateCells(cellName, xCols,yRows);
    // ...
}

public Cell[][] populateCells(String cellName, int xCols, int yRows) {
    // ...
}

这种方法几乎不算作面向对象的编程,并且很难维护您为Cell类识别的更“可定制”的行为。不推荐。


多个矩阵实施

或者,您可以创建Matrix类的层次结构,其中包含一个包含核心行为的抽象超类,以及能够更恰当地填充其自己的单元格类型的专用子类。

public abstract class Matrix<T extends Cell> {

    private public T matrix[][];

    public Matrix(int xCols, int yRows, float width, float height){
        matrix = populateCells(xCols,yRows);
        // ...
    }

    public T[][] populateCells(int xCols, int yRows) ;
}

public class RedBloodCellMatrix extends Matrix<RedBloodCell> {

    @Override
    public RedBloodCell[][] populateCells(int xCols, int yRows) {
        // ...
    }
}

这种方法没有错,它是一种经典的多重继承策略。


另一种选择,在我看来更可取的是,将单元格数量视为可插入的 策略 行为,您可以将其插入Matrix类在运行时。可以将此方法视为composition-over-inheritance原则的实现:

public class Matrix {

    private public Cell matrix[][];
    private CellPopulationStrategy cellPopulationStrategy;
    // ...

    public Matrix(CellPopulationStrategy strategy, int xCols, int yRows, float width, float height){
        matrix = populateCells(strategy, xCols,yRows);
        // ...
    }

    public Cell[][] populateCells(CellPopulationStrategy strategy, int xCols, int yRows) {
        return strategy.populateCells(xCols, yRows);
    }
}

public interface CellPopulationStrategy {
    public Cell[][] populateCells(int xCols, int yRows);
}

public RedBloodCellPopulationStrategy implements CellPopulationStrategy{

    @Override
    public Cell[][] populateCells(int xCols, int yRows) {
        // ...
    }
}

这是一种很好的使用方法,因为它可以很好地扩展对象的复杂性(特别是当你想要覆盖多个封装对象的行为时。

答案 1 :(得分:1)

  

并不是我不想改变populateCells,但我不想调用super并循环遍历数组,然后在子进程中再次执行。

如果您希望能够以Matrix中可以执行额外步骤的方式扩展populateCells,则可以将其中的一部分移动到可以覆盖的方法中。比如下面的代码

public class Matrix {

    public Cell[][] populateCells(int xCols, int yRows) {
        Cell c[][] = new Cell[xCols][yRows];
        int k = 0;
        for (int i = 0; i < xCols; i++){
            for(int j = 0; j < yRows; j++){
                c[i][j] = getCell(i,j,k);
                k++;
            }
        }
        return c;
    }

    public Cell getCell(int i, int j, int k) {
        return new Cell(i, j, k);
    }

}


class SuperMatrix extends Matrix {
    @Override
    public Cell getCell(int i, int j, int k) {
        Cell c = super.getCell(i, j, k);
        modify(c);
        return c;
    }
}

您也可以使用@Perception建议的相同策略模式。不同之处在于,在下面的代码中只生成一个ICell,而在另一个示例中,完整的单元格数组将是产品。

public class Matrix {
    /** a common cell interface */
    public interface ICell {
        public int getX();
        public int getY();
    }

    /** somthing that knows how to produce a cell */
    public interface CellProducerStrategy {
        public ICell getCell(int i, int j, int k);
    }

    /** method of Matrix that is always the same for all Cells and Strategies */
    public ICell[][] populateCells(int xCols, int yRows, CellProducerStrategy strategy) {
        ICell c[][] = new ICell[xCols][yRows];
        int k = 0;
        for (int i = 0; i < xCols; i++){
            for(int j = 0; j < yRows; j++){
                c[i][j] = strategy.getCell(i,j,k);
                k++;
            }
        }
        return c;
    }

    // ---------- some implementation --------------------

    static class DefaultCell implements ICell {
        public DefaultCell(int i, int j, int k) {
            // something
        }
        @Override
        public int getX() {
            return 0;
        }
        @Override
        public int getY() {
            return 0;
        }
    }

    public static class DefaultCellProducer implements CellProducerStrategy {
        @Override
        public ICell getCell(int i, int j, int k) {
            return new DefaultCell(i, j, k);
        }
    }

    // ---------- alternative implementation --------------------

    static class NamedCell implements ICell {
        private String name;
        public NamedCell(int i, int j, int k, String name) {
            // something
            this.name = name;
        }
        @Override
        public int getX() {
            return 0;
        }
        @Override
        public int getY() {
            return 0;
        }
        public String getName() {
            return this.name;
        }
    }

    public static class NamedCellProducer implements CellProducerStrategy {
        @Override
        public ICell getCell(int i, int j, int k) {
            return new NamedCell(i, j, k, "John-" + i*j*k);
        }
    }
}

上面的代码不使用泛型,如果你想使用未在基类中定义的方法,那么你需要将项目转换为它们的实际类型。

Matrix m = new Matrix(5, 5, new Matrix.NamedCellProducer());
ICell cell = m.get(0,0);
if (cell instanceof NamedCell) {
    String name = ((NamedCell)cell).getName();
}

答案 2 :(得分:0)

根据我的理解,你需要使构造函数public(int x,int y,int num)在所有派生类中工作。请参阅下面的示例 - 它基于受保护的init方法。

public class Cell {
protected int x, y, num;

public Cell() {
}

public Cell(int x, int y, int num) {
    init(x, y, num);
}

protected void init(int x, int y, int num) {
    this.x = x;
    this.y = y;
    this.num = num;
}

public float getX() {
    return x;
}

public float getY() {
    return y;
}

public int getNum() {
    return num;
}
}

public class CellWithName extends Cell {
protected String name;

public CellWithName(int x, int y, int num, String name) {
    init(x, y, num);
    this.name = name;
}
}