1

「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.populateCells を逐語的に書き換えずに Matrix と Cell を拡張する方法を理解できないようです。つまり、Matrix での参照をすべて壊すことなく、Cell を拡張したいと考えています。

では、基本的に、抽象化が本来行うべきこと、つまり、重複コードの制限、カプセル化などを行うように、これをどのように整理すればよいでしょうか?


選択した回答の実装を要約すると:

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

3 に答える 3

2

これを処理するには多くの方法があります。


1 つのマトリックスの実装、1 つのセルの実装

私の意見では、Cell クラスに「カスタマイズ可能な」プロパティが 1 つしかない場合、最も望ましくないのは、そのプロパティを Matrix クラスのコンストラクターに渡して、population 呼び出し中に使用することです。

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 クラスにプラグインできるプラグイン可能な戦略的動作と見なすことです。このメソッドは、継承よりも構成優先の原則の実装と見なすことができます。

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) {
        // ...
    }
}

これは、オブジェクトの複雑さに合わせて非常にうまくスケーリングできるため、使用する優れた方法です (特に、複数のカプセル化されたオブジェクトの動作をオーバーライドする場合)。

于 2012-11-19T17:43:15.033 に答える
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 で提案されているのと同じ戦略パターンで上記を使用することもできます。違いは、以下のコードでは 1 つだけ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();
}
于 2012-11-19T17:33:46.617 に答える
0

私が理解していることから、すべての派生クラスで動作するコンストラクタ public Cell(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;
}
}
于 2012-11-19T17:49:33.607 に答える