3

我只是碰巧想到了一个听起来很微不足道的 OO 概念,但我不知道为什么我觉得它很混乱。

无论如何,我正在考虑例如,如果我有一个 Animal 类和一个 Location 类。而且我只允许一只动物在任何时候出现在一个位置。所以这有点像一对一的关系。同时,我希望 Animal 和 Location 类不需要某种双向引用,以便它们保持松散耦合。如果说我有这个:

class Animal {
   private Location loc;

   public Animal(int x, int y) {
      loc = new Location(x,y);
   }

   public static newAnimal(Location[][] map, int x, int y) {
      if(map[x][y] != null) {
         return new Animal(x, y);
      } else return null;
}

class Location extends Point {
   public Location(int x, int y) {
      super(x, y);
   }
}

public static void main(String[] args) {
   //populates a map
   Location[][] map = new Location[10][10];
   for(int x=0; x<10; x++) {
      for(int y=0; y<10; y++) {
         map[x][y] = new Location(x, y);
      }
   }

   Animal dog = new Animal(2, 4);    //dog is at location 2,4
   Animal cat = new Animal(5, 6);    //cat is at location 5,6

   //But this does not restrict a constraint requirement that there should only be one animal at any one point
   Animal horse = new Animal(2, 4);    //now, horse is at the same location as dog but we only wanted one location to have one animal

   Animal rabbit = Animal.newAnimal(map, 20, 50);    //rabbit is null because it is out of the map size
}

由此,我预见到2个问题。

首先,因为我的位置不知道上面是否已经有动物,所以许多动物都可以指向地图阵列上的同一个位置。这将违反我想要的 1-1 多重性约束。就我而言,我让 Animal 拥有 Location。这可能是发生这种情况的原因。如果说我让位置拥有动物,这可以解决。但是如果我想知道我的动物在哪里,我需要遍历整个地图才能找到我的动物的位置之一?或者,我可以保留双向引用,但这会导致类高度耦合。

我觉得可能是一个问题的第二个问题是 Animal 类的设计。我有一个静态 newAnimal() 方法来实例化新动物。我这样做是因为我认为允许调用者直接从构造函数创建新的 Animal 实例可能会允许超出范围的坐标输入。但我还是觉得这个设计很尴尬。

我在我的示例中使用 Java 代码。而且我正在考虑类对象本身的设计,并且还没有涉及数据库。

任何改善我提出的两个问题的建议都可能很棒。谢谢!

4

4 回答 4

2
1.Location/Map in our case is a real world object and has boundaries.
2.Map can not hold more than one animal at any pont
3.An animal can not occupy more than one location

以上是与问题相关的事实。

位置可以被认为是一个矩阵。一个二维数组。暂时我们假设一只动物只精确地持有一个单位(一个细胞),并且在给定的时间点不会少于或多于。

这个二维数组是动物(只能包含动物对象),那么包含是真实的,没有两个可以占据相同的空间 - 它必须替换现有的一个才能占据。

此外,应更新动物的位置属性。

或者,可以编写一个LocationManagerClass来存储和管理地图上的动物占用。但如果它接近“空间占用”的现实生活场景,还有待进一步评估

于 2011-06-03T11:58:37.017 回答
1

我将有单独的接口+类来管理/维护关系,并有添加新动物或将动物移动到其他位置的方法。通过这种方式,您可以轻松检查和维护您的前/后条件。另外我会保持 Animal 和 Location 不可变(在这种情况下)。拥有接口和类将使关系的不同实现变得更容易:映射、数据库、文件等。

于 2011-06-03T04:55:15.707 回答
0

为什么要让 Animal 类包含 Location?

另一种方法是在 Animal 类中只包含 Animal 属性。位置类很好。有一个名为 Map 的第三个类,它将管理位置(在地图中)和每个位置存在的动物。

用于此的 c++ 代码将是这样的:

class Animal
{
    public:

            Animal(char *name);
            ~Animal();

            char *getName();
    private:

            char *name;

};

//这里我没有使用Location类,而是使用x,y。你可以做出那个小小的改变。

class Map
{

    private:

        Animal *animalLocation[10][10];


    public:
        //Note this function will check if any Animal exists at the specified (x,y). 
        void addAnimal(int x, int y, void *animal);
        void* getAnimal(int x, int y);

        Map();
        ~Map();

};

于 2011-06-04T11:45:40.643 回答
0

您可以使用与前面的答案类似的东西,但使用另一种数据结构而不是二维数组。例如稀疏数组、有序列表或哈希表。这将提供更快的查找但更慢的动物插入或移动。它仍然可以强制要求在任何位置不超过 1 只动物。

于 2011-06-04T15:41:52.400 回答