I am trying to create a minesweeper game with design patterns, but i have trouble implementing a creational pattern for the tiles in minesweeper.
in minesweeper there are 2 types of tiles, the number tile and mines tile. the number tile contains a number or a blank depending on the amount of mines tile beside them. while the mines tile contain a mine img to load on the tile.
How i approached this is by trying to use a Factory Method for the tiles, here is an example in java.
The abstract class
public abstract class Tile {
boolean flag = false;
boolean opened = false;
abstract void open();
void flag(){
// business logic
}
boolean checkFlagged(){
return flag;
}
boolean checkOpened(){
return opened;
}
}
The NumberTile class
public class NumberTile extends Tile {
int number;
@Override
void open(){
// business logic
}
void setNumber(int number){
this.number = number;
}
int getNumber(){
return number;
}
}
The MinesTile class
public class MinesTile extends Tile {
ImageIcon mineImg;
@Override
void open(){
// business logic
explode();
}
void explode(){
// tell main board to end game
}
}
Client Code
Tile numberTile = new NumberTile(); // get and setNumber method will not be accesible
Tile minesTile = new MinesTile(); // explode method will not be accesible
how do i use the same abstract Tile data type without having the child class methods inaccesible?
if i would abstract all the methods from NumberTile and MinesTile into the abstract class then they would also have to override methods that won't be used and cause refused bequest.
how do i solve this design pattern problem?
Aucun commentaire:
Enregistrer un commentaire