如何创建两个Java类的接口只读一个,一个读写? [英] How to create two interfaces to a Java class one read-only, one read-write?

查看:144
本文介绍了如何创建两个Java类的接口只读一个,一个读写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个游戏引擎用于双人纸牌游戏,我的学生将为其编写AI玩家。

I'm writing a game engine in Java for a two player card game for which my students are going to write AI players.

AI玩家将轮流在他们面前的桌子的一部分上打牌。他们可以用自己场上的牌攻击另一个玩家场上的牌。卡片可能正面朝上或面朝下。

The AI players will take turns playing cards onto their 'field' a part of the 'table' in front of them. They can attack with a card from their field a card in the other player's field. Cards may be face up or face down.

GameEngine类通过调用GamePlayer.TakeTurn(GameEngine eng)方法允许AI玩家轮到他/她。玩家可以向游戏引擎询问防守玩家的场地,以便玩家可以根据那里的牌数以及哪些牌面朝上来做出决定。假设这个方法是GameEngine.GetDefendingField()

The GameEngine class allows an AI player to take his/her turn by calling the GamePlayer.TakeTurn(GameEngine eng) method. The player can ask the game engine for the defending player's field so the player can make decisions based on the number of cards there and which ones are face up. Let's say this method is GameEngine.GetDefendingField()

现在,我想确保攻击玩家无法修改防守玩家的场地或防守玩家场地中的牌并且攻击玩家只能识别防守球员区域中的正面牌。

Now, I want to make sure that the attacking player cannot modify the defending player's field or the cards in the defending player's field and that the attacking player can only identify the face up cards in the defending players field.

我有卡,场(卡片阵列列表),GamePlayer和GameEngine的课程。有没有办法为Field创建一个界面,以便GameEngine可以返回防守玩家的场地而攻击玩家无法改变它?并且没有攻击玩家能够将防御玩家的场重建为可以改变的东西。

I have classes for Card, Field (an ArrayList of Cards), GamePlayer and GameEngine. Is there any way to create an interface for Field so that the GameEngine can return the defending player's field without the attacking player being able to change it? and without the attacking player being able to recast the defending player's field into something that can be changed.

我可以让GameEngine.GetDefendingField()返回一个只读接口一个无法重建到Field的字段?

Can I have GameEngine.GetDefendingField() return a read-only interface to a Field that cannot be recast to a Field?

tia,

Sh -

推荐答案

如果你想在没有复制的情况下实现这一点,并且没有可能将只读接口引用转换为相应的读写接口,你可以尝试使用包装器方法。

If you want to achieve this without copying and without the possibility to cast the read-only interface reference to a corresponding read-write interface, you could try to use a wrapper approach.

对于这样的接口:

interface Info {
    String getInfoA();
    void setInfoA(String infoA);
    String getInfoB();
    void setInfoB(String infoB);
}

您可以创建一个只读包装器,它会忽略setter或throws异常,例如:

You could create a readonly wrapper which ignores the setters or throws exceptions, like:

class ReadonlyInfo implements Info {
    final Info instance;
    ReadonlyInfo(Info info) { 
         if (null == info) { throw new InvalidArgumentException(); } 
         instance = info; 
    }
    String getInfoA() { return instance.getInfoA(); }
    void setInfoA(String infoA) { /** silent ignore */ }
    String getInfoB() { return instance.getInfoB(); }
    void setInfoB(String infoA) { throw new IllegalStateException(); }
}

通过使用包装器方法,您可以在客户端使用多态,安全地反对转换引用以获得更多的访问权限,并在默默地忽略被调用的setter或者抛出非法访问异常之间进行选择。

By using the wrapper approach you can use polymorphism on the client side, be safe against casting the reference to get more access rights and chose between silently ignore called setters, or throwing an illegal access exception.

注意:当然你不会安全使用反射来检索包装对象的代码。

Note: of course you will not be safe against code that uses reflection to retrieve the wrapped object.

这篇关于如何创建两个Java类的接口只读一个,一个读写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆