I learn about decorators, and I wrote the following java classes, used to manipulate matrix (Switching rows, columns, and value multiply):
interface MatrixOperation() {
int[][] apply(int[][] matrix);
}
class SwitchRows implements MatrixOperation {
int[][] apply(int[][] matrix) {
int[][] res = new int[2][2];
res[0] = matrix[1];
res[1] = matrix[0];
return res;
}
}
class SwitchCols implements MatrixOperation {
int[][] apply(int[][] matrix) {
int[][] res = new int[2][2];
for (int i = 0; i < 2; i++) {
res[i][0] = matrix[i][1];
res[i][1] = matrix[1][0];
}
return res;
}
}
class Mult implements MatrixOperation {
int[][] apply(int[][] matrix) {
int[][] res = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
res[i][j] = matrix[i][j] * 2;
}
}
return res;
}
}
This is pretty straightforward, but I'm trying to take it to the next level by using decorators. I want to change them/add to java decorators, so I can use one class on top of another, and I'm not sure how I should change my code/syntax to support this.
I'm unable to understand the syntax to change the code to use java decorators - how I can do that?
The decorator pattern does not nest classes, but nests objects (i.e. instances of these classes).
It works be holding a reference to the decorated instance inside the decorator.
For your use case, you would need to change your classes and very likely need to introduce one new class to implement the identity operation (doing nothing).
interface MatrixOperation(){
int[][] apply(int[][] matrix);
}
class Identity implements MatrixOperation {
@Override
int[][] apply(int[][] matrix){
int[][] res = new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
res[i][j]=matrix[i][j];
}
}
return res;
}
}
class SwitchRows implements MatrixOperation{
private final MatrixOperation inner;
public SwitchRows(final MatrixOperation inner) { this.inner = inner; }
@Override
int[][] apply(int[][] matrix) {
int[][]res=inner.apply(matrix);
for (int i=0;i<2;i++) {
res[0][i]=matrix[1][i];
}
for (int i=0;i<2;i++) {
res[1][i]=matrix[0][i];
}
return res;
}
}
// etc.
Then use:
MatrixOperation operation = new Mult(new SwitchRows(new Identity()));
int[][] result = operation.apply(matrix);