Created
October 16, 2022 12:33
-
-
Save selvamani-k-8445/a0ee8dc6006fe518542e994479336857 to your computer and use it in GitHub Desktop.
Flyweight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package FlyWeight; | |
public class SpreadSheet { | |
private final int MAX_ROWS = 3; | |
private final int MAX_COLS = 3; | |
private CellPropertyFactory factory = new CellPropertyFactory(); | |
// In a real app, these values should not be hardcoded here. | |
// They should be read from a configuration file. | |
private final String fontFamily = "Times New Roman"; | |
private final int fontSize = 12; | |
private final boolean isBold = false; | |
private Cell[][] cells = new Cell[MAX_ROWS][MAX_COLS]; | |
public SpreadSheet() { | |
generateCells(); | |
} | |
public void setContent(int row, int col, String content) { | |
ensureCellExists(row, col); | |
cells[row][col].setContent(content); | |
} | |
public void setFontFamily(int row, int col, String fontFamily) { | |
ensureCellExists(row, col); | |
Cell cell = cells[row][col]; | |
CellProperty property = factory.getDefaultCellProperty(fontFamily,fontSize,isBold); | |
cell.setProperties(property); | |
} | |
private void ensureCellExists(int row, int col) { | |
if (row < 0 || row >= MAX_ROWS) | |
throw new IllegalArgumentException(); | |
if (col < 0 || col >= MAX_COLS) | |
throw new IllegalArgumentException(); | |
} | |
private void generateCells() { | |
for (int row = 0; row < MAX_ROWS; row++) | |
for (int col = 0; col < MAX_COLS; col++) { | |
CellProperty property = factory.getDefaultCellProperty(fontFamily,fontSize,isBold); | |
Cell cell = new Cell(row, col,property); | |
cells[row][col] = cell; | |
} | |
} | |
public void render() { | |
for (int row = 0; row < MAX_ROWS; row++) | |
for (int col = 0; col < MAX_COLS; col++) | |
cells[row][col].render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment