对象关系设计
我只是碰巧思考了一个面向对象的概念,这可能听起来很微不足道,但我不知道为什么我觉得它很令人困惑。
无论如何,我在想,例如,如果我有一个动物类和一个位置类。而且我在任何时间只允许一只动物出现在一个地点。所以这有点像一对一的关系。同时,我希望 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
}
由此,我预见到两个问题。
首先,因为我的位置不知道动物是否已经在其上,所以许多动物都可以指向地图数组上的同一位置。这将违反我想要的 1-1 多重性约束。就我而言,我让动物拥有该位置。这可能就是发生这种情况的原因。如果我让位置拥有动物,这个问题就可以解决。但在某种情况下,如果我想知道我的动物在哪里,我需要遍历整个地图才能找到我的动物的位置在哪里?或者,我可以保留双向引用,但这会导致类高度耦合。
我认为可能存在问题的第二个问题是 Animal 类的设计。我有一个静态 newAnimal() 方法来实例化新动物。我这样做是因为我认为允许调用者直接从构造函数创建新的 Animal 实例可能会允许超出范围的坐标输入。但我还是觉得这个设计很别扭。
我在示例中使用 Java 代码。我正在考虑类对象本身的设计,尚未涉及数据库。
任何改善我提出的两个问题的建议都可能很棒。 谢谢!
I just happen to ponder of a OO concept which may sound quite trivial but I don't know why I find it quite confusing.
Anyway, I am thinking for example, if I have an Animal class and a Location class. And I only allow one animal to be at one location at any time. So it is kind of like a 1-to-1 relationship. At the same time, I wish that the Animal and Location classes do not need some kind of bidirectional reference so that they are kept loosely coupled. If say I have this:
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
}
From this, I foresee 2 problems.
First, because my location does not know if an animal is already on it, many animals can all be pointing to a same location on the map array. This would violate the 1-1 multiplicity constraint that I wanted. In my case, I let the Animal to own the Location. this could the be reason why this could happen. If say I let the Location to own the Animal, this could be solved. But in a case if I want to know where my Animal is at, I need to loop through the entire map just to find where one of my Animal's location is? Alternatively, I can keep a bidirectional reference but this would cause the classes to be highly-coupled.
The second problem that I feel could be a problem is the design in the Animal class. I have a static newAnimal() method to instantiate new animals. I did it this way because I thought allowing the caller to create new Animal instance directly from the constructor may allow an out of range coordinate input. But I still find the design very awkward.
I am using Java codes in my example. And I am thinking the design within the class objects itself and is not involving the database yet.
Any suggestions to improve the two issues I raised could be great.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以上是与该问题相关的事实。
位置可以被视为一个矩阵。二维数组。
目前,我们假设一只动物只精确地持有一个单位(一个细胞),并且不比给定时间点的单位少或多。
这个二维数组是Animals(只能包含Animal对象),那么包含是真实的,没有两个可以占据相同的空间 - 它必须替换现有的一个来占据。
另外,动物的位置属性也应该更新。
或者,可以编写 LocationManagerClass 来存储和管理地图上的动物占用情况。但是否接近现实生活中的“占空”场景还有待进一步评估
Above are the facts associated to the problem.
Location can be considered as a matrix. A 2-D array.
For the time being we assume that one animal hold only a single unit (One cell) exactly and not any less or more than that at a given point of time.
This 2D array is of Animals (which can contain Animal objects only), then the containment is real, no two can occupy same space-it has to replace existing one to occupy.
Also, The animal's location property should be updated.
Alternatively, A LocationManagerClass can be written to store and manage Animals occupancy on map. But it is to be assed further if it is close to the real life scenaio of 'space occupancy'
我将有单独的接口+类来管理/维护关系,并具有添加新动物或将动物移动到其他位置的方法。通过这种方式,您可以轻松检查和维护前/后条件。另外,我会保持动物和位置不可变(在这种情况下)。
拥有接口和类将更容易对关系进行不同的实现:Map、DB、文件等。
I would have separate interface+class that manages/maintains the relation, and have methods to add new animal, or move animal to other location. In this way you can check and maintain your pre/post conditions easily. Also i would keep Animal and Location immutable (in this context).
Having interface and class will make it easier to do different implementations of the relations: Map, DB, file, etc.
为什么您希望 Animal 类包含 Location?
另一种方法是在 Animal 类中仅具有 Animal 属性。位置等级很好。第三个类称为“地图”,它将管理位置(在地图中)和每个位置存在的动物。
C++ 代码如下
:
//这里我没有使用Location类,而是使用x,y。你可以做出这样的小改变。
};
Why do you want the Animal class to contain Location?
An alternative would be to have only Animal properties in the Animal class. Location class is fine as it is. Have a third class called Map, that will manage the Locations(in the map) and the Animal present at each location.
A c++ code for this would be something like this:
};
//Here I haven't used the Location class, instead I'm using x,y. You can make that small change.
};
您可以使用类似于前面答案的内容,但使用另一个数据结构而不是二维数组。例如稀疏数组、有序列表或哈希表。这将提供更快的查找速度,但动物的插入或移动速度会更慢。而且它仍然可以强制执行任何地点不超过 1 只动物的要求。
You can use something like the previous answers but with another data structure instead of a 2D array. Such as a sparse array, ordered list or hash table. That will provide faster lookups but slower insertion or movement of animals. And it can still enforce the requirement of no more than 1 animal in any location.