简体   繁体   English

对两个对象进行建模的好方法是什么,但它们有两个不同的功能?

[英]What is a good way to model two objects that are the same thing, but have two different functionalities?

I am looking for a good way to model a 'move' in a board game. 我正在寻找一种模拟棋盘游戏中“移动”的好方法。 I want to be able to ask a Player what their move is and have them respond, then be able to perform the correct actions on the board based on their move. 我希望能够向玩家询问他们的举动是什么并让他们做出回应,然后能够根据他们的举动在棋盘上执行正确的动作。 The problem is, there are several types of moves, each with its own set of data. 问题是,有几种类型的移动,每种移动都有自己的数据集。 For example, in Scrabble, you can play a word, which should return the word, its position and direction (or a set of tiles with positions or whatever), but you can also swap tiles (which would involve returning the tiles you want to swap) or pass. 例如,在Scrabble中,你可以播放一个单词,它应该返回单词,它的位置和方向(或一组带有位置或任何东西的平铺),但你也可以交换平铺(这将涉及返回你想要的平铺交换)或通过。

Similarly in chess, you normally move a piece, which should return a move with the piece and where it goes, but you could also castle, which involves indicating two pieces and their positions, or a side (king/queen), or some other piece of information besides piece/position. 同样在国际象棋中,你通常移动一块,它应该返回一个移动与该块和它去的地方,但你也可以城堡,其中包括指示两个部分及其位置,或一侧(国王/王后),或其他除了件/位置之外的一条信息。

I'm assuming that a Player returning a Move object is the best way to go, but I'm open to any other modeling choices or suggestions as well. 我假设玩家返回Move对象是最好的方式,但我也可以接受任何其他建模选择或建议。

Thanks! 谢谢!

These may not be exactly what you are looking for, but there seems to be a lot of great ideas on this old post: 这些可能不是你想要的,但在这篇旧帖子上似乎有很多很棒的想法:

Any patterns for modelling board games? 用于建模棋盘游戏的任何模式?

Some of the answers include suggestions for Move implementations, and could hopefully give you some new ideas. 一些答案包括Move实现的建议,并希望能给你一些新的想法。

As a rule of thumb, if you want to have common type but different behavior for a thing - here a Move - you need to let the thing implement the behavior. 根据经验,如果你想拥有一个共同的类型但是对于一个东西有不同的行为 - 这里是一个Move - 你需要让这个东西实现这个行为。 So instead of asking how the board can interpret two different moves differently, you should consider how to have two different moves use the board differently when performing their move. 因此,不应询问电路板如何以不同方式解释两种不同的移动,而应考虑如何在执行移动时以不同方式使用电路板。

So: The base class (should probably be interface) is a Move, it has a perform() method, and the two kinds of moves have different implementations of the perform() method. 所以:基类(应该是接口)是一个Move,它有一个perform()方法,这两种移动有不同的perform()方法实现。

A good model is always based on the detailed knowledge of requirements, data flow, control flow, overall system architecture, and so on. 一个好的模型总是基于需求,数据流,控制流,整体系统架构等的详细知识。 It is not very fruitful to design an application by handwaving and philosophical argument. 通过手工和哲学论证来设计应用程序并不是很有成效。

Concerning your particular question, one of generally legitimate models is for the Player to return a Move , but depending on full details, its actual merit may range from a total success to a total disaster. 关于你的特定问题,一个普遍合法的模型是玩家返回一个Move ,但根据完整的细节,它的实际价值可能从总成功到总灾难。

A move in a game is not really an object. 在游戏中移动并不是真正的对象。 It's an event. 这是一个事件。

Your chess example is the easiest. 你的国际象棋示例是最简单的。 A move returns a piece and new position on the board, but it could also return a castle event, a capture event, and / or a piece promotion event. 移动返回棋盘上的棋子和新位置,但它也可以返回城堡事件,捕获事件和/或片段促销事件。

Similarly, in a Scrabble game, a move could return a word play event, a exchange tile event, or a pass event. 类似地,在Scrabble游戏中,移动可以返回文字游戏事件,交换区块事件或传递事件。

Think of an event as a message to the rest of the game. 将事件视为对游戏其余部分的信息。 You can code an event as you would a class. 您可以像对待课程一样对事件进行编码。 This event class would have to have access to the various game classes so that the model could create the message. 此事件类必须能够访问各种游戏类,以便模型可以创建消息。

An event handler class processes all of the game events. 事件处理程序类处理所有游戏事件。 This class would be the class that manipulates the other game classes. 这个类将是操纵其他游戏类的类。 The events would just have to all be in a format that the event handler could handle. 这些事件必须全部采用事件处理程序可以处理的格式。

If different implementations will have nothing in common other than how they present themselves to the outside world, which sort of comes across from your explanation, Move should be a loosely defined interface with each implementation across different games doing its own thing. 如果不同的实现没有任何共同点,除了它们如何呈现给外部世界,哪种类型来自你的解释,Move应该是一个松散定义的接口,每个实现在不同的游戏中做自己的事情。

But my main question is whether there really is a reason to have this interface known across different games. 但我的主要问题是,是否真的有理由在不同的游戏中使用这个界面。 If there is not a shared handler of some sort, which expects certain action formats across different games, I would not bother and simply define different game-specific Move classes. 如果没有某种类型的共享处理程序,它需要在不同游戏中使用某些动作格式,我就不会费心并简单地定义不同的游戏特定的Move类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法让两个不同的对象有一些相同的方法? - Is there a way to two different Objects have some same Methods? 如果两个相同的对象具有不同的哈希码,那么缺点是什么 - What is the drawback if two same objects have different hashcode 如果两个不同的对象具有相同的哈希码,会发生什么? - What happens if two different objects have the same hashcode? 在什么条件下两个不同的对象可能具有相同的hashcode()值..? - Under what Conditions two different objects may have same hashcode() value..? 使构建器返回具有相同字段的两个不同POJO的对象的最佳方法是什么? - What is the best way to make a builder return objects of two different POJOs having same fields 我应该在一个类中有两个不同的变量来代表几乎同一件事吗 - Should I have two different variables in one class that represent almost the same thing 同一对象的两个列表具有不同的哈希码 - Two lists of same objects have different hash codes 如果两个对象具有相同的哈希码,那么它们的内存地址呢? - if two objects have same hashcode then what about their memory addresses 解析Java中两个不同模式的好方法 - good way to parse two different patterns in java 当两个不同的注释具有相同的名称时会发生什么? - What happens when two different annotations have the same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM