Декораторы Java — написание классов в качестве декораторов ⇐ JAVA
-
Anonymous
Декораторы Java — написание классов в качестве декораторов
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[0] = matrix[1]; res[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[j] = matrix[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?
Источник: https://stackoverflow.com/questions/780 ... decorators
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[0] = matrix[1]; res[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[j] = matrix[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?
Источник: https://stackoverflow.com/questions/780 ... decorators
Мобильная версия