Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 - Fatal编程技术网

Java 设计模式-单个对象只能存在于单个事件中

Java 设计模式-单个对象只能存在于单个事件中,java,design-patterns,Java,Design Patterns,我正在为这个小游戏寻找合适的设计模式 一个玩家只能存在于单个事件中 但只要旧事件终止==true,就可以在新事件中添加(存在)玩家 什么设计模式适合我的场景?我当前的设置有意义吗 public class Player { public int id; public Player(int id) { this.id = id; } } public class Event { public List<Player> a

我正在为这个小游戏寻找合适的设计模式

  • 一个玩家只能存在于单个事件中
  • 但只要旧事件终止==true,就可以在新事件中添加(存在)玩家
什么设计模式适合我的场景?我当前的设置有意义吗

public class Player 
{
    public int id;  
    public Player(int id) {
        this.id = id;
    }
}

 public class Event 
 {
    public List<Player> allPlayer = new ArrayList<Player>();    
    public boolean terminated = false;
    public Event(){}        
    public void addPlayer(Player player){
        allPlayer.add(player);
    }       
}

public class Battle extends Event
{
    public Battle(){}   
}

public class Training extends Event
{    
    public Training(){}
}
公共类播放器
{
公共int id;
公共播放器(int-id){
this.id=id;
}
}
公开课活动
{
public List allPlayer=new ArrayList();
公共布尔终止=假;
公共事件(){}
公共无效添加玩家(玩家){
allPlayer.add(player);
}       
}
公众阶级斗争扩大了这一事件
{
公共战斗({}
}
公开课培训扩展活动
{    
公共培训({}
}

您可以将
事件
委托给
玩家
,当事件终止时,您可以使用
终止
标志确定是否可以将玩家与其他事件关联

public class Player {
    private Event event;

    private int id;

    ...

    public void setEvent(Event event) throws Exception {
        if (!this.event.terminated) {
            throw new Exception("Player has an event to be associated with");
        } else {
            this.event = event;
        } 
    }
}

请注意,还有其他方法通知
玩家
事件已终止。

您可以将
事件
委托给
玩家
,当事件终止时,您可以使用
终止
标志确定是否能够将玩家与其他事件关联

public class Player {
    private Event event;

    private int id;

    ...

    public void setEvent(Event event) throws Exception {
        if (!this.event.terminated) {
            throw new Exception("Player has an event to be associated with");
        } else {
            this.event = event;
        } 
    }
}

请注意,还有其他方法通知
玩家
事件已终止。

您可以将
事件
委托给
玩家
,当事件终止时,您可以使用
终止
标志确定是否能够将玩家与其他事件关联

public class Player {
    private Event event;

    private int id;

    ...

    public void setEvent(Event event) throws Exception {
        if (!this.event.terminated) {
            throw new Exception("Player has an event to be associated with");
        } else {
            this.event = event;
        } 
    }
}

请注意,还有其他方法通知
玩家
事件已终止。

您可以将
事件
委托给
玩家
,当事件终止时,您可以使用
终止
标志确定是否能够将玩家与其他事件关联

public class Player {
    private Event event;

    private int id;

    ...

    public void setEvent(Event event) throws Exception {
        if (!this.event.terminated) {
            throw new Exception("Player has an event to be associated with");
        } else {
            this.event = event;
        } 
    }
}

请注意,还有其他方法通知
玩家
事件已终止。

您可以为Player:participating添加一个布尔参数,该参数指示玩家是否加入一个事件

public class Player {

public int id; 
public boolean participating;

public Player(int id) {
    this.id = id;
}
//Get and Set method here...
}
在每个事件中,只需检查此属性

public class Event {

public List<Player> allPlayer = new ArrayList<Player>();

public boolean terminated = false;

public Event(){}

public void addPlayer(Player player){
    if(player.getParticipating())//If the player is participating in other event, or already participating in this event, return
       return;
    allPlayer.add(player);
    player.setParticipating(true);
}

public void terminate(){
   for(Player p : allPlayer){
       p.setParticipating(false);
   }
   terminated = true;
}

}
公共类事件{
public List allPlayer=new ArrayList();
公共布尔终止=假;
公共事件(){}
公共无效添加玩家(玩家){
if(player.getParticipating())//如果该玩家正在参与其他事件,或者已经参与此事件,则返回
返回;
allPlayer.add(player);
player.sets(true);
}
公共无效终止(){
对于(玩家p:allPlayer){
p、 设定值(假);
}
终止=真;
}
}

您可以为Player:participating添加一个布尔参数,该参数指示玩家是否正在参加一个事件

public class Player {

public int id; 
public boolean participating;

public Player(int id) {
    this.id = id;
}
//Get and Set method here...
}
在每个事件中,只需检查此属性

public class Event {

public List<Player> allPlayer = new ArrayList<Player>();

public boolean terminated = false;

public Event(){}

public void addPlayer(Player player){
    if(player.getParticipating())//If the player is participating in other event, or already participating in this event, return
       return;
    allPlayer.add(player);
    player.setParticipating(true);
}

public void terminate(){
   for(Player p : allPlayer){
       p.setParticipating(false);
   }
   terminated = true;
}

}
公共类事件{
public List allPlayer=new ArrayList();
公共布尔终止=假;
公共事件(){}
公共无效添加玩家(玩家){
if(player.getParticipating())//如果该玩家正在参与其他事件,或者已经参与此事件,则返回
返回;
allPlayer.add(player);
player.sets(true);
}
公共无效终止(){
对于(玩家p:allPlayer){
p、 设定值(假);
}
终止=真;
}
}

您可以为Player:participating添加一个布尔参数,该参数指示玩家是否正在参加一个事件

public class Player {

public int id; 
public boolean participating;

public Player(int id) {
    this.id = id;
}
//Get and Set method here...
}
在每个事件中,只需检查此属性

public class Event {

public List<Player> allPlayer = new ArrayList<Player>();

public boolean terminated = false;

public Event(){}

public void addPlayer(Player player){
    if(player.getParticipating())//If the player is participating in other event, or already participating in this event, return
       return;
    allPlayer.add(player);
    player.setParticipating(true);
}

public void terminate(){
   for(Player p : allPlayer){
       p.setParticipating(false);
   }
   terminated = true;
}

}
公共类事件{
public List allPlayer=new ArrayList();
公共布尔终止=假;
公共事件(){}
公共无效添加玩家(玩家){
if(player.getParticipating())//如果该玩家正在参与其他事件,或者已经参与此事件,则返回
返回;
allPlayer.add(player);
player.sets(true);
}
公共无效终止(){
对于(玩家p:allPlayer){
p、 设定值(假);
}
终止=真;
}
}

您可以为Player:participating添加一个布尔参数,该参数指示玩家是否正在参加一个事件

public class Player {

public int id; 
public boolean participating;

public Player(int id) {
    this.id = id;
}
//Get and Set method here...
}
在每个事件中,只需检查此属性

public class Event {

public List<Player> allPlayer = new ArrayList<Player>();

public boolean terminated = false;

public Event(){}

public void addPlayer(Player player){
    if(player.getParticipating())//If the player is participating in other event, or already participating in this event, return
       return;
    allPlayer.add(player);
    player.setParticipating(true);
}

public void terminate(){
   for(Player p : allPlayer){
       p.setParticipating(false);
   }
   terminated = true;
}

}
公共类事件{
public List allPlayer=new ArrayList();
公共布尔终止=假;
公共事件(){}
公共无效添加玩家(玩家){
if(player.getParticipating())//如果该玩家正在参与其他事件,或者已经参与此事件,则返回
返回;
allPlayer.add(player);
player.sets(true);
}
公共无效终止(){
对于(玩家p:allPlayer){
p、 设定值(假);
}
终止=真;
}
}

事件(主题)和玩家(观察者)之间的玩家类和观察者的单例模式。当事件结束时,它的参与者被通知,并且他们将变量“existsInAnyEvent”指定为false,例如。事件(主题)和参与者(观察者)之间的参与者类和观察者的单例模式。当事件结束时,它的参与者被通知,并且他们将变量“existsInAnyEvent”指定为false,例如。事件(主题)和参与者(观察者)之间的参与者类和观察者的单例模式。当事件结束时,它的参与者被通知,并且他们将变量“existsInAnyEvent”指定为false,例如。事件(主题)和参与者(观察者)之间的参与者类和观察者的单例模式。当事件完成时,它的参与者将被通知,他们将变量“existsInAnyEvent”指定为false,例如。谢谢主席先生,在setEvent方法中,我需要再次检查(this.Event.terminated==true)this.Event=Event;是的,我相应地修改了我的答案:)当心
Player.event
为空时的NullPointerException。用上述c