所以我的任务是完成Mancala游戏的GUI部分。我需要使用策略模式,以便用户可以在两种类型的电路板设计中进行选择,以便以后可以添加更多电路板。看一些教程,我设计了以下裸骨架:
Board.java接口
public interface Board {
String board();
}
class Classic implements Board{
@Override
public String board() {
return "Classic";
}
}
class Zelda implements Board{
@Override
public String board() {
return "Zelda";
}
}
MancalaBoard.java
public class MancalaBoard extends JComponent {
private static final int DEFAULT_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; //sets the width to the users screen dimension
private static final int DEFAULT_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; //sets the height to the users screen dimension
private static final int BOARD_WIDTH = DEFAULT_WIDTH - 100;
private static final int BOARD_HEIGHT = DEFAULT_HEIGHT - 100;
private static int pits;
private int stones;
public Board boardType;
private JButton undoButton;
public MancalaBoard(int stones){
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("Mancala Game");
frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
frame.setVisible(true);
pits = 14;
this.stones = stones; // user specified amount, no greater than 4
}
public String selectBoard(){
return boardType.board();
}
public void setBoardType(Board newBoardType){
boardType = newBoardType;
}
}
ClassicBoard.java
public class ClassicBoard extends MancalaBoard {
public ClassicBoard(){
super(4);
boardType = new Classic();
}
public Double boardShape(){
return new Rectangle2D.Double();
}
ZeldaBoard.java(是的,我是#nerdy)
public class ZeldaBoard extends MancalaBoard {
public ZeldaBoard(){
super(4);
boardType = new Zelda();
}
//board shape will be an ellipse
}
MancalaTester.java
public class MancalaTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
MancalaBoard myMancala = new MancalaBoard(4);
myMancala.setBoardType(new Classic());
System.out.println("myMancala: " + myMancala.selectBoard());
}
}
所以我能够在我的测试人员中使用多态来获得正确的字符串来打印,这很好,但我遇到了障碍。要么我有一个大脑屁或我不完全理解战略模式,但这是我的问题:我如何确定哪个班负责什么?我的意思是,它们都具有相同数量的凹坑,它们都具有"撤消按钮"但是美学设计将是不同的。我在哪里指定这些更改?例如,在它的核心,我的ClassicBoard将是一个矩形,我的ZeldaBoard将是一个椭圆。我是在Board.java类中编写的,还是分别在ClassicBoard和ZeldaBoard中编写它。或者它是MancalaBoard.java的因素,并根据用户选择的经典板或Zelda板决定吗?
抱歉咆哮,但我希望这是有道理的。我只是需要有人指出我正确的方向,哪个班级负责什么。