Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单态多态性_Java_Design Patterns_Design Principles - Fatal编程技术网

Java 单态多态性

Java 单态多态性,java,design-patterns,design-principles,Java,Design Patterns,Design Principles,假设棋盘上的单重子表示是由细胞的双二元数组组成的。有时,我希望这种表示被认为是行的板,有时作为CalMunn,有时作为网格,但是每一种情况都必须在板相同的底层单元上工作。 所以我有4个类要实现: class CheesBoard : singleton consist of just a stright dd array of cells class CheesBoardAsGrids : consist of that same cells but presented by grids cla

假设棋盘上的单重子表示是由细胞的双二元数组组成的。有时,我希望这种表示被认为是行的板,有时作为CalMunn,有时作为网格,但是每一种情况都必须在板

相同的底层单元上工作。 所以我有4个类要实现:

class CheesBoard : singleton consist of just a stright dd array of cells
class CheesBoardAsGrids : consist of that same cells but presented by grids
class CheesBoardAsRows : strigtforward as above but rows
class CheesBoardAsColumns : ... you already get it
我不知道他们之间应该有什么样的关系才能使它最具可读性和可重用性


例如:我不能从CheesBoard继承,因为它是单例的

单例,根据定义,是一个你只想实例化一次的对象

您可以在黑板上创建抽象类。类
CheesBoardAsGrids
CheesBoardAsRows
CheesBoardAsColumns
将从此类继承。 CheesBoard类应该包含Board变量

如果希望使用一些数据初始化单例,可以在getInstance方法之后使用数据加载它

CheesBoard类应该包含init方法。此方法将获取Board参数并初始化Board变量:

Board board = new CheesBoardAsGrids();
CheesBoard singleton = CheesBoard.getInstance();
singleton.init(board);

为什么国际象棋棋盘是单人的?这似乎是个奇怪的选择。对于你的问题,给你的类一个棋盘的实例,然后实现处理该实例的方法。我提出了这个单件,因为我试图尽可能接近现实,换句话说,我只想在整个程序生命周期中有一次董事会检查。你应该停下来问问自己,你的数据的4种不同表示是否可以保证4种不同的类别。我会说:可能不会。毕竟,它还是一样的东西:棋盘。相反,为什么不让1个类和4个多方法来满足您的需求呢?